Search code examples
javascriptsassgulpgulp-sass

Write to compiled sass to parent directory of file


Say I have the file structure:

styles
    truly
        very
            deep
                afile.sass

And the desired output is

styles
    truly
        very
            afile.css
            deep
                afile.sass

What I've tried so far:

gulp.task('sass', function () {
    return gulp.src('./truly/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('truly'));
});

Almost, but compiles to the same directory.

The following are the gulp.dest() parameters I've tried:

..

Outputs nothing, probably understanding outside of directory root.

.

Replicates the structure from very onwards from the root.

truly/..

Also replicates outside, I've tried many others with no success either.


Solution

  • See my answer at modify parent directories a very similar question.

    So without testing:

    const flatten = require('gulp-flatten');    
    
    gulp.task('sass', function () {
      return gulp.src('truly/**/*.scss')
        .pipe(sass().on('error', sass.logError))
    
        // will strip off the last directory, 'deep' in your case
        .pipe(flatten({ subPath: [0, -1] }))
    
        .pipe(gulp.dest('truly'));
    });