Buy

Yep, we're combining multiple files into one. That's pretty cool. But our main.css has an embarrassing amount of whitespace. Gross. Let's minify this.

Another plugin to the rescue! This one is called gulp-minify-css. And yea, there are a lot of plugins that can minify CSS. But this is a good one, and it's a superfriend with the sourcemaps plugin. They've even given us a nice install line, so I'll copy that, stop my watch task, and get it downloading:

npm install --save-dev gulp-minify-css

Next, go steal the require line and paste it on top:

27 lines gulpfile.js
var gulp = require('gulp');
... lines 2 - 27

Oh, and let's put a var before that.

And once again, we're going to use the trusty pipe() function to push things through minifyCSS():

27 lines gulpfile.js
... lines 1 - 12
gulp.src(config.assetsDir+'/'+config.sassPattern)
... lines 14 - 15
.pipe(concat('main.css'))
.pipe(minifyCSS())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('web/css'));
});
... lines 21 - 27

This is looking really nice. Oh, and most of these functions - like minifyCSS() or sass() do take some options. So if you need to customize how things are minified, you can totally do that.

Ok, go back and run gulp!

gulp

And now main.css is a single line. BUT, through the power of sourcemaps, we still get the correct styles.scss line in the inspector.

Leave a comment!

  • 2016-05-26 Daniel Noyola

    it works with gulp-clean-css, I repeat, it works (at the moment of writing this xD)

  • 2016-05-05 weaverryan

    Yep - the JavaScript world moves at the speed of light! Some of the packages we use in this tutorial are deprecated now. Using the new package should be pretty much the same, but I haven't tried it yet to be sure :).

  • 2016-05-05 kaizoku

    Hey there, MinifyCss is deprecated. Use gulp-clean-css instead

    npm WARN deprecated gulp-minify-css@1.2.4: Please use gulp-clean-css