gulp-uglify ![](https://camo.githubusercontent.com/c2904ebfd5450a09e8e381de376ff5974a93829621c5ab58b3dd97d9f8a3c4c1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f746572696e6a6f6b65732f67756c702d75676c6966792f6d61737465722e7376673f6c6162656c3d5472617669732532304349267374796c653d666c61742d737175617265)
![](https://camo.githubusercontent.com/225c152488670183bc5716b594a4b74f26612a9c5a933c735c7395b2f14cc344/68747470733a2f2f696d672e736869656c64732e696f2f6170707665796f722f63692f746572696e6a6f6b65732f67756c702d75676c6966792f6d61737465722e7376673f6c6162656c3d4170705665796f72267374796c653d666c61742d737175617265)
![](https://camo.githubusercontent.com/743f7215bc06c08d26909e161875c07966d5041c70e1bf36580d52e99b2ca524/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f67756c702d75676c6966792e7376673f7374796c653d666c61742d737175617265)
![](https://camo.githubusercontent.com/3d09f300b4e29d191a80f67912a5e31c463e9032b062ca989fa2c22dd9c93803/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f67756c702d75676c6966792e7376673f7374796c653d666c61742d737175617265)
![](https://camo.githubusercontent.com/7fdfb91eb34d0c90a1a7b0fabfc5e45473ffbf8f58470c499498f4436a300b22/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f746572696e6a6f6b65732f67756c702d75676c6966792f6d61737465722e7376673f7374796c653d666c61742d737175617265)
Minify JavaScript with UglifyJS3.
Installation
Install package with NPM and add it to your development dependencies:
npm install --save-dev gulp-uglify
Usage
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')
);
});
To help properly handle error conditions with Node streams, this project recommends the use of pipeline
, from readable-stream
.
Options
Most of the minify options from the UglifyJS API are supported. There are a few exceptions:
- The
sourceMap
option must not be set, as it will be automatically configured based on your Gulp configuration. See the documentation for Gulp sourcemaps.
Errors
gulp-uglify
emits an 'error' event if it is unable to minify a specific file. The GulpUglifyError constructor is exported by this plugin for instanceof
checks. It contains the following properties:
fileName
: The full file path for the file being minified.cause
: The original UglifyJS error, if available.
Most UglifyJS error messages have the following properties:
message
(ormsg
)filename
line
To see useful error messages, see Why Use Pipeline?.
Using a Different UglifyJS
By default, gulp-uglify
uses the version of UglifyJS installed as a dependency. It's possible to configure the use of a different version using the "composer" entry point.
var uglifyjs = require('uglify-js'); // can be a git checkout
// or another module (such as `uglify-es` for ES6 support)
var composer = require('gulp-uglify/composer');
var pump = require('pump');
var minify = composer(uglifyjs, console);
gulp.task('compress', function (cb) {
// the same options as described above
var options = {};
pump([
gulp.src('lib/*.js'),
minify(options),
gulp.dest('dist')
],
cb
);
});