Skip to content

Commit 4cc8751

Browse files
committed
feat(uglify): add support for UglifyJS3
BREAKING CHANGE: The "preserveComments" option has been removed. As a replacement, set the `options.output.comments` option directly, UglifyJS3 understands the following options: - "all", to attempt to keep all comments. - "some", to keep comments containing some license text. - a RegExp - a function that should return true or false. While the "some" option works for many cases, it doesn't fully match the behavior of the "license" option. Fortunately, you can pass the exported function from `uglify-save-license` as the comment option. Fixes #284. Fixes #265.
1 parent ad73ab8 commit 4cc8751

File tree

8 files changed

+35
-223
lines changed

8 files changed

+35
-223
lines changed

README.md

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -32,59 +32,15 @@ information, see [Why Use Pump?](docs/why-use-pump/README.md#why-use-pump).
3232

3333
## Options
3434

35-
- `mangle`
35+
Most of the [minify options](https://github.com/mishoo/UglifyJS2#minify-options) from
36+
the UglifyJS API are supported. There are a few exceptions:
3637

37-
Pass `false` to skip mangling names.
38+
1. The `sourceMap` option must not be set, as it will be automatically configured
39+
based on your Gulp configuration. See the documentation for [Gulp sourcemaps][gulp-sm].
40+
2. Setting the `warnings` option has no visible affect. [See #163](gh-163).
3841

39-
- `output`
40-
41-
Pass an object if you wish to specify additional [output
42-
options](http://lisperator.net/uglifyjs/codegen). The defaults are
43-
optimized for best compression.
44-
45-
- `compress`
46-
47-
Pass an object to specify custom [compressor
48-
options](http://lisperator.net/uglifyjs/compress). Pass `false` to skip
49-
compression completely.
50-
51-
- `preserveComments`
52-
53-
A convenience option for `options.output.comments`. Defaults to preserving no
54-
comments.
55-
56-
- `all`
57-
58-
Preserve all comments in code blocks
59-
60-
- `license`
61-
62-
Attempts to preserve comments that likely contain licensing information,
63-
even if the comment does not have directives such as `@license` or `/*!`.
64-
65-
Implemented via the [`uglify-save-license`](https://github.com/shinnn/uglify-save-license)
66-
module, this option preserves a comment if one of the following is true:
67-
68-
1. The comment is in the *first* line of a file
69-
2. A regular expression matches the string of the comment.
70-
For example: `MIT`, `@license`, or `Copyright`.
71-
3. There is a comment at the *previous* line, and it matches 1, 2, or 3.
72-
73-
- `function`
74-
75-
Specify your own comment preservation function. You will be passed the
76-
current node and the current comment and are expected to return either
77-
`true` or `false`.
78-
79-
- `some` (deprecated)
80-
81-
Preserve comments that start with a bang (`!`) or include a Closure Compiler
82-
directive (`@preserve`, `@license`, `@cc_on`).
83-
Deprecated in favor of the `license` option, documented above.
84-
85-
You can also pass the `uglify` function any of the options [listed
86-
here](https://github.com/mishoo/UglifyJS2#the-simple-way) to modify
87-
UglifyJS's behavior.
42+
[gulp-sm]: https://github.com/gulp-sourcemaps/gulp-sourcemaps#usage
43+
[gh-163]: https://github.com/terinjokes/gulp-uglify/issues/163
8844

8945
## Errors
9046

@@ -105,20 +61,20 @@ To see useful error messages, see [Why Use Pump?](docs/why-use-pump/README.md#wh
10561

10662
## Using a Different UglifyJS
10763

64+
**This API may change before the v3 release. Consider it deprecated.**
65+
10866
By default, `gulp-uglify` uses the version of UglifyJS installed as a dependency.
10967
It's possible to configure the use of a different version using the "minifier" entry point.
11068

11169
```javascript
11270
var uglifyjs = require('uglify-js'); // can be a git checkout
113-
// or another module (such as `uglify-js-harmony` for ES6 support)
71+
// or another module (such as `uglify-es` for ES6 support)
11472
var minifier = require('gulp-uglify/minifier');
11573
var pump = require('pump');
11674

11775
gulp.task('compress', function (cb) {
11876
// the same options as described above
119-
var options = {
120-
preserveComments: 'license'
121-
};
77+
var options = {};
12278

12379
pump([
12480
gulp.src('lib/*.js'),

minifier.js

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
11
'use strict';
22
var through = require('through2');
33
var applySourceMap = require('vinyl-sourcemaps-apply');
4-
var saveLicense = require('uglify-save-license');
54
var isObject = require('lodash/fp/isObject');
6-
var zipObject = require('lodash/fp/zipObject');
7-
var map = require('lodash/fp/map');
8-
var prop = require('lodash/fp/prop');
9-
var _ = require('lodash/fp/placeholder');
105
var defaultsDeep = require('lodash/fp/defaultsDeep');
116
var log = require('./lib/log');
127
var createError = require('./lib/create-error');
13-
var GulpUglifyError = require('./lib/gulp-uglify-error');
14-
15-
var reSourceMapComment = /\n\/\/# sourceMappingURL=.+?$/;
168

179
var defaultOptions = defaultsDeep({
18-
fromString: true,
1910
output: {}
2011
});
2112

22-
function trycatch(fn, handle) {
23-
try {
24-
return fn();
25-
} catch (err) {
26-
return handle(err);
27-
}
28-
}
29-
3013
function setup(opts) {
3114
if (opts && !isObject(opts)) {
3215
log.warn('gulp-uglify expects an object, non-object provided');
@@ -35,24 +18,12 @@ function setup(opts) {
3518

3619
var options = defaultOptions(opts);
3720

38-
if (options.preserveComments === 'all') {
39-
options.output.comments = true;
40-
} else if (options.preserveComments === 'some') {
41-
// Preserve comments with directives or that start with a bang (!)
42-
options.output.comments = /^!|@preserve|@license|@cc_on/i;
43-
} else if (options.preserveComments === 'license') {
44-
options.output.comments = saveLicense;
45-
} else if (typeof options.preserveComments === 'function') {
46-
options.output.comments = options.preserveComments;
47-
}
48-
4921
return options;
5022
}
5123

5224
module.exports = function(opts, uglify) {
5325
function minify(file, encoding, callback) {
5426
var options = setup(opts || {});
55-
var sources;
5627

5728
if (file.isNull()) {
5829
return callback(null, file);
@@ -63,37 +34,33 @@ module.exports = function(opts, uglify) {
6334
}
6435

6536
if (file.sourceMap) {
37+
options.sourceMap = {
38+
filename: file.sourceMap.file,
39+
includeSources: true
40+
};
41+
6642
// UglifyJS generates broken source maps if the input source map
6743
// does not contain mappings.
6844
if (file.sourceMap.mappings) {
69-
options.inSourceMap = file.sourceMap;
45+
options.sourceMap.content = file.sourceMap;
7046
}
71-
options.outSourceMap = file.relative;
72-
73-
sources = zipObject(
74-
file.sourceMap.sources,
75-
file.sourceMap.sourcesContent
76-
);
7747
}
7848

79-
var mangled = trycatch(function() {
80-
var map = {};
81-
map[file.relative] = String(file.contents);
82-
var m = uglify.minify(map, options);
83-
m.code = new Buffer(m.code.replace(reSourceMapComment, ''));
84-
return m;
85-
}, createError(file, 'unable to minify JavaScript'));
49+
var fileMap = {};
50+
fileMap[file.relative] = String(file.contents);
8651

87-
if (mangled instanceof GulpUglifyError) {
88-
return callback(mangled);
52+
var mangled = uglify.minify(fileMap, options);
53+
54+
if (mangled.error) {
55+
return callback(
56+
createError(file, 'unable to minify JavaScript', mangled.error)
57+
);
8958
}
9059

91-
file.contents = mangled.code;
60+
file.contents = new Buffer(mangled.code);
9261

9362
if (file.sourceMap) {
9463
var sourceMap = JSON.parse(mangled.map);
95-
96-
sourceMap.sourcesContent = map(prop(_, sources), sourceMap.sources);
9764
applySourceMap(file, sourceMap);
9865
}
9966

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "gulp-uglify",
33
"description": "Minify files with UglifyJS.",
4-
"version": "2.1.2",
4+
"version": "3.0.0",
55
"author": "Terin Stock <[email protected]>",
66
"bugs": "https://github.com/terinjokes/gulp-uglify/issues",
77
"dependencies": {
@@ -10,14 +10,13 @@
1010
"lodash": "^4.13.1",
1111
"make-error-cause": "^1.1.1",
1212
"through2": "^2.0.0",
13-
"uglify-js": "~2.8.10",
14-
"uglify-save-license": "^0.4.1",
13+
"uglify-js": "^3.0.5",
1514
"vinyl-sourcemaps-apply": "^0.2.0"
1615
},
1716
"devDependencies": {
1817
"coveralls": "^2.11.4",
1918
"eslint": "^3.18.0",
20-
"eslint-config-prettier": "^1.5.0",
19+
"eslint-config-prettier": "^2.1.0",
2120
"eslint-config-xo": "^0.18.1",
2221
"eslint-plugin-no-use-extend-native": "^0.3.12",
2322
"eslint-plugin-prettier": "^2.0.1",
@@ -28,8 +27,8 @@
2827
"istanbul": "^0.4.0",
2928
"mississippi": "^1.2.0",
3029
"mocha": "^3.0.1",
31-
"prettier": "^1.1.0",
3230
"power-assert": "^1.4.1",
31+
"prettier": "^1.1.0",
3332
"semver": "^5.3.0",
3433
"tape": "^4.0.0",
3534
"testdouble": "^2.1.2",

test/comments.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

test/injectable.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ describe('injecting mocha', function() {
3434
},
3535
{
3636
injecting: true,
37-
fromString: true,
3837
output: {}
3938
}
4039
)

test/minify.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ var beforeEach = mocha.beforeEach;
1919
describe('minify', function() {
2020
var testContentsInput =
2121
'"use strict"; (function(console, first, second) { console.log(first + second) }(5, 10))';
22-
var testContentsExpected = uglifyjs.minify(testContentsInput, {
23-
fromString: true
24-
}).code;
22+
var testContentsExpected = uglifyjs.minify(testContentsInput).code;
2523

2624
beforeEach(function() {
2725
this.log = td.replace('../lib/log');

test/no-compress.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ describe('no-compress', function() {
1818
var testContentsInput =
1919
'"use strict"; (function(console, first, second) {\n\tconsole.log(first + second)\n}(5, 10))';
2020
var testContentsExpected = uglifyjs.minify(testContentsInput, {
21-
fromString: true,
2221
compress: false
2322
}).code;
2423

test/sourcemap.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@ var beforeEach = mocha.beforeEach;
1919
describe('source maps', function() {
2020
var testContents1Input =
2121
'(function(first, second) {\n console.log(first + second);\n}(5, 10));\n';
22-
var testContents1Expected = uglifyjs.minify(testContents1Input, {
23-
fromString: true
24-
}).code;
22+
var testContents1Expected = uglifyjs.minify(testContents1Input).code;
2523
var testContents2Input = '(function(alert) {\n alert(5);\n}(alert));\n';
26-
var testContents2Expected = uglifyjs.minify(testContents2Input, {
27-
fromString: true
28-
}).code;
24+
var testContents2Expected = uglifyjs.minify(testContents2Input).code;
2925
var testConcatExpected = uglifyjs.minify(
30-
testContents1Input + '\n' + testContents2Input,
31-
{fromString: true}
26+
testContents1Input + '\n' + testContents2Input
3227
).code;
3328

3429
beforeEach(function() {
@@ -222,9 +217,7 @@ describe('source maps', function() {
222217
it('should avoid "ghost" files in sourcemaps', function(done) {
223218
var testBabelInput =
224219
'"use strict";\n\n(function (first, second) {\n console.log(first + second);\n})(5, 10);';
225-
var testBabelExpected = uglifyjs.minify(testBabelInput, {
226-
fromString: true
227-
}).code;
220+
var testBabelExpected = uglifyjs.minify(testBabelInput).code;
228221

229222
var testFile = new Vinyl({
230223
cwd: '/home/terin/broken-promises/',

0 commit comments

Comments
 (0)