diff --git a/README.md b/README.md index 6ad9224e..ac947099 100644 --- a/README.md +++ b/README.md @@ -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 }; }, }), ], @@ -338,7 +339,6 @@ module.exports = { new TerserPlugin({ terserOptions: { ecma: undefined, - warnings: false, parse: {}, compress: {}, mangle: true, // Note `mangle.properties` is `false` by default. @@ -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 diff --git a/package-lock.json b/package-lock.json index f379c325..a4dbf862 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3287,9 +3287,9 @@ } }, "caniuse-lite": { - "version": "1.0.30001109", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001109.tgz", - "integrity": "sha512-4JIXRodHzdS3HdK8nSgIqXYLExOvG+D2/EenSvcub2Kp3QEADjo2v2oUn5g0n0D+UNwG9BtwKOyGcSq2qvQXvQ==", + "version": "1.0.30001110", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001110.tgz", + "integrity": "sha512-KqJWeat4rhSHF0ito4yz9q/JuZHkvn71SsBnxge4azjPDbowIjOUnS8i1xpKGxZxU6BFiPqO2hSV2eiCpFQVRw==", "dev": true }, "capture-exit": { @@ -5057,9 +5057,9 @@ } }, "electron-to-chromium": { - "version": "1.3.516", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.516.tgz", - "integrity": "sha512-WDM5AAQdOrvLqSX8g3Zd5AujBXfMxf96oeZkff0U2HF5op3tjShE+on2yay3r1UD4M9I3p0iHpAS4+yV8U8A9A==", + "version": "1.3.518", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.518.tgz", + "integrity": "sha512-IspiwXYDKZMxo+qc3Vof4WtwbG9BMDbJfati8PYj7uS4DJmJ67pwjCKZxlTBSAuCZSMcbRnj2Xz2H14uiKT7bQ==", "dev": true }, "elliptic": { diff --git a/src/index.js b/src/index.js index d9eb2ab1..12943ca4 100644 --- a/src/index.js +++ b/src/index.js @@ -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, { @@ -34,7 +32,6 @@ class TerserPlugin { minify, terserOptions = {}, test = /\.m?js(\?.*)?$/i, - warningsFilter = () => true, extractComments = true, sourceMap, cache = true, @@ -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 }; } diff --git a/src/minify.js b/src/minify.js index 2a28867e..63b4e3cb 100644 --- a/src/minify.js +++ b/src/minify.js @@ -2,7 +2,6 @@ const { minify: terserMinify } = require('terser'); const buildTerserOptions = ({ ecma, - warnings, parse = {}, compress = {}, mangle, @@ -40,7 +39,6 @@ const buildTerserOptions = ({ nameCache, safari10, toplevel, - warnings, }); function isObject(value) { @@ -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) { @@ -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) diff --git a/test/__snapshots__/terserOptions-option.test.js.snap.webpack4 b/test/__snapshots__/terserOptions-option.test.js.snap.webpack4 index ceafdb7c..4641ad1b 100644 --- a/test/__snapshots__/terserOptions-option.test.js.snap.webpack4 +++ b/test/__snapshots__/terserOptions-option.test.js.snap.webpack4 @@ -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 []`; diff --git a/test/__snapshots__/terserOptions-option.test.js.snap.webpack5 b/test/__snapshots__/terserOptions-option.test.js.snap.webpack5 index 8fe835e2..5cf6ae9d 100644 --- a/test/__snapshots__/terserOptions-option.test.js.snap.webpack5 +++ b/test/__snapshots__/terserOptions-option.test.js.snap.webpack5 @@ -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 []`; diff --git a/test/__snapshots__/worker.test.js.snap.webpack4 b/test/__snapshots__/worker.test.js.snap.webpack4 index e0f536e4..08977dfe 100644 --- a/test/__snapshots__/worker.test.js.snap.webpack4 +++ b/test/__snapshots__/worker.test.js.snap.webpack4 @@ -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, } `; @@ -14,7 +13,6 @@ Object { "extractedComments": Array [ "/* hello */", ], - "map": undefined, } `; @@ -22,7 +20,6 @@ exports[`worker should match snapshot when terserOptions.extractComments is numb Object { "code": "var foo=1;", "extractedComments": Array [], - "map": undefined, } `; @@ -30,7 +27,6 @@ exports[`worker should match snapshot when terserOptions.output.comments is stri Object { "code": "var foo=1;", "extractedComments": Array [], - "map": undefined, } `; @@ -38,7 +34,6 @@ exports[`worker should match snapshot with extract option set to a single file: Object { "code": "/******/function hello(o){console.log(o)}", "extractedComments": Array [], - "map": undefined, } `; diff --git a/test/__snapshots__/worker.test.js.snap.webpack5 b/test/__snapshots__/worker.test.js.snap.webpack5 index e0f536e4..08977dfe 100644 --- a/test/__snapshots__/worker.test.js.snap.webpack5 +++ b/test/__snapshots__/worker.test.js.snap.webpack5 @@ -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, } `; @@ -14,7 +13,6 @@ Object { "extractedComments": Array [ "/* hello */", ], - "map": undefined, } `; @@ -22,7 +20,6 @@ exports[`worker should match snapshot when terserOptions.extractComments is numb Object { "code": "var foo=1;", "extractedComments": Array [], - "map": undefined, } `; @@ -30,7 +27,6 @@ exports[`worker should match snapshot when terserOptions.output.comments is stri Object { "code": "var foo=1;", "extractedComments": Array [], - "map": undefined, } `; @@ -38,7 +34,6 @@ exports[`worker should match snapshot with extract option set to a single file: Object { "code": "/******/function hello(o){console.log(o)}", "extractedComments": Array [], - "map": undefined, } `; diff --git a/test/terserOptions-option.test.js b/test/terserOptions-option.test.js index 2261a68a..74e5c8e8 100644 --- a/test/terserOptions-option.test.js +++ b/test/terserOptions-option.test.js @@ -23,7 +23,6 @@ describe('terserOptions option', () => { terserOptions: { ecma: 5, mangle: false, - warnings: true, output: { beautify: true, }, @@ -46,7 +45,6 @@ describe('terserOptions option', () => { terserOptions: { ecma: 6, mangle: false, - warnings: true, output: { beautify: true, }, @@ -69,7 +67,6 @@ describe('terserOptions option', () => { terserOptions: { ecma: 7, mangle: false, - warnings: true, output: { beautify: true, }, @@ -92,7 +89,6 @@ describe('terserOptions option', () => { terserOptions: { ecma: 8, mangle: false, - warnings: true, output: { beautify: true, }, @@ -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(); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 45ec0f24..b55b397e 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -175,7 +175,6 @@ it('validation', () => { terserOptions: { // eslint-disable-next-line no-undefined ecma: undefined, - warnings: false, parse: {}, compress: {}, mangle: true,