11

Is it possible to generate a sourcemap with all these transformations?

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'));
});

Libraries used:

I know less can generate sourcemaps, gulp-concat I don't think supports them, autoprefixer is supposed to preserve them, and minify-css/clean-css don't seem to make any mention of source maps. Am I out of luck?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

7

I've been having the same issue tonight, and I'm still working on finding out a clean solution. However, I just managed to get something working so I thought I would share:

Note: I'm using neither gulpif nor minify

Use gulp-sourcemaps since gulp-less seems to come without the source map option (at least for the moment).

Replace gulp-concat by gulp-concat-sourcemap to preserve the sourcemaps.

Gulp-concat isn't currently supported by gulp-sourcemaps (cf. README). Until that gets fixed there's a patched version to grab here: 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'));
});
Theo.T
  • 8,905
  • 3
  • 24
  • 38
  • Almost there; are you doing any kind of minification? – mpen Jul 02 '14 at 02:17
  • Not yet, doing this for dev env only ftm. Any hints please let me/us know ! – Theo.T Jul 02 '14 at 08:03
  • Good point, maybe I should drop the minification step on dev too. I'm not sure I need sourcemaps in production anyway. – mpen Jul 02 '14 at 18:52
  • That would be a waste indeed, but as a proof of concept it would be nice to see it working :-) ... I noticed all the line numbers in my source maps were offset by about 7 lines. Not sure where that's coming from. Again not a big deal but just makes it all feel a bit unstable. – Theo.T Jul 02 '14 at 22:36
  • My line numbers are even more off than that. Paths are all screwy too, can't seem to get them right. – mpen Jul 03 '14 at 00:36
  • Looks all perfectly neat on my side (for now). I've edited the answer so check the diff. – Theo.T Jul 03 '14 at 12:24
  • 2
    tx, looks like gulp-concat is supported now… – ptim Jul 21 '14 at 04:39
  • 1
    Thanks @memeLab for letting us know, I've updated the answer. – Theo.T Jul 23 '14 at 10:41
  • Please edit it to say you are using "gulp-sourcemapS" instead of "gulp-sourcemap." Caused me a major headache! – Jdban101 Feb 17 '17 at 23:28
3

Since version 2 of Less you can use plugins for the Less compiler. Also gulp-less allows you to use these plugins (programmatic) see also http://lesscss.org/usage/#plugins-using-a-plugin-in-code

Documentation of gulp-less describes how to use the clean-css and autoprefix plugin at https://github.com/plus3network/gulp-less#plugins. Notice that gulp-minify-css is leveraging clean-css's code too.

Also the usage of gulp-less with gulp-sourcemaps to create sourcemaps has been described at https://github.com/plus3network/gulp-less#source-maps

So you should be able to use:

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'));

The above should generate the autoprefixed and minified CSS of your Less source, with CSS sourcemaps written into ./public/css/maps

Alternatively you could consider to use the Pleeease plugin Pleeease can run many postprocessors once, see http://pleeease.io/docs/#features

Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • I've found when loading the CSS into chrome, that the maps files don't point to the correct lines of code? – Alex KeySmith Jan 21 '15 at 14:40
  • by default your Less code had been include in the sourcemap, see also https://github.com/floridoo/gulp-sourcemaps#write-options for the `includeContent` and `sourceRoot` options – Bass Jobsen Jan 25 '15 at 00:26