2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2013-09-14 17:50:39 +08:00
|
|
|
var SourceMapConsumer = require("webpack-core/lib/source-map").SourceMapConsumer;
|
2013-03-26 23:54:41 +08:00
|
|
|
var SourceMapSource = require("webpack-core/lib/SourceMapSource");
|
2013-09-14 17:50:39 +08:00
|
|
|
var RequestShortener = require("../RequestShortener");
|
2013-01-31 01:49:25 +08:00
|
|
|
var uglify = require("uglify-js");
|
|
|
|
|
2013-09-14 17:50:39 +08:00
|
|
|
function UglifyJsPlugin(context, options) {
|
|
|
|
if(typeof options !== "object") options = {};
|
|
|
|
if(typeof options.compressor !== "undefined") {
|
|
|
|
options.compress = options.compressor;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
this.options = options;
|
2013-09-14 17:50:39 +08:00
|
|
|
this.requestShortener = new RequestShortener(context);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
module.exports = UglifyJsPlugin;
|
|
|
|
UglifyJsPlugin.prototype.apply = function(compiler) {
|
|
|
|
var options = this.options;
|
2013-09-14 17:50:39 +08:00
|
|
|
var requestShortener = this.requestShortener;
|
2013-01-31 01:49:25 +08:00
|
|
|
compiler.plugin("compilation", function(compilation) {
|
2013-10-31 07:06:39 +08:00
|
|
|
compilation.plugin("build-module", function(module) {
|
|
|
|
// to get detailed location info about errors
|
|
|
|
module.useSourceMap = true;
|
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
|
|
|
|
var files = [];
|
|
|
|
chunks.forEach(function(chunk) {
|
|
|
|
chunk.files.forEach(function(file) {
|
|
|
|
files.push(file);
|
|
|
|
});
|
|
|
|
});
|
2013-07-01 19:59:02 +08:00
|
|
|
compilation.additionalChunkAssets.forEach(function(file) {
|
|
|
|
files.push(file);
|
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
files.forEach(function(file) {
|
2013-09-14 17:50:39 +08:00
|
|
|
var oldWarnFunction = uglify.AST_Node.warn_function;
|
|
|
|
var warnings = [];
|
2013-01-31 01:49:25 +08:00
|
|
|
try {
|
2013-07-04 17:55:37 +08:00
|
|
|
var asset = compilation.assets[file];
|
|
|
|
var input = asset.source();
|
|
|
|
if(asset.__UglifyJsPlugin) {
|
|
|
|
compilation.assets[file] = asset.__UglifyJsPlugin;
|
|
|
|
return;
|
|
|
|
}
|
2013-03-26 23:54:41 +08:00
|
|
|
var inputSourceMap = compilation.assets[file].map();
|
2013-10-31 07:06:39 +08:00
|
|
|
var sourceMap = new SourceMapConsumer(inputSourceMap);
|
2013-09-14 17:50:39 +08:00
|
|
|
uglify.AST_Node.warn_function = function(warning) {
|
|
|
|
var match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning);
|
2013-10-31 07:06:39 +08:00
|
|
|
var line = +match[1];
|
|
|
|
var column = +match[2];
|
2013-09-14 17:50:39 +08:00
|
|
|
var original = sourceMap.originalPositionFor({
|
|
|
|
line: line,
|
|
|
|
column: column
|
|
|
|
});
|
|
|
|
if(!original || !original.source || original.source === file) return;
|
|
|
|
warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") +
|
|
|
|
"[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]");
|
|
|
|
};
|
2013-01-31 01:49:25 +08:00
|
|
|
var ast = uglify.parse(input, {
|
|
|
|
filename: file
|
|
|
|
});
|
|
|
|
ast.figure_out_scope()
|
2013-09-14 17:50:39 +08:00
|
|
|
if(options.compress !== false) {
|
|
|
|
var compress = uglify.Compressor(options.compress);
|
|
|
|
ast = ast.transform(compress);
|
2013-01-31 01:49:25 +08:00
|
|
|
ast.figure_out_scope();
|
2013-07-11 06:16:07 +08:00
|
|
|
if(options.mangle !== false) {
|
|
|
|
ast.compute_char_frequency(options.mangle || {});
|
|
|
|
ast.mangle_names(options.mangle || {});
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
2013-03-26 23:54:41 +08:00
|
|
|
var map = null;
|
|
|
|
map = uglify.SourceMap({
|
|
|
|
file: file,
|
|
|
|
root: ""
|
|
|
|
});
|
|
|
|
var stream = uglify.OutputStream({
|
2013-01-31 01:49:25 +08:00
|
|
|
comments: options.comments || /^\**!|@preserve|@license/,
|
2013-03-26 23:54:41 +08:00
|
|
|
beautify: options.beautify,
|
|
|
|
source_map: map
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
2013-03-26 23:54:41 +08:00
|
|
|
ast.print(stream);
|
|
|
|
map = map + "";
|
|
|
|
stream = stream + "";
|
2013-07-04 17:55:37 +08:00
|
|
|
asset.__UglifyJsPlugin = compilation.assets[file] = new SourceMapSource(stream, file, map, input, inputSourceMap);
|
2013-09-14 17:50:39 +08:00
|
|
|
if(warnings.length > 0) {
|
|
|
|
compilation.warnings.push(new Error(file + " from UglifyJs\n" + warnings.join("\n")));
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
} catch(err) {
|
|
|
|
err.file = file;
|
2013-09-14 17:50:39 +08:00
|
|
|
compilation.errors.push(err);
|
|
|
|
} finally {
|
|
|
|
uglify.AST_Node.warn_function = oldWarnFunction;
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
callback();
|
|
|
|
});
|
2013-05-13 04:44:10 +08:00
|
|
|
compilation.plugin("normal-module-loader", function(context) {
|
|
|
|
context.minimize = true;
|
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
};
|