Minifying JS
Syntax#
extAn object that specifies output source and minified file extensions.sourceThe suffix string of the filenames that output source files ends with.minWhen string: The suffix string of the filenames that output minified files ends with.- When Array: The regex expressions to be replaced with input filenames. For example: [/.(.*)-source.js$/, ‘$1.js’]
excludeWill not minify files in the dirs.noSourceWill not output the source code in the dest dirs.ignoreFilesWill not minify files which matches the pattern.manglePassfalseto skip mangling names.outputPass an object if you wish to specify additionaloutput options. The defaults are optimized for best compression.compressPass an object to specify customcompressor options. Pass false to skip compression completely.preserveCommentsA convenience option for options.output.comments. Defaults to preserving no comments.allPreserve all comments in code blockssomePreserve comments that start with a bang(!)or include a Closure Compiler directive(@preserve, @license, @cc_on)functionSpecify your own comment preservation function. You will be passed the current node and the current comment and are expected to return eithertrueorfalse.
Remarks#
Minify JS using gulp-minify
First, Install gulp and gulp-minify to project directory locally
npm install --save-dev gulp gulp-minifyThen add following min-js task to your gulpfile.js
var gulp = require('gulp');
var minify = require('gulp-minify');
gulp.task('min-js', function() {
return gulp.src('lib/*.js')
.pipe(minify({
ext: {
min: '.min.js'
},
ignoreFiles: ['-min.js']
}))
.pipe(gulp.dest('lib'))
});
gulp.task('watch', function(){
gulp.watch('lib/*.js', ['min-js']);
// Other watchers
});
gulp.task('default', ['min-js', 'watch']);This task find all js files in lib directory, minfy it and save to lib directory with .min.js suffix. For example, after minify lib/app.js file will be created a lib/app.min.js file
Besides running as a dependency for the 'default' gulp task, this task can be run manually by typing the following command:
gulp min-js