1 | # gulp-uglify [![][travis-shield-img]][travis-shield][![][appveyor-shield-img]][appveyor-shield][![][npm-dl-shield-img]][npm-shield][![][npm-v-shield-img]][npm-shield][![][coveralls-shield-img]][coveralls-shield]
|
2 |
|
3 | > Minify JavaScript with UglifyJS3.
|
4 |
|
5 | ## Installation
|
6 |
|
7 | Install package with NPM and add it to your development dependencies:
|
8 |
|
9 | `npm install --save-dev gulp-uglify`
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```javascript
|
14 | var gulp = require('gulp');
|
15 | var uglify = require('gulp-uglify');
|
16 | var pipeline = require('readable-stream').pipeline;
|
17 |
|
18 | gulp.task('compress', function () {
|
19 | return pipeline(
|
20 | gulp.src('lib/*.js'),
|
21 | uglify(),
|
22 | gulp.dest('dist')
|
23 | );
|
24 | });
|
25 | ```
|
26 |
|
27 | To help properly handle error conditions with Node streams, this project
|
28 | recommends the use of
|
29 | [`pipeline`](https://nodejs.org/docs/latest/api/stream.html#stream_stream_pipeline_streams_callback),
|
30 | from [`readable-stream`](https://github.com/nodejs/readable-stream).
|
31 |
|
32 | ## Options
|
33 |
|
34 | Most of the [minify options](https://github.com/mishoo/UglifyJS2#minify-options) from
|
35 | the UglifyJS API are supported. There are a few exceptions:
|
36 |
|
37 | 1. The `sourceMap` option must not be set, as it will be automatically configured
|
38 | based on your Gulp configuration. See the documentation for [Gulp sourcemaps][gulp-sm].
|
39 |
|
40 | [gulp-sm]: https://github.com/gulp-sourcemaps/gulp-sourcemaps#usage
|
41 |
|
42 | ## Errors
|
43 |
|
44 | `gulp-uglify` emits an 'error' event if it is unable to minify a specific file.
|
45 | The GulpUglifyError constructor is exported by this plugin for `instanceof` checks.
|
46 | It contains the following properties:
|
47 |
|
48 | - `fileName`: The full file path for the file being minified.
|
49 | - `cause`: The original UglifyJS error, if available.
|
50 |
|
51 | Most UglifyJS error messages have the following properties:
|
52 |
|
53 | - `message` (or `msg`)
|
54 | - `filename`
|
55 | - `line`
|
56 |
|
57 | To see useful error messages, see [Why Use Pipeline?](docs/why-use-pipeline/README.md#why-use-pipeline).
|
58 |
|
59 | ## Using a Different UglifyJS
|
60 |
|
61 | By default, `gulp-uglify` uses the version of UglifyJS installed as a dependency.
|
62 | It's possible to configure the use of a different version using the "composer" entry point.
|
63 |
|
64 | ```javascript
|
65 | var uglifyjs = require('uglify-js'); // can be a git checkout
|
66 | // or another module (such as `uglify-es` for ES6 support)
|
67 | var composer = require('gulp-uglify/composer');
|
68 | var pump = require('pump');
|
69 |
|
70 | var minify = composer(uglifyjs, console);
|
71 |
|
72 | gulp.task('compress', function (cb) {
|
73 | // the same options as described above
|
74 | var options = {};
|
75 |
|
76 | pump([
|
77 | gulp.src('lib/*.js'),
|
78 | minify(options),
|
79 | gulp.dest('dist')
|
80 | ],
|
81 | cb
|
82 | );
|
83 | });
|
84 | ```
|
85 |
|
86 | [travis-shield-img]: https://img.shields.io/travis/terinjokes/gulp-uglify/master.svg?label=Travis%20CI&style=flat-square
|
87 | [travis-shield]: https://travis-ci.org/terinjokes/gulp-uglify
|
88 | [appveyor-shield-img]: https://img.shields.io/appveyor/ci/terinjokes/gulp-uglify/master.svg?label=AppVeyor&style=flat-square
|
89 | [appveyor-shield]: https://ci.appveyor.com/project/terinjokes/gulp-uglify
|
90 | [npm-dl-shield-img]: https://img.shields.io/npm/dm/gulp-uglify.svg?style=flat-square
|
91 | [npm-shield]: https://yarnpkg.com/en/package/gulp-uglify
|
92 | [npm-v-shield-img]: https://img.shields.io/npm/v/gulp-uglify.svg?style=flat-square
|
93 | [coveralls-shield-img]: https://img.shields.io/coveralls/terinjokes/gulp-uglify/master.svg?style=flat-square
|
94 | [coveralls-shield]: https://coveralls.io/github/terinjokes/gulp-uglify
|