Skip to content

Commit

Permalink
Gulp: Modify 'svgsprite' task to support generation of multiple sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
astik-dev committed Feb 6, 2024
1 parent c2c94f1 commit e78ef11
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
4 changes: 2 additions & 2 deletions gulp/config/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ export const path = {
scss: `${srcFolder}/scss/style.scss`,
html: `${srcFolder}/*.html`,
files: `${srcFolder}/files/**/*.*`,
svgicons: `${srcFolder}/svgicons/*.svg`,
svgicons: `${srcFolder}/svgicons`,
},
watch: {
js: `${srcFolder}/js/**/*.js`,
scss: `${srcFolder}/scss/**/*.scss`,
html: `${srcFolder}/**/*.html`,
images: `${srcFolder}/img/**/*.{jpg,jpeg,png,svg,gif,ico,webp}`,
files: `${srcFolder}/files/**/*.*`,
svgicons: `${srcFolder}/svgicons/*.svg`,
svgicons: `${srcFolder}/svgicons/**/*.svg`,
},
buildFolder: buildFolder,
srcFolder: srcFolder,
Expand Down
44 changes: 32 additions & 12 deletions gulp/tasks/svgsprite.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import svgSprite from "gulp-svg-sprite";
import fs from "fs";

export const svgsprite = () => {
return app.gulp.src(app.path.src.svgicons)
.pipe(app.plugins.plumber())
.pipe(svgSprite({
mode: {
stack: {
sprite: "../icons/icons.svg",
}
}
}))
.pipe(app.gulp.dest(app.path.build.images))
.pipe(app.plugins.browsersync.stream());
export const svgsprite = async () => {
// Get a list of folders in the source SVG icons directory
const folders = fs.readdirSync(app.path.src.svgicons, {withFileTypes: true})
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);

// Generate tasks for each folder to create SVG sprites
const tasks = folders.map(folder => {
const srcPath = `${app.path.src.svgicons}/${folder}/*.svg`;
return new Promise ((resolve, reject) => {
app.gulp.src(srcPath)
.pipe(app.plugins.plumber())
.pipe(svgSprite({
mode: {
stack: {
sprite: `../svgsprite/${folder}.svg`,
}
}
}))
.pipe(app.gulp.dest(app.path.build.images))
.on('end', resolve)
.on('error', reject);
});
});

// Wait for all sprite generation tasks to complete
return Promise.all(tasks).then(() => {
// Refresh BrowserSync
app.gulp.src(app.path.src.svgicons)
.pipe(app.plugins.browsersync.stream());
});
}
8 changes: 4 additions & 4 deletions src/components/_footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
<div class="footer__info-socials">

<a href="#" class="footer__info-social">
<img src="img/icons/icons.svg#instagram" alt="Instagram">
<img src="img/svgsprite/socials.svg#instagram" alt="Instagram">
</a>

<a href="#" class="footer__info-social">
<img src="img/icons/icons.svg#dribbble" alt="Dribbble">
<img src="img/svgsprite/socials.svg#dribbble" alt="Dribbble">
</a>

<a href="#" class="footer__info-social">
<img src="img/icons/icons.svg#twitter" alt="Twitter">
<img src="img/svgsprite/socials.svg#twitter" alt="Twitter">
</a>

<a href="#" class="footer__info-social">
<img src="img/icons/icons.svg#youtube" alt="YouTube">
<img src="img/svgsprite/socials.svg#youtube" alt="YouTube">
</a>

</div>
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit e78ef11

Please sign in to comment.