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

后端 未结 5 833
滥情空心
滥情空心 2020-11-28 03:36

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 o

相关标签:
5条回答
  • 2020-11-28 04:16

    This is simpler than numbers1311407 has led on. You don't need to specify the destination folder at all, simply use .. Also, be sure to set the base directory.

    gulp.src("sass/**/*.scss", { base: "./" })
        .pipe(sass())
        .pipe(gulp.dest("."));
    
    0 讨论(0)
  • 2020-11-28 04:20
    gulp.src("sass/**/*.scss")
      .pipe(sass())
      .pipe(gulp.dest(function(file) {
        return file.base;
      }));
    

    Originally answer given here: https://stackoverflow.com/a/29817916/3834540.

    I know this thread is old but it still shows up as the first result on google so I thought I might as well post the link here.

    0 讨论(0)
  • 2020-11-28 04:22

    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":

    gulp.src("sass/**/*.scss")
      .pipe(sass())
      .pipe(gulp.dest("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 = [
      "sass/**/*.scss", 
      "vendor/sass/**/*.scss"
    ];
    gulp.src(paths, {base: "./"})
      .pipe(sass())
      .pipe(gulp.dest("./"));
    
    0 讨论(0)
  • 2020-11-28 04:29

    if you want to save all files in their own path in the dist folder

    const media = () => {
        return gulp.src('./src/assets/media/**/*')
            .pipe(gulp.dest(file => file.base.replace('src', 'dist'))
        )
    }
    
    const watch = () => {
      gulp.watch(
        "./src/**/*",
        media
      );
    };
    
    0 讨论(0)
  • 2020-11-28 04:32

    This was very helpful!

    gulp.task("default", function(){
    
        //sass
        gulp.watch([srcPath + '.scss', '!'+ srcPath +'min.scss']).on("change", function(file) {
             console.log("Compiling SASS File: " + file.path)
             return gulp.src(file.path, { base: "./" })
            .pipe(sass({style: 'compressed'}))
            .pipe(rename({suffix: '.min'}))
            .pipe(sourcemaps.init())
            .pipe(autoprefixer({
                browsers: ['last 2 versions'],
                cascade: false
            }))
            .pipe(sourcemaps.write('./'))         
            .pipe(gulp.dest(".")); 
        });
    
        //scripts
        gulp.watch([srcPath + '.js','!'+ srcPath + 'min.js']).on("change", function(file) {
            console.log("Compiling JS File: " + file.path)
            gulp.src(file.path, { base: "./" })
            .pipe(uglify())
            .pipe(rename({suffix: '.min'}))       
            .pipe(gulp.dest(".")); 
        });
    })
    
    0 讨论(0)
提交回复
热议问题