为什么gulp-sourcemaps输出会中断gulp-strip-css-comments?

时间:2016-11-03 22:04:54

标签: gulp gulp-sass gulp-sourcemaps

我想删除CSS代码中的所有注释,但我注意到当我将gulp-strip-css-comments添加到我的gulp任务时,源图不再有效,即使我确定 gulp-strip-css-comments 使用正则表达式选项保留指向源地图/*# sourceMappingURL=style.css.map */的注释。

下面我在使用 gulp-strip-css-comments 之前和之后添加了我的代码和输出:

在gulp-strip-css-comments之前

gulp.task('sass:dev', function () {
  gulp.src('./sass/*.scss')
    .pipe(sourcemaps.init())
    .pipe(
      sass({
        includePaths: [
          './node_modules/breakpoint-sass/stylesheets/'
        ]
      })
      .on('error', sass.logError)
    )
    .pipe(autoprefixer({
      browsers: ['last 2 version', 'ie 11']
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('./css'));
});

enter image description here

gulp-strip-css-comments

之后
gulp.task('sass:dev', function () {
  gulp.src('./sass/*.scss')
    .pipe(sourcemaps.init())
    .pipe(
      sass({
        includePaths: [
          './node_modules/breakpoint-sass/stylesheets/'
        ]
      })
      .on('error', sass.logError)
    )
    .pipe(autoprefixer({
      browsers: ['last 2 version', 'ie 11']
    }))
    .pipe(sourcemaps.write('.'))
    .pipe(stripCssComments({preserve: /^# sourceMappingURL=/}))
    .pipe(gulp.dest('./css'));
});

enter image description here

1 个答案:

答案 0 :(得分:0)

在sourcemaps .pipe之前放置stripCssComments代码(sourcemaps.write('。'))



gulp.task('sass:dev', function () {
  gulp.src('./sass/*.scss')
    .pipe(sourcemaps.init())
    .pipe(
      sass({
        includePaths: [
          './node_modules/breakpoint-sass/stylesheets/'
        ]
      })
      .on('error', sass.logError)
    )
    .pipe(autoprefixer({
      browsers: ['last 2 version', 'ie 11']
    }))
    .pipe(stripCssComments({preserve: /^# sourceMappingURL=/}))  
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest('./css'));
});