More gulp common plug-ins use please visit: gulp common plug-ins summary
gulp-uglify This is a narrow use UglifyJS js file.
Greater use of the document, please click visit gulp-uglify tool official website .
installation
A key installation much explanation
npm install --save-dev gulp-uglify
use
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var pipeline = require('readable-stream').pipeline;
gulp.task('compress', function () {
return pipeline(
gulp.src('lib/*.js'),
uglify(),
gulp.dest('dist')
);
});
In order to help properly handle error conditions Node stream, the project recommends using Pipeline from the readable-Stream .
Options
support UglifyJS API Most minify options . There are some exceptions:
sourceMap
not set this option, because it according to your Gulp
configure automatic configuration. See Gulp source map document
Errors
gulp-uglify
If you can not shrink a particular file, the issue "error" event. The plug-export GulpUglifyError
constructor to be instanceof
checked. It contains the following properties:
fileName
: Full file path of the file to be reduced.cause
: The original UglifyJS error (if any).
Most UglifyJS error message has the following properties:message(或msg)
filename
line
Using different UglifyJS
default, gulp-uglify
use UglifyJS version is installed as a dependency. You can use "composer" entry points configured to use other versions.
var uglifyjs = require('uglify-js'); // 可以是git checkout
// 或其他模块(例如ES6支持的`uglify-es`)
var composer = require('gulp-uglify/composer');
var pump = require('pump');
var minify = composer(uglifyjs, console);
gulp.task('compress', function (cb) {
// 与上述相同的选项
var options = {};
pump([
gulp.src('lib/*.js'),
minify(options),
gulp.dest('dist')
],
cb
);
});