Skip to content

Commit

Permalink
refactor: code(#291)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `warningsFilter` option was remove without replacement, `terser` version is `5`, returned value of the `minify` option was changes, only `code`/`map`/`extractedComments` are valid
  • Loading branch information
evilebottnawi authored Aug 4, 2020
1 parent b5e6b8d commit 2e3598c
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 154 deletions.
45 changes: 3 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,18 @@ module.exports = {
minimize: true,
minimizer: [
new TerserPlugin({
// Can be async
minify: (file, sourceMap) => {
const extractedComments = [];

// Custom logic for extract comments

const { error, map, code, warnings } = require('uglify-module') // Or require('./path/to/uglify-module')
const { map, code } = require('uglify-module') // Or require('./path/to/uglify-module')
.minify(file, {
/* Your options for minification */
});

return { error, map, code, warnings, extractedComments };
return { map, code, extractedComments };
},
}),
],
Expand All @@ -338,7 +339,6 @@ module.exports = {
new TerserPlugin({
terserOptions: {
ecma: undefined,
warnings: false,
parse: {},
compress: {},
mangle: true, // Note `mangle.properties` is `false` by default.
Expand Down Expand Up @@ -573,45 +573,6 @@ module.exports = {
};
```

### `warningsFilter`

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

Allow to filter [terser](https://github.com/terser-js/terser) warnings.
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**

```js
module.exports = {
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
warningsFilter: (warning, file, source) => {
if (/Dropping unreachable code/i.test(warning)) {
return true;
}

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

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

return false;
},
}),
],
},
};
```

## Examples

### Preserve Comments
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import schema from './options.json';

import { minify as minifyFn } from './minify';

const warningRegex = /\[.+:([0-9]+),([0-9]+)\]/;

class TerserPlugin {
constructor(options = {}) {
validateOptions(schema, options, {
Expand All @@ -34,7 +32,6 @@ class TerserPlugin {
minify,
terserOptions = {},
test = /\.m?js(\?.*)?$/i,
warningsFilter = () => true,
extractComments = true,
sourceMap,
cache = true,
Expand Down Expand Up @@ -438,11 +435,9 @@ class TerserPlugin {
let taskResult;

try {
if (worker) {
taskResult = await worker.transform(serialize(task));
} else {
taskResult = await minifyFn(task);
}
taskResult = await (worker
? worker.transform(serialize(task))
: minifyFn(task));
} catch (error) {
taskResult = { error };
}
Expand Down
12 changes: 5 additions & 7 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { minify: terserMinify } = require('terser');

const buildTerserOptions = ({
ecma,
warnings,
parse = {},
compress = {},
mangle,
Expand Down Expand Up @@ -40,7 +39,6 @@ const buildTerserOptions = ({
nameCache,
safari10,
toplevel,
warnings,
});

function isObject(value) {
Expand Down Expand Up @@ -145,7 +143,7 @@ const buildComments = (options, terserOptions, extractedComments) => {
};
};

const minify = (options) => {
async function minify(options) {
const { name, input, inputSourceMap, minify: minifyFn } = options;

if (minifyFn) {
Expand All @@ -168,10 +166,10 @@ const minify = (options) => {
extractedComments
);

return terserMinify({ [name]: input }, terserOptions)
.then(({ map, code }) => ({ map, code, extractedComments }))
.catch((error) => ({ error }));
};
const result = await terserMinify({ [name]: input }, terserOptions);

return { ...result, extractedComments };
}

function transform(options) {
// 'use strict' => this === undefined (Clean Scope)
Expand Down
20 changes: 0 additions & 20 deletions test/__snapshots__/terserOptions-option.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -643,23 +643,3 @@ DefaultsError: \`unknown\` is not a supported option",
`;

exports[`terserOptions option should match snapshot for the "unknown" option: warnings 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "false" value: assets 1`] = `
Object {
"main.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){}]);",
}
`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "false" value: errors 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "false" value: warnings 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "true" value: assets 1`] = `
Object {
"main.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){}]);",
}
`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "true" value: errors 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "true" value: warnings 1`] = `Array []`;
20 changes: 0 additions & 20 deletions test/__snapshots__/terserOptions-option.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -432,23 +432,3 @@ DefaultsError: \`unknown\` is not a supported option",
`;

exports[`terserOptions option should match snapshot for the "unknown" option: warnings 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "false" value: assets 1`] = `
Object {
"main.js": "",
}
`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "false" value: errors 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "false" value: warnings 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "true" value: assets 1`] = `
Object {
"main.js": "",
}
`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "true" value: errors 1`] = `Array []`;

