Skip to content

Commit 6779ddb

Browse files
committed
allow to disable SourceMap in UglifyJsPlugin
1 parent e400afb commit 6779ddb

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

lib/optimize/UglifyJsPlugin.js

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
var SourceMapConsumer = require("webpack-core/lib/source-map").SourceMapConsumer;
66
var SourceMapSource = require("webpack-core/lib/SourceMapSource");
7+
var RawSource = require("webpack-core/lib/RawSource");
78
var RequestShortener = require("../RequestShortener");
89
var uglify = require("uglify-js");
910

@@ -23,10 +24,12 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
2324

2425
var requestShortener = new RequestShortener(compiler.context);
2526
compiler.plugin("compilation", function(compilation) {
26-
compilation.plugin("build-module", function(module) {
27-
// to get detailed location info about errors
28-
module.useSourceMap = true;
29-
});
27+
if(options.sourceMap !== false) {
28+
compilation.plugin("build-module", function(module) {
29+
// to get detailed location info about errors
30+
module.useSourceMap = true;
31+
});
32+
}
3033
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
3134
var files = [];
3235
chunks.forEach(function(chunk) {
@@ -48,56 +51,65 @@ UglifyJsPlugin.prototype.apply = function(compiler) {
4851
compilation.assets[file] = asset.__UglifyJsPlugin;
4952
return;
5053
}
51-
var inputSourceMap = asset.map();
52-
var sourceMap = new SourceMapConsumer(inputSourceMap);
53-
uglify.AST_Node.warn_function = function(warning) {
54-
var match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning);
55-
var line = +match[1];
56-
var column = +match[2];
57-
var original = sourceMap.originalPositionFor({
58-
line: line,
59-
column: column
60-
});
61-
if(!original || !original.source || original.source === file) return;
62-
warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") +
63-
"[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]");
64-
};
54+
if(options.sourceMap !== false) {
55+
var inputSourceMap = asset.map();
56+
var sourceMap = new SourceMapConsumer(inputSourceMap);
57+
uglify.AST_Node.warn_function = function(warning) {
58+
var match = /\[.+:([0-9]+),([0-9]+)\]/.exec(warning);
59+
var line = +match[1];
60+
var column = +match[2];
61+
var original = sourceMap.originalPositionFor({
62+
line: line,
63+
column: column
64+
});
65+
if(!original || !original.source || original.source === file) return;
66+
warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") +
67+
"[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]");
68+
};
69+
} else {
70+
uglify.AST_Node.warn_function = function(warning) {
71+
warnings.push(warning);
72+
};
73+
}
6574
var ast = uglify.parse(input, {
6675
filename: file
6776
});
68-
ast.figure_out_scope()
6977
if(options.compress !== false) {
78+
ast.figure_out_scope()
7079
var compress = uglify.Compressor(options.compress);
7180
ast = ast.transform(compress);
81+
}
82+
if(options.mangle !== false) {
7283
ast.figure_out_scope();
73-
if(options.mangle !== false) {
74-
ast.compute_char_frequency(options.mangle || {});
75-
ast.mangle_names(options.mangle || {});
76-
}
84+
ast.compute_char_frequency(options.mangle || {});
85+
ast.mangle_names(options.mangle || {});
7786
}
78-
var map = null;
79-
map = uglify.SourceMap({
80-
file: file,
81-
root: ""
82-
});
8387
var output = {};
8488
output.comments = options.comments || /^\**!|@preserve|@license/;
8589
output.beautify = options.beautify;
8690
for(var k in options.output) {
8791
output[k] = options.output[k];
8892
}
89-
output.source_map = map;
93+
if(options.sourceMap !== false) {
94+
var map = uglify.SourceMap({
95+
file: file,
96+
root: ""
97+
});
98+
output.source_map = map;
99+
}
90100
var stream = uglify.OutputStream(output);
91101
ast.print(stream);
92-
map = map + "";
102+
if(map) map = map + "";
93103
stream = stream + "";
94-
asset.__UglifyJsPlugin = compilation.assets[file] = new SourceMapSource(stream, file, map, input, inputSourceMap);
104+
asset.__UglifyJsPlugin = compilation.assets[file] = (map ?
105+
new SourceMapSource(stream, file, map, input, inputSourceMap) :
106+
new RawSource(stream));
95107
if(warnings.length > 0) {
96108
compilation.warnings.push(new Error(file + " from UglifyJs\n" + warnings.join("\n")));
97109
}
98110
} catch(err) {
99111
if(err.line) {
100-
var original = sourceMap.originalPositionFor({
112+
var original = sourceMap && sourceMap.originalPositionFor({
101113
line: err.line,
102114
column: err.col
103115
});

test/TestCases.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ describe("TestCases", function() {
2727
{ name: "devtool-eval-source-map", devtool: "#eval-source-map" },
2828
{ name: "devtool-source-map", devtool: "#@source-map" },
2929
{ name: "minimized", plugins: [
30+
new webpack.optimize.UglifyJsPlugin({
31+
sourceMap: false
32+
})
33+
]},
34+
{ name: "minimized-source-map", plugins: [
3035
new webpack.optimize.UglifyJsPlugin()
3136
]},
3237
{ name: "deduped", plugins: [

0 commit comments

Comments
 (0)