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

Commit

Permalink
fix: no warning when warningsFilter return false (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jun 25, 2018
1 parent 1521c82 commit cabee6c
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 46 deletions.
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ class UglifyJsPlugin {
column,
});

let warningMessage = null;
if (!warningsFilter(original.source)) {
return null;
}

if (warningsFilter(original.source)) {
warningMessage = warning.replace(warningRegex, '');
let warningMessage = warning.replace(warningRegex, '');

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

return `UglifyJs Plugin: ${warningMessage} in ${file}`;
Expand Down
178 changes: 138 additions & 40 deletions test/all-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,36 +99,40 @@ describe('when applied with all options', () => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = {
'test.js': {
source: () => '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }',
map: () => {
return {
sourceAndMap: () => {
return {
source: '/** @preserve Foo Bar */ function foo(longVariableName) { longVariableName = 1; }',
map: {
version: 3,
sources: ['test.js'],
names: ['foo', 'longVariableName'],
mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB',
};
},
},
};
},
'test1.js': {
source: () => 'invalid javascript',
map: () => {
sourceAndMap: () => {
return {
version: 3,
sources: ['test1.js'],
names: [''],
mappings: 'AAAA',
source: 'invalid javascript',
map: {
version: 3,
sources: ['test1.js'],
names: [''],
mappings: 'AAAA',
},
};
},
},
'test2.js': {
source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: () => {
sourceAndMap: () => {
return {
version: 3,
sources: ['test1.js'],
names: ['foo', 'x', 'bar', 'not_called1'],
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: {
version: 3,
sources: ['test1.js'],
names: ['foo', 'x', 'bar', 'not_called1'],
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
},
};
},
},
Expand All @@ -146,13 +150,15 @@ describe('when applied with all options', () => {
},
},
'test4.js': {
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; }',
map: () => {
sourceAndMap: () => {
return {
version: 3,
sources: ['test.js'],
names: ['foo', 'longVariableName'],
mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB',
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; }',
map: {
version: 3,
sources: ['test.js'],
names: ['foo', 'longVariableName'],
mappings: 'AAAA,QAASA,KAAIC,kBACTA,iBAAmB',
},
};
},
},
Expand Down Expand Up @@ -279,7 +285,7 @@ describe('when applied with all options', () => {
});

describe('with warningsFilter set', () => {
describe('and the filter returns true', () => {
describe('and the filter returns true without source map', () => {
beforeEach(() => {
const pluginEnvironment = new PluginEnvironment();
const compilerEnv = pluginEnvironment.getEnvironmentStub();
Expand All @@ -302,14 +308,61 @@ describe('when applied with all options', () => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = {
'test2.js': {
'test.js': {
source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: () => {
},
};
compilation.errors = [];
compilation.warnings = [];

eventBindings[0].handler(compilation);
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
});

it('should get all warnings', () => {
compilationEventBindings[1].handler([{
files: ['test.js'],
}], () => {
expect(compilation.warnings.length).toBe(1);
expect(compilation.warnings[0]).toBeInstanceOf(Error);
expect(compilation.warnings[0].message).toEqual(expect.stringContaining('Dropping unreachable code'));
});
});
});

describe('and the filter returns true with source map', () => {
beforeEach(() => {
const pluginEnvironment = new PluginEnvironment();
const compilerEnv = pluginEnvironment.getEnvironmentStub();
compilerEnv.context = '';

const plugin = new UglifyJsPlugin({
warningsFilter: () => true,
sourceMap: true,
uglifyOptions: {
warnings: true,
mangle: false,
output: {
beautify: true,
},
},
});
plugin.apply(compilerEnv);
eventBindings = pluginEnvironment.getEventBindings();

chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = {
'test.js': {
sourceAndMap: () => {
return {
version: 3,
sources: ['test1.js'],
names: ['foo', 'x', 'bar', 'not_called1'],
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: {
version: 3,
sources: ['test.js'],
names: ['foo', 'x', 'bar', 'not_called1'],
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
},
};
},
},
Expand All @@ -323,7 +376,7 @@ describe('when applied with all options', () => {

it('should get all warnings', () => {
compilationEventBindings[1].handler([{
files: ['test2.js'],
files: ['test.js'],
}], () => {
expect(compilation.warnings.length).toBe(1);
expect(compilation.warnings[0]).toBeInstanceOf(Error);
Expand All @@ -332,7 +385,7 @@ describe('when applied with all options', () => {
});
});

describe('and the filter returns false', () => {
describe('and the filter returns false without source map', () => {
beforeEach(() => {
const pluginEnvironment = new PluginEnvironment();
const compilerEnv = pluginEnvironment.getEnvironmentStub();
Expand All @@ -355,14 +408,59 @@ describe('when applied with all options', () => {
chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = {
'test2.js': {
'test.js': {
source: () => 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: () => {
},
};
compilation.errors = [];
compilation.warnings = [];

eventBindings[0].handler(compilation);
compilationEventBindings = chunkPluginEnvironment.getEventBindings();
});

it('should get no warnings', () => {
compilationEventBindings[1].handler([{
files: ['test.js'],
}], () => {
expect(compilation.warnings.length).toBe(0);
});
});
});

describe('and the filter returns false with source map', () => {
beforeEach(() => {
const pluginEnvironment = new PluginEnvironment();
const compilerEnv = pluginEnvironment.getEnvironmentStub();
compilerEnv.context = '';

const plugin = new UglifyJsPlugin({
warningsFilter: () => false,
sourceMap: true,
uglifyOptions: {
warnings: true,
mangle: false,
output: {
beautify: true,
},
},
});
plugin.apply(compilerEnv);
eventBindings = pluginEnvironment.getEventBindings();

chunkPluginEnvironment = new PluginEnvironment();
compilation = chunkPluginEnvironment.getEnvironmentStub();
compilation.assets = {
'test.js': {
sourceAndMap: () => {
return {
version: 3,
sources: ['test1.js'],
names: ['foo', 'x', 'bar', 'not_called1'],
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
source: 'function foo(x) { if (x) { return bar(); not_called1(); } }',
map: {
version: 3,
sources: ['test.js'],
names: ['foo', 'x', 'bar', 'not_called1'],
mappings: 'AAAA,QAASA,KAAIC,GACT,GAAIA,EAAG,CACH,MAAOC,MACPC',
},
};
},
},
Expand All @@ -376,7 +474,7 @@ describe('when applied with all options', () => {

it('should get no warnings', () => {
compilationEventBindings[1].handler([{
files: ['test2.js'],
files: ['test.js'],
}], () => {
expect(compilation.warnings.length).toBe(0);
});
Expand Down

0 comments on commit cabee6c

Please sign in to comment.