Minify
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
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:
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()
:
// ... 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.
Hey there, MinifyCss is deprecated. Use gulp-clean-css instead
npm WARN deprecated gulp-minify-css@1.2.4: Please use gulp-clean-css