|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var SourceMapConsumer = require("source-map").SourceMapConsumer; |
6 | | -var SourceMapSource = require("webpack-sources").SourceMapSource; |
7 | | -var RawSource = require("webpack-sources").RawSource; |
8 | | -var RequestShortener = require("../RequestShortener"); |
9 | | -var ModuleFilenameHelpers = require("../ModuleFilenameHelpers"); |
10 | | -var uglify = require("uglify-js"); |
| 5 | +"use strict"; |
11 | 6 |
|
12 | | -function UglifyJsPlugin(options) { |
13 | | - if(typeof options !== "object") options = {}; |
14 | | - if(typeof options.compressor !== "undefined") { |
15 | | - options.compress = options.compressor; |
| 7 | +const SourceMapConsumer = require("source-map").SourceMapConsumer; |
| 8 | +const SourceMapSource = require("webpack-sources").SourceMapSource; |
| 9 | +const RawSource = require("webpack-sources").RawSource; |
| 10 | +const RequestShortener = require("../RequestShortener"); |
| 11 | +const ModuleFilenameHelpers = require("../ModuleFilenameHelpers"); |
| 12 | +const uglify = require("uglify-js"); |
| 13 | + |
| 14 | +class UglifyJsPlugin { |
| 15 | + constructor(options) { |
| 16 | + if(typeof options !== "object" || Array.isArray(options)) options = {}; |
| 17 | + if(typeof options.compressor !== "undefined") options.compress = options.compressor; |
| 18 | + this.options = options; |
16 | 19 | } |
17 | | - this.options = options; |
18 | | -} |
19 | | -module.exports = UglifyJsPlugin; |
20 | 20 |
|
21 | | -UglifyJsPlugin.prototype.apply = function(compiler) { |
22 | | - var options = this.options; |
23 | | - options.test = options.test || /\.js($|\?)/i; |
| 21 | + apply(compiler) { |
| 22 | + let options = this.options; |
| 23 | + options.test = options.test || /\.js($|\?)/i; |
24 | 24 |
|
25 | | - var requestShortener = new RequestShortener(compiler.context); |
26 | | - compiler.plugin("compilation", function(compilation) { |
27 | | - if(options.sourceMap) { |
28 | | - compilation.plugin("build-module", function(module) { |
29 | | - // to get detailed location info about errors |
30 | | - module.useSourceMap = true; |
31 | | - }); |
32 | | - } |
33 | | - compilation.plugin("optimize-chunk-assets", function(chunks, callback) { |
34 | | - var files = []; |
35 | | - chunks.forEach(function(chunk) { |
36 | | - chunk.files.forEach(function(file) { |
37 | | - files.push(file); |
| 25 | + let requestShortener = new RequestShortener(compiler.context); |
| 26 | + compiler.plugin("compilation", (compilation) => { |
| 27 | + if(options.sourceMap) { |
| 28 | + compilation.plugin("build-module", (module) => { |
| 29 | + // to get detailed location info about errors |
| 30 | + module.useSourceMap = true; |
38 | 31 | }); |
39 | | - }); |
40 | | - compilation.additionalChunkAssets.forEach(function(file) { |
41 | | - files.push(file); |
42 | | - }); |
43 | | - files = files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)); |
44 | | - files.forEach(function(file) { |
45 | | - var oldWarnFunction = uglify.AST_Node.warn_function; |
46 | | - var warnings = []; |
47 | | - try { |
48 | | - var asset = compilation.assets[file]; |
49 | | - if(asset.__UglifyJsPlugin) { |
50 | | - compilation.assets[file] = asset.__UglifyJsPlugin; |
51 | | - return; |
52 | | - } |
53 | | - var input; |
54 | | - if(options.sourceMap) { |
55 | | - var inputSourceMap; |
56 | | - if(asset.sourceAndMap) { |
57 | | - var sourceAndMap = asset.sourceAndMap(); |
58 | | - inputSourceMap = sourceAndMap.map; |
59 | | - input = sourceAndMap.source; |
| 32 | + } |
| 33 | + compilation.plugin("optimize-chunk-assets", (chunks, callback) => { |
| 34 | + let files = []; |
| 35 | + chunks.forEach((chunk) => files.push.apply(files, chunk.files)); |
| 36 | + files.push.apply(files, compilation.additionalChunkAssets); |
| 37 | + files = files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)); |
| 38 | + files.forEach((file) => { |
| 39 | + let oldWarnFunction = uglify.AST_Node.warn_function; |
| 40 | + let warnings = []; |
| 41 | + let sourceMap; |
| 42 | + try { |
| 43 | + let asset = compilation.assets[file]; |
| 44 | + if(asset.__UglifyJsPlugin) { |
| 45 | + compilation.assets[file] = asset.__UglifyJsPlugin; |
| 46 | + return; |
| 47 | + } |
| 48 | + let input; |
| 49 | + let inputSourceMap; |
| 50 | + if(options.sourceMap) { |
| 51 | + if(asset.sourceAndMap) { |
| 52 | + let sourceAndMap = asset.sourceAndMap(); |
| 53 | + inputSourceMap = sourceAndMap.map; |
| 54 | + input = sourceAndMap.source; |
| 55 | + } else { |
| 56 | + inputSourceMap = asset.map(); |
| 57 | + input = asset.source(); |
| 58 | + } |
| 59 | + sourceMap = new SourceMapConsumer(inputSourceMap); |
| 60 | + uglify.AST_Node.warn_function = (warning) => { // eslint-disable-line camelcase |
| 61 | + let match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning); |
| 62 | + let line = +match[1]; |
| 63 | + let column = +match[2]; |
| 64 | + let original = sourceMap.originalPositionFor({ |
| 65 | + line: line, |
| 66 | + column: column |
| 67 | + }); |
| 68 | + if(!original || !original.source || original.source === file) return; |
| 69 | + warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") + |
| 70 | + "[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]"); |
| 71 | + }; |
60 | 72 | } else { |
61 | | - inputSourceMap = asset.map(); |
62 | 73 | input = asset.source(); |
| 74 | + uglify.AST_Node.warn_function = (warning) => { // eslint-disable-line camelcase |
| 75 | + warnings.push(warning); |
| 76 | + }; |
| 77 | + } |
| 78 | + uglify.base54.reset(); |
| 79 | + let ast = uglify.parse(input, { |
| 80 | + filename: file |
| 81 | + }); |
| 82 | + if(options.compress !== false) { |
| 83 | + ast.figure_out_scope(); |
| 84 | + let compress = uglify.Compressor(options.compress || { |
| 85 | + warnings: false |
| 86 | + }); // eslint-disable-line new-cap |
| 87 | + ast = ast.transform(compress); |
| 88 | + } |
| 89 | + if(options.mangle !== false) { |
| 90 | + ast.figure_out_scope(options.mangle || {}); |
| 91 | + ast.compute_char_frequency(options.mangle || {}); |
| 92 | + ast.mangle_names(options.mangle || {}); |
| 93 | + if(options.mangle && options.mangle.props) { |
| 94 | + uglify.mangle_properties(ast, options.mangle.props); |
| 95 | + } |
63 | 96 | } |
64 | | - var sourceMap = new SourceMapConsumer(inputSourceMap); |
65 | | - uglify.AST_Node.warn_function = function(warning) { // eslint-disable-line camelcase |
66 | | - var match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning); |
67 | | - var line = +match[1]; |
68 | | - var column = +match[2]; |
69 | | - var original = sourceMap.originalPositionFor({ |
70 | | - line: line, |
71 | | - column: column |
| 97 | + let output = {}; |
| 98 | + output.comments = Object.prototype.hasOwnProperty.call(options, "comments") ? options.comments : /^\**!|@preserve|@license/; |
| 99 | + output.beautify = options.beautify; |
| 100 | + for(let k in options.output) { |
| 101 | + output[k] = options.output[k]; |
| 102 | + } |
| 103 | + let map; |
| 104 | + if(options.sourceMap) { |
| 105 | + map = uglify.SourceMap({ // eslint-disable-line new-cap |
| 106 | + file: file, |
| 107 | + root: "" |
72 | 108 | }); |
73 | | - if(!original || !original.source || original.source === file) return; |
74 | | - warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") + |
75 | | - "[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]"); |
76 | | - }; |
77 | | - } else { |
78 | | - input = asset.source(); |
79 | | - uglify.AST_Node.warn_function = function(warning) { // eslint-disable-line camelcase |
80 | | - warnings.push(warning); |
81 | | - }; |
82 | | - } |
83 | | - uglify.base54.reset(); |
84 | | - var ast = uglify.parse(input, { |
85 | | - filename: file |
86 | | - }); |
87 | | - if(options.compress !== false) { |
88 | | - ast.figure_out_scope(); |
89 | | - var compress = uglify.Compressor(options.compress || { |
90 | | - warnings: false |
91 | | - }); // eslint-disable-line new-cap |
92 | | - ast = ast.transform(compress); |
93 | | - } |
94 | | - if(options.mangle !== false) { |
95 | | - ast.figure_out_scope(options.mangle || {}); |
96 | | - ast.compute_char_frequency(options.mangle || {}); |
97 | | - ast.mangle_names(options.mangle || {}); |
98 | | - if(options.mangle && options.mangle.props) { |
99 | | - uglify.mangle_properties(ast, options.mangle.props); |
| 109 | + output.source_map = map; // eslint-disable-line camelcase |
100 | 110 | } |
101 | | - } |
102 | | - var output = {}; |
103 | | - output.comments = Object.prototype.hasOwnProperty.call(options, "comments") ? options.comments : /^\**!|@preserve|@license/; |
104 | | - output.beautify = options.beautify; |
105 | | - for(var k in options.output) { |
106 | | - output[k] = options.output[k]; |
107 | | - } |
108 | | - if(options.sourceMap) { |
109 | | - var map = uglify.SourceMap({ // eslint-disable-line new-cap |
110 | | - file: file, |
111 | | - root: "" |
112 | | - }); |
113 | | - output.source_map = map; // eslint-disable-line camelcase |
114 | | - } |
115 | | - var stream = uglify.OutputStream(output); // eslint-disable-line new-cap |
116 | | - ast.print(stream); |
117 | | - if(map) map = map + ""; |
118 | | - stream = stream + ""; |
119 | | - asset.__UglifyJsPlugin = compilation.assets[file] = (map ? |
120 | | - new SourceMapSource(stream, file, JSON.parse(map), input, inputSourceMap) : |
121 | | - new RawSource(stream)); |
122 | | - if(warnings.length > 0) { |
123 | | - compilation.warnings.push(new Error(file + " from UglifyJs\n" + warnings.join("\n"))); |
124 | | - } |
125 | | - } catch(err) { |
126 | | - if(err.line) { |
127 | | - var original = sourceMap && sourceMap.originalPositionFor({ |
128 | | - line: err.line, |
129 | | - column: err.col |
130 | | - }); |
131 | | - if(original && original.source) { |
132 | | - compilation.errors.push(new Error(file + " from UglifyJs\n" + err.message + " [" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "][" + file + ":" + err.line + "," + err.col + "]")); |
133 | | - } else { |
134 | | - compilation.errors.push(new Error(file + " from UglifyJs\n" + err.message + " [" + file + ":" + err.line + "," + err.col + "]")); |
| 111 | + let stream = uglify.OutputStream(output); // eslint-disable-line new-cap |
| 112 | + ast.print(stream); |
| 113 | + if(map) map = map + ""; |
| 114 | + stream = stream + ""; |
| 115 | + asset.__UglifyJsPlugin = compilation.assets[file] = (map ? |
| 116 | + new SourceMapSource(stream, file, JSON.parse(map), input, inputSourceMap) : |
| 117 | + new RawSource(stream)); |
| 118 | + if(warnings.length > 0) { |
| 119 | + compilation.warnings.push(new Error(file + " from UglifyJs\n" + warnings.join("\n"))); |
135 | 120 | } |
136 | | - } else if(err.msg) { |
137 | | - compilation.errors.push(new Error(file + " from UglifyJs\n" + err.msg)); |
138 | | - } else |
139 | | - compilation.errors.push(new Error(file + " from UglifyJs\n" + err.stack)); |
140 | | - } finally { |
141 | | - uglify.AST_Node.warn_function = oldWarnFunction; // eslint-disable-line camelcase |
142 | | - } |
| 121 | + } catch(err) { |
| 122 | + if(err.line) { |
| 123 | + let original = sourceMap && sourceMap.originalPositionFor({ |
| 124 | + line: err.line, |
| 125 | + column: err.col |
| 126 | + }); |
| 127 | + if(original && original.source) { |
| 128 | + compilation.errors.push(new Error(file + " from UglifyJs\n" + err.message + " [" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "][" + file + ":" + err.line + "," + err.col + "]")); |
| 129 | + } else { |
| 130 | + compilation.errors.push(new Error(file + " from UglifyJs\n" + err.message + " [" + file + ":" + err.line + "," + err.col + "]")); |
| 131 | + } |
| 132 | + } else if(err.msg) { |
| 133 | + compilation.errors.push(new Error(file + " from UglifyJs\n" + err.msg)); |
| 134 | + } else |
| 135 | + compilation.errors.push(new Error(file + " from UglifyJs\n" + err.stack)); |
| 136 | + } finally { |
| 137 | + uglify.AST_Node.warn_function = oldWarnFunction; // eslint-disable-line camelcase |
| 138 | + } |
| 139 | + }); |
| 140 | + callback(); |
143 | 141 | }); |
144 | | - callback(); |
145 | 142 | }); |
146 | | - }); |
147 | | -}; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +module.exports = UglifyJsPlugin; |
0 commit comments