exports[`terserOptions option should match snapshot for the "warnings" option with the "true" value: warnings 1`] = `Array []`;
5 changes: 0 additions & 5 deletions test/__snapshots__/worker.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports[`worker normalizes when terserOptions.output.comments is string: all: te
Object {
"code": "var foo=1;/* hello */",
"extractedComments": Array [],
"map": undefined,
}
`;

Expand All @@ -14,31 +13,27 @@ Object {
"extractedComments": Array [
"/* hello */",
],
"map": undefined,
}
`;

exports[`worker should match snapshot when terserOptions.extractComments is number: test4.js 1`] = `
Object {
"code": "var foo=1;",
"extractedComments": Array [],
"map": undefined,
}
`;

exports[`worker should match snapshot when terserOptions.output.comments is string: some: test3.js 1`] = `
Object {
"code": "var foo=1;",
"extractedComments": Array [],
"map": undefined,
}
`;

exports[`worker should match snapshot with extract option set to a single file: test5.js 1`] = `
Object {
"code": "/******/function hello(o){console.log(o)}",
"extractedComments": Array [],
"map": undefined,
}
`;

Expand Down
5 changes: 0 additions & 5 deletions test/__snapshots__/worker.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ exports[`worker normalizes when terserOptions.output.comments is string: all: te
Object {
"code": "var foo=1;/* hello */",
"extractedComments": Array [],
"map": undefined,
}
`;

Expand All @@ -14,31 +13,27 @@ Object {
"extractedComments": Array [
"/* hello */",
],
"map": undefined,
}
`;

exports[`worker should match snapshot when terserOptions.extractComments is number: test4.js 1`] = `
Object {
"code": "var foo=1;",
"extractedComments": Array [],
"map": undefined,
}
`;

exports[`worker should match snapshot when terserOptions.output.comments is string: some: test3.js 1`] = `
Object {
"code": "var foo=1;",
"extractedComments": Array [],
"map": undefined,
}
`;

exports[`worker should match snapshot with extract option set to a single file: test5.js 1`] = `
Object {
"code": "/******/function hello(o){console.log(o)}",
"extractedComments": Array [],
"map": undefined,
}
`;

Expand Down
40 changes: 0 additions & 40 deletions test/terserOptions-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('terserOptions option', () => {
terserOptions: {
ecma: 5,
mangle: false,
warnings: true,
output: {
beautify: true,
},
Expand All @@ -46,7 +45,6 @@ describe('terserOptions option', () => {
terserOptions: {
ecma: 6,
mangle: false,
warnings: true,
output: {
beautify: true,
},
Expand All @@ -69,7 +67,6 @@ describe('terserOptions option', () => {
terserOptions: {
ecma: 7,
mangle: false,
warnings: true,
output: {
beautify: true,
},
Expand All @@ -92,7 +89,6 @@ describe('terserOptions option', () => {
terserOptions: {
ecma: 8,
mangle: false,
warnings: true,
output: {
beautify: true,
},
Expand All @@ -106,42 +102,6 @@ describe('terserOptions option', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should match snapshot for the "warnings" option with the "false" value', async () => {
const compiler = getCompiler({
entry: `${__dirname}/fixtures/unreachable-code.js`,
});

new TerserPlugin({
terserOptions: {
warnings: false,
},
}).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should match snapshot for the "warnings" option with the "true" value', async () => {
const compiler = getCompiler({
entry: `${__dirname}/fixtures/unreachable-code.js`,
});

new TerserPlugin({
terserOptions: {
warnings: true,
},
}).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should match snapshot for the "parse.ecma" option with the "8" value', async () => {
const compiler = getCompiler();

Expand Down
1 change: 0 additions & 1 deletion test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ it('validation', () => {
terserOptions: {
// eslint-disable-next-line no-undefined
ecma: undefined,
warnings: false,
parse: {},
compress: {},
mangle: true,
Expand Down

0 comments on commit 2e3598c

Please sign in to comment.