Skip to content

Commit be4f27c

Browse files
authored
Merge pull request webpack#4200 from timse/allow-to-supress-uglifyjs-warnings
Allow to supress uglifyjs warnings
2 parents b88220c + 6b1872c commit be4f27c

6 files changed

Lines changed: 178 additions & 1 deletion

File tree

lib/optimize/UglifyJsPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class UglifyJsPlugin {
2121
apply(compiler) {
2222
const options = this.options;
2323
options.test = options.test || /\.js($|\?)/i;
24+
const warningsFilter = options.warningsFilter || (() => true);
2425

2526
const requestShortener = new RequestShortener(compiler.context);
2627
compiler.plugin("compilation", (compilation) => {
@@ -66,6 +67,7 @@ class UglifyJsPlugin {
6667
column: column
6768
});
6869
if(!original || !original.source || original.source === file) return;
70+
if(!warningsFilter(original.source)) return;
6971
warnings.push(warning.replace(/\[.+:([0-9]+),([0-9]+)\]/, "") +
7072
"[" + requestShortener.shorten(original.source) + ":" + original.line + "," + original.column + "]");
7173
};

test/UglifyJsPlugin.test.js

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* globals describe, it, beforeEach*/
12
"use strict";
2-
const should = require("should");
3+
require("should");
34
const sinon = require("sinon");
45
const UglifyJsPlugin = require("../lib/optimize/UglifyJsPlugin");
56
const PluginEnvironment = require("./helpers/PluginEnvironment");
@@ -409,6 +410,120 @@ describe("UglifyJsPlugin", function() {
409410
compilation.assets["test3.js"].should.be.instanceof(SourceMapSource);
410411
});
411412
});
413+
414+
describe("with warningsFilter set", function() {
415+
let compilationEventBindings, compilation;
416+
417+
describe("and the filter returns true", function() {
418+
beforeEach(function() {
419+
const pluginEnvironment = new PluginEnvironment();
420+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
421+
compilerEnv.context = "";
422+
423+
const plugin = new UglifyJsPlugin({
424+
warningsFilter: function() {
425+
return true;
426+
},
427+
sourceMap: true,
428+
compress: {
429+
warnings: true,
430+
},
431+
mangle: false,
432+
beautify: true,
433+
comments: false
434+
});
435+
plugin.apply(compilerEnv);
436+
const eventBindings = pluginEnvironment.getEventBindings();
437+
438+
const chunkPluginEnvironment = new PluginEnvironment();
439+
compilation = chunkPluginEnvironment.getEnvironmentStub();
440+
compilation.assets = {
441+
"test2.js": {
442+
source: function() {
443+
return "function foo(x) { if (x) { return bar(); not_called1(); } }";
444+
},
445+
map: function() {
446+
return {
447+
version: 3,
448+
sources: ["test1.js"],
449+
names: ["foo", "x", "bar", "not_called1"],
450+
mappings: "AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC"
451+
};
452+
}
453+
},
454+
};
455+
compilation.errors = [];
456+
compilation.warnings = [];
457+
458+
eventBindings[0].handler(compilation);
459+
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
460+
});
461+
462+
it("should get all warnings", function() {
463+
compilationEventBindings[1].handler([{
464+
files: ["test2.js"]
465+
}], function() {
466+
compilation.warnings.length.should.be.exactly(1);
467+
compilation.warnings[0].should.be.an.Error;
468+
compilation.warnings[0].message.should.containEql("Dropping unreachable code");
469+
});
470+
});
471+
});
472+
473+
describe("and the filter returns false", function() {
474+
beforeEach(function() {
475+
const pluginEnvironment = new PluginEnvironment();
476+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
477+
compilerEnv.context = "";
478+
479+
const plugin = new UglifyJsPlugin({
480+
warningsFilter: function() {
481+
return false;
482+
},
483+
sourceMap: true,
484+
compress: {
485+
warnings: true,
486+
},
487+
mangle: false,
488+
beautify: true,
489+
comments: false
490+
});
491+
plugin.apply(compilerEnv);
492+
const eventBindings = pluginEnvironment.getEventBindings();
493+
494+
const chunkPluginEnvironment = new PluginEnvironment();
495+
compilation = chunkPluginEnvironment.getEnvironmentStub();
496+
compilation.assets = {
497+
"test2.js": {
498+
source: function() {
499+
return "function foo(x) { if (x) { return bar(); not_called1(); } }";
500+
},
501+
map: function() {
502+
return {
503+
version: 3,
504+
sources: ["test1.js"],
505+
names: ["foo", "x", "bar", "not_called1"],
506+
mappings: "AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC"
507+
};
508+
}
509+
},
510+
};
511+
compilation.errors = [];
512+
compilation.warnings = [];
513+
514+
eventBindings[0].handler(compilation);
515+
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
516+
});
517+
518+
it("should get no warnings", function() {
519+
compilationEventBindings[1].handler([{
520+
files: ["test2.js"]
521+
}], function() {
522+
compilation.warnings.length.should.be.exactly(0);
523+
});
524+
});
525+
});
526+
});
412527
});
413528
});
414529
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.export = function someUsedFunction() {};
2+
3+
function someUnRemoteUsedFunction1() {}
4+
function someUnRemoteUsedFunction2() {}
5+
function someUnRemoteUsedFunction3() {}
6+
function someUnRemoteUsedFunction4() {}
7+
function someUnRemoteUsedFunction5() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Hash: 4beee256fa6b8f69eae8
2+
Time: Xms
3+
Asset Size Chunks Chunk Names
4+
bundle.js 2.3 kB 0 [emitted] main
5+
chunk {0} bundle.js (main) 1.04 kB [entry] [rendered]
6+
[0] (webpack)/buildin/module.js 495 bytes {0} [built]
7+
[1] (webpack)/test/statsCases/warnings-uglifyjs/a.js 249 bytes {0} [built]
8+
[2] (webpack)/test/statsCases/warnings-uglifyjs/index.js 299 bytes {0} [built]
9+
10+
WARNING in bundle.js from UglifyJs
11+
Dropping unused function someUnRemoteUsedFunction1 [./a.js:3,0]
12+
Dropping unused function someUnRemoteUsedFunction2 [./a.js:4,0]
13+
Dropping unused function someUnRemoteUsedFunction3 [./a.js:5,0]
14+
Dropping unused function someUnRemoteUsedFunction4 [./a.js:6,0]
15+
Dropping unused function someUnRemoteUsedFunction5 [./a.js:7,0]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var someRequiredUsedFunction = require("./a");
2+
3+
function someUsedFunction() {}
4+
5+
someRequiredUsedFunction();
6+
someUsedFunction();
7+
8+
function someUnUsedFunction1() {}
9+
function someUnUsedFunction2() {}
10+
function someUnUsedFunction3() {}
11+
function someUnUsedFunction4() {}
12+
function someUnUsedFunction5() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var webpack = require("webpack");
2+
3+
module.exports = {
4+
entry: "./index",
5+
output: {
6+
filename: "bundle.js"
7+
},
8+
plugins: [new webpack.optimize.UglifyJsPlugin({
9+
warningsFilter: function(filename) {
10+
return /a\.js$/.test(filename);
11+
},
12+
sourceMap: true,
13+
compress: {
14+
warnings: true,
15+
},
16+
mangle: false,
17+
beautify: true,
18+
comments: false
19+
})],
20+
stats: {
21+
chunkModules: false,
22+
modules: true,
23+
providedExports: true,
24+
usedExports: true
25+
}
26+
};

0 commit comments

Comments
 (0)