Skip to content

Commit 10d5d32

Browse files
JemarJoneshulkish
authored andcommitted
Updated readme to reflect last compatible version of UglifyJS2#harmony (webpack-contrib#40)
1 parent 6be1cf2 commit 10d5d32

File tree

3 files changed

+120
-88
lines changed

3 files changed

+120
-88
lines changed

.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"node": true,
77
"jasmine": true
88
},
9+
"parser": "babel-eslint",
10+
"parserOptions": {
11+
"sourceType": "module",
12+
},
913
"rules": {
1014
"quotes": ["error", "double"],
1115
"no-undef": "error",
@@ -41,6 +45,7 @@
4145
"try": {"after": true},
4246
"else": {"after": true},
4347
"throw": {"after": true},
48+
"const": {"after": true},
4449
"case": {"after": true},
4550
"return": {"after": true},
4651
"finally": {"after": true},

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ npm install uglifyjs-webpack-plugin --save-dev
3636
If your minification target is ES6:
3737

3838
```bash
39-
yarn add git://github.com/mishoo/UglifyJS2#harmony --dev
39+
yarn add git://github.com/mishoo/UglifyJS2#harmony-v2.8.22 --dev
4040
```
4141

4242
If your minification target is ES5:

src/index.js

Lines changed: 114 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
/*
2-
MIT License http://www.opensource.org/licenses/mit-license.php
3-
Author Tobias Koppers @sokra
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
44
*/
5-
"use strict";
5+
/* eslint-disable no-cond-assign, no-plusplus, no-continue, no-param-reassign, no-underscore-dangle, no-loop-func */
66

77
const SourceMapConsumer = require("source-map").SourceMapConsumer;
8-
const SourceMapSource = require("webpack-sources").SourceMapSource;
9-
const RawSource = require("webpack-sources").RawSource;
108
const ConcatSource = require("webpack-sources").ConcatSource;
9+
const RawSource = require("webpack-sources").RawSource;
10+
const SourceMapSource = require("webpack-sources").SourceMapSource;
1111
const RequestShortener = require("webpack/lib/RequestShortener");
1212
const ModuleFilenameHelpers = require("webpack/lib/ModuleFilenameHelpers");
1313
const uglify = require("uglify-js");
1414

15+
const WARN_PATTERN = /\[.+:([0-9]+),([0-9]+)\]/;
16+
17+
function ensureConditionFunc(condition, key) {
18+
switch(typeof condition[key]) {
19+
case "boolean": {
20+
const b = condition[key];
21+
condition[key] = () => b;
22+
break;
23+
}
24+
case "function":
25+
break;
26+
case "string": {
27+
if(condition[key] === "all") {
28+
condition[key] = () => true;
29+
break;
30+
}
31+
const regex = new RegExp(condition[key]);
32+
condition[key] = (astNode, comment) => regex.test(comment.value);
33+
break;
34+
}
35+
default: {
36+
const defaultRegex = condition[key];
37+
condition[key] = (astNode, comment) => defaultRegex.test(comment.value);
38+
}
39+
}
40+
}
41+
1542
class UglifyJsPlugin {
1643
constructor(options) {
17-
if(typeof options !== "object" || Array.isArray(options)) options = {};
18-
if(typeof options.compressor !== "undefined") options.compress = options.compressor;
44+
if(typeof options !== "object" || Array.isArray(options)) {
45+
options = {};
46+
}
47+
if(typeof options.compressor !== "undefined") {
48+
options.compress = options.compressor;
49+
}
50+
1951
this.options = options;
2052
}
2153

@@ -28,27 +60,38 @@ class UglifyJsPlugin {
2860
compiler.plugin("compilation", (compilation) => {
2961
if(options.sourceMap) {
3062
compilation.plugin("build-module", (module) => {
31-
// to get detailed location info about errors
63+
// to get detailed location info about errors
3264
module.useSourceMap = true;
3365
});
3466
}
3567
compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
36-
const files = [];
37-
chunks.forEach((chunk) => files.push.apply(files, chunk.files));
38-
files.push.apply(files, compilation.additionalChunkAssets);
39-
const filteredFiles = files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options));
40-
filteredFiles.forEach((file) => {
41-
const oldWarnFunction = uglify.AST_Node.warn_function;
42-
const warnings = [];
43-
let sourceMap;
68+
const files = compilation.additionalChunkAssets;
69+
let i = chunks.length - 1;
70+
while(i--) {
71+
// eslint-disable-next-line prefer-spread
72+
files.push.apply(files, chunks[i].files);
73+
}
74+
75+
let file;
76+
let sourceMap;
77+
let input;
78+
let inputSourceMap;
79+
let map;
80+
let oldWarnFunction;
81+
const warnings = [];
82+
i = files.length;
83+
84+
while(i--) {
85+
file = files[i];
86+
if(!ModuleFilenameHelpers.matchObject(options, file)) continue;
87+
oldWarnFunction = uglify.AST_Node.warn_function;
88+
4489
try {
4590
const asset = compilation.assets[file];
4691
if(asset.__UglifyJsPlugin) {
4792
compilation.assets[file] = asset.__UglifyJsPlugin;
4893
return;
4994
}
50-
let input;
51-
let inputSourceMap;
5295
if(options.sourceMap) {
5396
if(asset.sourceAndMap) {
5497
const sourceAndMap = asset.sourceAndMap();
@@ -60,17 +103,17 @@ class UglifyJsPlugin {
60103
}
61104
sourceMap = new SourceMapConsumer(inputSourceMap);
62105
uglify.AST_Node.warn_function = (warning) => { // eslint-disable-line camelcase
63-
const match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning);
106+
const match = WARN_PATTERN.exec(warning);
64107
const line = +match[1];
65108
const column = +match[2];
66109
const original = sourceMap.originalPositionFor({
67-
line: line,
68-
column: column
110+
line,
111+
column,
69112
});
70113
if(!original || !original.source || original.source === file) return;
71114
if(!warningsFilter(original.source)) return;
72-
warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") +
73-
"[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]");
115+
warnings.push(`${warning.replace(WARN_PATTERN, "")
116+
}[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`);
74117
};
75118
} else {
76119
input = asset.source();
@@ -80,12 +123,12 @@ class UglifyJsPlugin {
80123
}
81124
uglify.base54.reset();
82125
let ast = uglify.parse(input, {
83-
filename: file
126+
filename: file,
84127
});
85128
if(options.compress !== false) {
86129
ast.figure_out_scope();
87130
const compress = uglify.Compressor(options.compress || {
88-
warnings: false
131+
warnings: false,
89132
}); // eslint-disable-line new-cap
90133
ast = compress.compress(ast);
91134
}
@@ -97,136 +140,120 @@ class UglifyJsPlugin {
97140
uglify.mangle_properties(ast, options.mangle.props);
98141
}
99142
}
100-
const output = {};
101-
output.comments = Object.prototype.hasOwnProperty.call(options, "comments") ? options.comments : /^\**!|@preserve|@license/;
143+
const output = { ...options.output };
144+
output.comments = options.comments || /^\**!|@preserve|@license/;
102145
output.beautify = options.beautify;
103-
for(let k in options.output) {
104-
output[k] = options.output[k];
105-
}
146+
// for (const k in options.output) { // eslint-disable-line guard-for-in
147+
// output[k] = options.output[k];
148+
// }
106149
const extractedComments = [];
107150
if(options.extractComments) {
108151
const condition = {};
109152
if(typeof options.extractComments === "string" || options.extractComments instanceof RegExp) {
110-
// extractComments specifies the extract condition and output.comments specifies the preserve condition
153+
// extractComments specifies the extract condition and output.comments specifies the preserve condition
111154
condition.preserve = output.comments;
112155
condition.extract = options.extractComments;
113156
} else if(Object.prototype.hasOwnProperty.call(options.extractComments, "condition")) {
114-
// Extract condition is given in extractComments.condition
157+
// Extract condition is given in extractComments.condition
115158
condition.preserve = output.comments;
116159
condition.extract = options.extractComments.condition;
117160
} else {
118-
// No extract condition is given. Extract comments that match output.comments instead of preserving them
161+
// No extract condition is given. Extract comments that match output.comments instead of preserving them
119162
condition.preserve = false;
120163
condition.extract = output.comments;
121164
}
122165

123-
// Ensure that both conditions are functions
124-
["preserve", "extract"].forEach(key => {
125-
switch(typeof condition[key]) {
126-
case "boolean":
127-
var b = condition[key];
128-
condition[key] = () => b;
129-
break;
130-
case "function":
131-
break;
132-
case "string":
133-
if(condition[key] === "all") {
134-
condition[key] = () => true;
135-
break;
136-
}
137-
var regex = new RegExp(condition[key]);
138-
condition[key] = (astNode, comment) => regex.test(comment.value);
139-
break;
140-
default:
141-
regex = condition[key];
142-
condition[key] = (astNode, comment) => regex.test(comment.value);
143-
}
144-
});
166+
// Ensure that both conditions are functions
167+
ensureConditionFunc(condition, "preserve");
168+
ensureConditionFunc(condition, "extract");
145169

146-
// Redefine the comments function to extract and preserve
147-
// comments according to the two conditions
170+
// Redefine the comments function to extract and preserve
171+
// comments according to the two conditions
148172
output.comments = (astNode, comment) => {
149173
if(condition.extract(astNode, comment)) {
150174
extractedComments.push(
151-
comment.type === "comment2" ? "/*" + comment.value + "*/" : "//" + comment.value
152-
);
175+
comment.type === "comment2" ? `/*${comment.value}*/` : `//${comment.value}`
176+
);
153177
}
154178
return condition.preserve(astNode, comment);
155179
};
156180
}
157-
let map;
181+
map = null;
158182
if(options.sourceMap) {
159183
map = uglify.SourceMap({ // eslint-disable-line new-cap
160-
file: file,
161-
root: ""
184+
file,
185+
root: "",
162186
});
163187
output.source_map = map; // eslint-disable-line camelcase
164188
}
165189
const stream = uglify.OutputStream(output); // eslint-disable-line new-cap
166190
ast.print(stream);
167-
if(map) map = map + "";
168-
const stringifiedStream = stream + "";
169-
let outputSource = (map ?
170-
new SourceMapSource(stringifiedStream, file, JSON.parse(map), input, inputSourceMap) :
171-
new RawSource(stringifiedStream));
191+
if(map) map += "";
192+
const stringifiedStream = `${stream}`;
193+
let outputSource = map ? new SourceMapSource(
194+
stringifiedStream, file, JSON.parse(map), input, inputSourceMap // eslint-disable-line comma-dangle
195+
) : new RawSource(stringifiedStream);
172196
if(extractedComments.length > 0) {
173-
let commentsFile = options.extractComments.filename || file + ".LICENSE";
197+
let commentsFile = options.extractComments.filename || `${file}.LICENSE`;
174198
if(typeof commentsFile === "function") {
175199
commentsFile = commentsFile(file);
176200
}
177201

178-
// Write extracted comments to commentsFile
179-
const commentsSource = new RawSource(extractedComments.join("\n\n") + "\n");
202+
// Write extracted comments to commentsFile
203+
const commentsSource = new RawSource(`${extractedComments.join("\n\n")}\n`);
180204
if(commentsFile in compilation.assets) {
181-
// commentsFile already exists, append new comments...
205+
// commentsFile already exists, append new comments...
182206
if(compilation.assets[commentsFile] instanceof ConcatSource) {
183207
compilation.assets[commentsFile].add("\n");
184208
compilation.assets[commentsFile].add(commentsSource);
185209
} else {
186210
compilation.assets[commentsFile] = new ConcatSource(
187-
compilation.assets[commentsFile], "\n", commentsSource
188-
);
211+
compilation.assets[commentsFile], "\n", commentsSource
212+
);
189213
}
190214
} else {
191215
compilation.assets[commentsFile] = commentsSource;
192216
}
193217

194-
// Add a banner to the original file
218+
// Add a banner to the original file
195219
if(options.extractComments.banner !== false) {
196-
let banner = options.extractComments.banner || "For license information please see " + commentsFile;
220+
let banner = options.extractComments.banner || `For license information please see ${commentsFile}`;
197221
if(typeof banner === "function") {
198222
banner = banner(commentsFile);
199223
}
200224
if(banner) {
201225
outputSource = new ConcatSource(
202-
"/*! " + banner + " */\n", outputSource
203-
);
226+
`/*! ${banner} */\n`, outputSource
227+
);
204228
}
205229
}
206230
}
207-
asset.__UglifyJsPlugin = compilation.assets[file] = outputSource;
208-
if(warnings.length > 0) {
209-
compilation.warnings.push(new Error(file + " from UglifyJs\n" + warnings.join("\n")));
231+
compilation.assets[file] = outputSource;
232+
asset.__UglifyJsPlugin = outputSource;
233+
234+
if(warnings.length) {
235+
compilation.warnings.push(new Error(`${file} from UglifyJs\n${warnings.join("\n")}`));
210236
}
211237
} catch(err) {
212238
if(err.line) {
213239
const original = sourceMap && sourceMap.originalPositionFor({
214240
line: err.line,
215-
column: err.col
241+
column: err.col,
216242
});
217243
if(original && original.source) {
218-
compilation.errors.push(new Error(file + " from UglifyJs\n" + err.message + " [" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "][" + file + ":" + err.line + "," + err.col + "]"));
244+
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.message} [${requestShortener.shorten(original.source)}:${original.line},${original.column}][${file}:${err.line},${err.col}]`));
219245
} else {
220-
compilation.errors.push(new Error(file + " from UglifyJs\n" + err.message + " [" + file + ":" + err.line + "," + err.col + "]"));
246+
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.message} [${file}:${err.line},${err.col}]`));
221247
}
222248
} else if(err.msg) {
223-
compilation.errors.push(new Error(file + " from UglifyJs\n" + err.msg));
224-
} else
225-
compilation.errors.push(new Error(file + " from UglifyJs\n" + err.stack));
249+
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.msg}`));
250+
} else {
251+
compilation.errors.push(new Error(`${file} from UglifyJs\n${err.stack}`));
252+
}
226253
} finally {
227254
uglify.AST_Node.warn_function = oldWarnFunction; // eslint-disable-line camelcase
228255
}
229-
});
256+
}
230257
callback();
231258
});
232259
});

0 commit comments

Comments
 (0)