I'm new to gulp, but I'm wondering if its possible to iterate through directories in a gulp task.
Here's what I mean, I know a lot of the tutorials / demos show processing a bunch of JavaScript files using something like "**/*.js" and then they compile it into a single JavaScript file. But I want to iterate over a set of directories, and compile each directory into it's own JS file.
For instance, I have a file structure like:
/js/feature1/something.js
/js/feature1/else.js
/js/feature1/foo/bar.js
/js/feature1/foo/bar2.js
/js/feature2/another-thing.js
/js/feature2/yet-again.js
...And I want two files: /js/feature1/feature1.min.js
and /js/feature2/feature2.min.js
where the first contains the first 4 files and the second contains the last 2 files.
Is this possible, or am I going to have to manually add those directories to a manifest? It would be really nice to pragmatically iterate over all the directories within /js/
.
Thanks for any help you can give me.
-Nate
Edit: It should be noted that I don't only have 2 directories, but I have many (maybe 10-20) so I don't really want to write a task for each directory. I want to handle each directory the same way: get all of the JS inside of it (and any sub-directories) and compile it down to a feature-based minified JS file.
There's an official recipe for this: Generating a file per folder
You could use
glob
to get a list of directories and iterate over them, usinggulp.src
to create a separate pipeline for each feature. You can then return a promise which is resolved when all of your streams haveend
ed.First, install gulp-concat & gulp-uglify.
Next, do something like:
Now, all you have to do to minify everything from the CLI is:
I am trying myself to get how streams work in node. I made a simple example for you, on how to make a stream to filter folders and start a new given stream for them.
So in your case, you can make a stream with whatever you want to make with the files in a folder ( like minify them and concatenate them ) and then pass an instance of this stream to the
forEachFolder
stream I made. Like I do with theprintFileNames
custom stream.Give it a try and let me know if it works for you.