I am using Gulp to store multiple SVG files into SVG storages which are later reused in order to maximize performance. However, I am now using a rather interesting folder structure where the paths contain interchangeable fixed and dynamic parts.
Let me illustrate. Here is the folder structure I am using. The app/images/products
folder contains folders named productA
, productB
etc. which again contain a subfolder called color-sprites
full of SVG files ready to get combined into a single file and stored in build/images/products/{product name}/color-sprites.svg
.
root/
|_ gulpfile.js
|_ app
|_ images
|_ products
|_ **productA**
|_ color-sprites
|_ sprite1.svg
|_ sprite2.svg
|_ ...
|_ build
|_ images
|_ products
|_ **productA**
|_ color-sprites.svg
Here is a simplified version of the code I am trying to use to accomplish what I need (take note that rename
is an instance of gulp-rename
).
gulp.task('svg-color-sprites', function () {
return gulp.src('app/images/products/**/color-sprites/*.+(svg)')
.pipe(svgMin())
.pipe(svgStore())
.pipe(rename(...))
.pipe(gulp.dest('build/images/'));
});
The problem is that I am not exactly sure how to access current stream parameters in order to construct a target with gulp-rename
because running the gulpfile.js
like this results in taking the first parent element of the path before *
or **
which again results in creating a file called products.svg
in build/images/
. I've seen a couple of examples which use path
to access the basename, relative path etc. but I am not sure how to use it in the context of gulp streams.