Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

Commit cabee6c

Browse files
fix: no warning when warningsFilter return false (#317)
1 parent 1521c82 commit cabee6c

File tree

2 files changed

+144
-46
lines changed

2 files changed

+144
-46
lines changed

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ class UglifyJsPlugin {
9191
column,
9292
});
9393

94-
let warningMessage = null;
94+
if (!warningsFilter(original.source)) {
95+
return null;
96+
}
9597

96-
if (warningsFilter(original.source)) {
97-
warningMessage = warning.replace(warningRegex, '');
98+
let warningMessage = warning.replace(warningRegex, '');
9899

99-
if (original && original.source && original.source !== file) {
100-
warningMessage += `[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
101-
}
100+
if (original && original.source && original.source !== file) {
101+
warningMessage += `[${requestShortener.shorten(original.source)}:${original.line},${original.column}]`;
102102
}
103103

104104
return `UglifyJs Plugin: ${warningMessage} in ${file}`;

test/all-options.test.js

Lines changed: 138 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,36 +99,40 @@ describe('when applied with all options', () => {
9999
chunkPluginEnvironment = new PluginEnvironment();
100100
compilation = chunkPluginEnvironment.getEnvironmentStub();
101101
compilation.assets = {
102-
'test.js': {
103-
source: () => '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }',
104-
map: () => {
105-
return {
102+
sourceAndMap: () => {
103+
return {
104+
source: '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }',
105+
map: {
106106
version: 3,
107107
sources: ['test.js'],
108108
names: ['foo', 'longVariableName'],
109109
mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB',
110-
};
111-
},
110+
},
111+
};
112112
},
113113
'test1.js': {
114-
source: () => 'invalid javascript',
115-
map: () => {
114+
sourceAndMap: () => {
116115
return {
117-
version: 3,
118-
sources: ['test1.js'],
119-
names: [''],
120-
mappings: 'AAAA',
116+
source: 'invalid javascript',
117+
map: {
118+
version: 3,
119+
sources: ['test1.js'],
120+
names: [''],
121+
mappings: 'AAAA',
122+
},
121123
};
122124
},
123125
},
124126
'test2.js': {
125-
source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }',
126-
map: () => {
127+
sourceAndMap: () => {
127128
return {
128-
version: 3,
129-
sources: ['test1.js'],
130-
names: ['foo', 'x', 'bar', 'not_called1'],
131-
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
129+
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
130+
map: {
131+
version: 3,
132+
sources: ['test1.js'],
133+
names: ['foo', 'x', 'bar', 'not_called1'],
134+
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
135+
},
132136
};
133137
},
134138
},
@@ -146,13 +150,15 @@ describe('when applied with all options', () => {
146150
},
147151
},
148152
'test4.js': {
149-
source: () => '/*! this comment should be extracted */ function foo(longVariableName) { /* this will not be extracted */ longVariableName = 1; } // another comment that should be extracted to a separate file\n function foo2(bar) { return bar; }',
150-
map: () => {
153+
sourceAndMap: () => {
151154
return {
152-
version: 3,
153-
sources: ['test.js'],
154-
names: ['foo', 'longVariableName'],
155-
mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB',
155+
source: '/*! this comment should be extracted */ function foo(longVariableName) { /* this will not be extracted */ longVariableName = 1; } // another comment that should be extracted to a separate file\n function foo2(bar) { return bar; }',
156+
map: {
157+
version: 3,
158+
sources: ['test.js'],
159+
names: ['foo', 'longVariableName'],
160+
mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB',
161+
},
156162
};
157163
},
158164
},
@@ -279,7 +285,7 @@ describe('when applied with all options', () => {
279285
});
280286

281287
describe('with warningsFilter set', () => {
282-
describe('and the filter returns true', () => {
288+
describe('and the filter returns true without source map', () => {
283289
beforeEach(() => {
284290
const pluginEnvironment = new PluginEnvironment();
285291
const compilerEnv = pluginEnvironment.getEnvironmentStub();
@@ -302,14 +308,61 @@ describe('when applied with all options', () => {
302308
chunkPluginEnvironment = new PluginEnvironment();
303309
compilation = chunkPluginEnvironment.getEnvironmentStub();
304310
compilation.assets = {
305-
'test2.js': {
311+
'test.js': {
306312
source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }',
307-
map: () => {
313+
},
314+
};
315+
compilation.errors = [];
316+
compilation.warnings = [];
317+
318+
eventBindings[0].handler(compilation);
319+
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
320+
});
321+
322+
it('should get all warnings', () => {
323+
compilationEventBindings[1].handler([{
324+
files: ['test.js'],
325+
}], () => {
326+
expect(compilation.warnings.length).toBe(1);
327+
expect(compilation.warnings[0]).toBeInstanceOf(Error);
328+
expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code'));
329+
});
330+
});
331+
});
332+
333+
describe('and the filter returns true with source map', () => {
334+
beforeEach(() => {
335+
const pluginEnvironment = new PluginEnvironment();
336+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
337+
compilerEnv.context = '';
338+
339+
const plugin = new UglifyJsPlugin({
340+
warningsFilter: () => true,
341+
sourceMap: true,
342+
uglifyOptions: {
343+
warnings: true,
344+
mangle: false,
345+
output: {
346+
beautify: true,
347+
},
348+
},
349+
});
350+
plugin.apply(compilerEnv);
351+
eventBindings = pluginEnvironment.getEventBindings();
352+
353+
chunkPluginEnvironment = new PluginEnvironment();
354+
compilation = chunkPluginEnvironment.getEnvironmentStub();
355+
compilation.assets = {
356+
'test.js': {
357+
sourceAndMap: () => {
308358
return {
309-
version: 3,
310-
sources: ['test1.js'],
311-
names: ['foo', 'x', 'bar', 'not_called1'],
312-
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
359+
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
360+
map: {
361+
version: 3,
362+
sources: ['test.js'],
363+
names: ['foo', 'x', 'bar', 'not_called1'],
364+
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
365+
},
313366
};
314367
},
315368
},
@@ -323,7 +376,7 @@ describe('when applied with all options', () => {
323376

324377
it('should get all warnings', () => {
325378
compilationEventBindings[1].handler([{
326-
files: ['test2.js'],
379+
files: ['test.js'],
327380
}], () => {
328381
expect(compilation.warnings.length).toBe(1);
329382
expect(compilation.warnings[0]).toBeInstanceOf(Error);
@@ -332,7 +385,7 @@ describe('when applied with all options', () => {
332385
});
333386
});
334387

335-
describe('and the filter returns false', () => {
388+
describe('and the filter returns false without source map', () => {
336389
beforeEach(() => {
337390
const pluginEnvironment = new PluginEnvironment();
338391
const compilerEnv = pluginEnvironment.getEnvironmentStub();
@@ -355,14 +408,59 @@ describe('when applied with all options', () => {
355408
chunkPluginEnvironment = new PluginEnvironment();
356409
compilation = chunkPluginEnvironment.getEnvironmentStub();
357410
compilation.assets = {
358-
'test2.js': {
411+
'test.js': {
359412
source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }',
360-
map: () => {
413+
},
414+
};
415+
compilation.errors = [];
416+
compilation.warnings = [];
417+
418+
eventBindings[0].handler(compilation);
419+
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
420+
});
421+
422+
it('should get no warnings', () => {
423+
compilationEventBindings[1].handler([{
424+
files: ['test.js'],
425+
}], () => {
426+
expect(compilation.warnings.length).toBe(0);
427+
});
428+
});
429+
});
430+
431+
describe('and the filter returns false with source map', () => {
432+
beforeEach(() => {
433+
const pluginEnvironment = new PluginEnvironment();
434+
const compilerEnv = pluginEnvironment.getEnvironmentStub();
435+
compilerEnv.context = '';
436+
437+
const plugin = new UglifyJsPlugin({
438+
warningsFilter: () => false,
439+
sourceMap: true,
440+
uglifyOptions: {
441+
warnings: true,
442+
mangle: false,
443+
output: {
444+
beautify: true,
445+
},
446+
},
447+
});
448+
plugin.apply(compilerEnv);
449+
eventBindings = pluginEnvironment.getEventBindings();
450+
451+
chunkPluginEnvironment = new PluginEnvironment();
452+
compilation = chunkPluginEnvironment.getEnvironmentStub();
453+
compilation.assets = {
454+
'test.js': {
455+
sourceAndMap: () => {
361456
return {
362-
version: 3,
363-
sources: ['test1.js'],
364-
names: ['foo', 'x', 'bar', 'not_called1'],
365-
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
457+
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
458+
map: {
459+
version: 3,
460+
sources: ['test.js'],
461+
names: ['foo', 'x', 'bar', 'not_called1'],
462+
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
463+
},
366464
};
367465
},
368466
},
@@ -376,7 +474,7 @@ describe('when applied with all options', () => {
376474

377475
it('should get no warnings', () => {
378476
compilationEventBindings[1].handler([{
379-
files: ['test2.js'],
477+
files: ['test.js'],
380478
}], () => {
381479
expect(compilation.warnings.length).toBe(0);
382480
});

0 commit comments

Comments
 (0)