Skip to content

Commit

Permalink
feat: pass asset path for warningsFilter (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Oct 21, 2019
1 parent 92c56ad commit 9a0a575
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,13 @@ module.exports = {

### `warningsFilter`

Type: `Function<(warning, source) -> Boolean>`
Type: `Function<(warning, source, file) -> Boolean>`
Default: `() => true`

Allow to filter [terser](https://github.com/terser-js/terser) warnings.
Return `true` to keep the warning, `false` otherwise.
Return `true` to keep the warning, a falsy value (`false`/`null`/`undefined`) otherwise.

> ⚠️ The `source` argument will contain `undefined` if you don't use source maps.
**webpack.config.js**

Expand All @@ -617,12 +619,16 @@ module.exports = {
minimize: true,
minimizer: [
new TerserPlugin({
warningsFilter: (warning, source) => {
warningsFilter: (warning, source, file) => {
if (/Dropping unreachable code/i.test(warning)) {
return true;
}

if (/filename\.js/i.test(source)) {
if (/source\.js/i.test(source)) {
return true;
}

if (/file\.js/i.test(file)) {
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class TerserPlugin {
}
}

if (warningsFilter && !warningsFilter(warning, source)) {
// Todo change order in next major release
if (warningsFilter && !warningsFilter(warning, source, file)) {
return null;
}

Expand Down
18 changes: 17 additions & 1 deletion test/__snapshots__/warningsFilter-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ Object {

exports[`warningsFilter option should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by file): errors 1`] = `Array []`;

exports[`warningsFilter option should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by file): warnings 1`] = `Array []`;
exports[`warningsFilter option should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by file): warnings 1`] = `
Array [
"Terser Plugin: Dropping unreachable code [./test/fixtures/unreachable-code-2.js:1,40]",
"Terser Plugin: Dropping unused function foo [./test/fixtures/unreachable-code-2.js:1,0]",
]
`;

exports[`warningsFilter option should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by message): assets 1`] = `
Object {
Expand Down Expand Up @@ -58,3 +63,14 @@ Array [
"Terser Plugin: Dropping unused function foo [./test/fixtures/unreachable-code.js:1,0]",
]
`;

exports[`warningsFilter option should match snapshot for an empty "function" value: assets 1`] = `
Object {
"one.js": "!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,\\"a\\",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p=\\"\\",r(r.s=0)}([function(e,t){}]);",
"two.js": "!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,\\"a\\",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p=\\"\\",r(r.s=1)}([,function(e,t){}]);",
}
`;

exports[`warningsFilter option should match snapshot for an empty "function" value: errors 1`] = `Array []`;

exports[`warningsFilter option should match snapshot for an empty "function" value: warnings 1`] = `Array []`;
21 changes: 20 additions & 1 deletion test/warningsFilter-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('warningsFilter option', () => {
it('should match snapshot for a "function" value and the "sourceMap" option is "true" (filter by file)', async () => {
new TerserPlugin({
warningsFilter(warning, source, file) {
if (/two\.(.*)?\.js/.test(file)) {
if (/two\.js/.test(file)) {
return true;
}

Expand All @@ -123,4 +123,23 @@ describe('warningsFilter option', () => {
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});

it('should match snapshot for an empty "function" value', async () => {
new TerserPlugin({
warningsFilter() {},
terserOptions: {
warnings: true,
},
sourceMap: false,
}).apply(compiler);

const stats = await compile(compiler);

const errors = stats.compilation.errors.map(cleanErrorStack);
const warnings = stats.compilation.warnings.map(cleanErrorStack);

expect(errors).toMatchSnapshot('errors');
expect(warnings).toMatchSnapshot('warnings');
expect(getAssets(stats, compiler)).toMatchSnapshot('assets');
});
});

0 comments on commit 9a0a575

Please sign in to comment.