Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMargaret Johnson
4,275 PointsGulp - how to loop through directories and perform same task on it's contents?
I want to compress SVGs that are used per page and then inject the compressed SVG stream into a file using a gulp task. I have directories such as ..svg/index , ...svg/about, etc. My current gulp task implementation hard codes the input and output directories and files. What is the best way to feed in a directory then do the task? Here is my current gulpfile.js:
'use strict';
const gulp = require('gulp');
const svgstore = require('gulp-svgstore');
const inject = require('gulp-inject');
const debug = require('gulp-debug');
const svgmin = require('gulp-svgmin');
// Useful functions.
function fileContents(filePath, file) {
return file.contents.toString();
}
// Read in the SVGs used by index.html and inject the stream
// into index.html.
gulp.task('make_sprite_index',function(){
// Compile the SVGs into a stream.
var svgs = gulp
.src('client/design/svg/index/*.svg')
.pipe(debug())
.pipe(svgmin())
.pipe(svgstore({
inlineSvg: true
}))
return gulp
// Flask based server stores html in the templates folder.
.src('client/design/html/index.html')
.pipe(inject(svgs, {
transform: fileContents
}))
.pipe(gulp.dest('templates'));
});
gulp.task('make_sprite_layout',function(){
// Compile/minimize the SVGs into a stream.
var svgs = gulp
.src('client/design/svg/layout/*.svg')
.pipe(debug())
.pipe(svgmin())
.pipe(svgstore({
inlineSvg: true
}))
// return the file in the distribution directory after SVG has been injected.
return gulp
// Flask based server stores html in the templates folder.
.src('client/design/html/layout.html')
.pipe(inject(svgs, {
transform: fileContents
}))
.pipe(gulp.dest('templates'));
});
thank you.