Dan-Nolan Dan-Nolan - 1 year ago 75
Javascript Question

Modify file in place (same dest) using Gulp.js and a globbing pattern

I have a gulp task that is attempting to convert .scss files into .css files (using gulp-ruby-sass) and then place the resulting .css file into the same place it found the original file. The problem is, since I'm using a globbing pattern, I don't necessarily know where the original file is stored.

In the code below I'm trying to use gulp-tap to tap into the stream and figure out the file path of the current file the stream was read from:

gulp.task('convertSass', function() {
var fileLocation = "";
fileLocation = path.dirname(file.path);

Based on the output of the
, this code seems like it should work fine. However, the resulting CSS files seem to be placed one directory higher than I'm expecting. Where it should be
, the resulting file path is just

If there's a much simplier way of doing this, I would definitely appreciate that solution even more. Thanks!

Answer Source

As you suspected, you are making this too complicated. The destination doesn't need to be dynamic as the globbed path is used for the dest as well. Simply pipe to the same base directory you're globbing the src from, in this case "sass":


If your files do not have a common base and you need to pass an array of paths, this is no longer sufficient. In this case, you'd want to specify the base option.

var paths = [
gulp.src(paths, {base: "./"})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download