问题 Gulp sourcemap with less / concat / autoprefixer / minifycss?


是否可以生成包含所有这些转换的源图?

gulp.task('styles', function() {
    return gulp.src(source.styles)
        .pipe(plumber())
        .pipe(gulpif(/\.less$/, plumber().pipe(less({
            strictMath: true,
            strictUnits: true,
        }))))
        .pipe(concat('bundle.css'))
        .pipe(prefixer(['last 2 versions', '> 1%', 'ie 9'], {cascade: false}))
        .pipe(minifyCss())
        .pipe(gulp.dest('public/css'));
});

使用的图书馆:

我知道  可以生成源图,gulp-concat我认为不支持它们, autoprefixer 应该保留它们,并且minify-css /清理CSS 似乎没有提到源地图。我运气不好吗?


9132
2017-07-01 20:57


起源



答案:


今晚我一直有同样的问题,我还在努力找到一个干净的解决方案。但是,我只是设法让某些东西工作,所以我想我会分享:

注意:我没有使用 gulpif 也不 minify

使用 gulp-sourcemaps 以来 gulp-less 似乎没有源映射选项(至少目前)。

更换 gulp-concat 通过 gulp-concat-sourcemap 保留源图。

Gulp-concat目前不支持gulp-sourcemaps(参见 自述)。 在修复之前,有一个修补版本可以抓住这里: https://github.com/floridoo/gulp-concat/tree/sourcemap_pipe2

gulp.task('less', function () {
  gulp.src(sources.less)
    .pipe(sourcemaps.init())
    .pipe(less())
    .pipe(prefix({ map: true })) 
    .pipe(concat('build.css'))
    .pipe(sourcemaps.write()
    .pipe(gulp.dest(dirs.build + 'assets/css'));
});

7
2017-07-02 00:43



差不多了;你在做什么样的缩小? - mpen
还没有,为dev env做这个只有ftm。任何提示请让我/我们知道! - Theo.T
tx,现在支持gulp-concat ... - ptim
感谢@memeLab让我们知道,我已经更新了答案。 - Theo.T
@Jdban101完了! - Theo.T


答案:


今晚我一直有同样的问题,我还在努力找到一个干净的解决方案。但是,我只是设法让某些东西工作,所以我想我会分享:

注意:我没有使用 gulpif 也不 minify

使用 gulp-sourcemaps 以来 gulp-less 似乎没有源映射选项(至少目前)。

更换 gulp-concat 通过 gulp-concat-sourcemap 保留源图。

Gulp-concat目前不支持gulp-sourcemaps(参见 自述)。 在修复之前,有一个修补版本可以抓住这里: https://github.com/floridoo/gulp-concat/tree/sourcemap_pipe2

gulp.task('less', function () {
  gulp.src(sources.less)
    .pipe(sourcemaps.init())
    .pipe(less())
    .pipe(prefix({ map: true })) 
    .pipe(concat('build.css'))
    .pipe(sourcemaps.write()
    .pipe(gulp.dest(dirs.build + 'assets/css'));
});

7
2017-07-02 00:43



差不多了;你在做什么样的缩小? - mpen
还没有,为dev env做这个只有ftm。任何提示请让我/我们知道! - Theo.T
tx,现在支持gulp-concat ... - ptim
感谢@memeLab让我们知道,我已经更新了答案。 - Theo.T
@Jdban101完了! - Theo.T


从Less版本2开始,您可以使用Less编译器的插件。还有gulp-less允许你使用这些插件(程序化)也参见 http://lesscss.org/usage/#plugins-using-a-plugin-in-code

gulp-less的文档描述了如何使用clean-css和autoprefix插件 https://github.com/plus3network/gulp-less#plugins。请注意,gulp-minify-css也在利用clean-css的代码。

另外,已经描述了使用gulp-less和gulp-sourcemaps来创建源映射的用法 https://github.com/plus3network/gulp-less#source-maps

所以你应该可以使用:

var LessPluginCleanCSS = require("less-plugin-clean-css"),
    cleancss = new LessPluginCleanCSS({advanced: true});

var LessPluginAutoPrefix = require('less-plugin-autoprefix'),
    autoprefix= new LessPluginAutoPrefix({browsers: ["last 2 versions"]});


gulp.src('./less/**/*.less')
  .pipe(sourcemaps.init())
  .pipe(less({
    plugins: [autoprefix, cleancss]
   }))
  .pipe(sourcemaps.write('./maps'))
  .pipe(gulp.dest('./public/css'));

上面应该生成你的Less源的autoprefixed和minified CSS,CSS源图写入./public/css/maps

或者你可以考虑使用 Pleeease插件 Pleeease可以运行一次后处理器,请参阅 http://pleeease.io/docs/#features


3
2018-01-08 00:07



我发现在将CSS加载到chrome时,map文件没有指向正确的代码行? - Alex KeySmith
默认情况下,您的Less代码已包含在sourcemap中,另请参阅 github.com/floridoo/gulp-sourcemaps#write-options 为了 includeContent 和 sourceRoot 选项 - Bass Jobsen