From 52c1aef218923b716a0b25433e7d916c5bb6c805 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Mon, 29 Aug 2022 21:13:47 +0300 Subject: [PATCH] fix: allow disable compress options for `terser` and `swc` (#514) --- src/utils.js | 52 +++++++++++-------- test/__snapshots__/minify-option.test.js.snap | 13 +++++ .../terserOptions-option.test.js.snap | 5 +- test/minify-option.test.js | 17 ++++++ 4 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/utils.js b/src/utils.js index 7db615a..1c7c3d5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -228,7 +228,9 @@ async function terserMinify( ...terserOptions, compress: typeof terserOptions.compress === "boolean" - ? {} + ? terserOptions.compress + ? {} + : false : { ...terserOptions.compress }, // ecma: terserOptions.ecma, // ie8: terserOptions.ie8, @@ -281,17 +283,19 @@ async function terserMinify( ); } - // More optimizations - if (typeof terserOptions.compress.ecma === "undefined") { - terserOptions.compress.ecma = terserOptions.ecma; - } + if (terserOptions.compress) { + // More optimizations + if (typeof terserOptions.compress.ecma === "undefined") { + terserOptions.compress.ecma = terserOptions.ecma; + } - // https://github.com/webpack/webpack/issues/16135 - if ( - terserOptions.ecma === 5 && - typeof terserOptions.compress.arrows === "undefined" - ) { - terserOptions.compress.arrows = false; + // https://github.com/webpack/webpack/issues/16135 + if ( + terserOptions.ecma === 5 && + typeof terserOptions.compress.arrows === "undefined" + ) { + terserOptions.compress.arrows = false; + } } const [[filename, code]] = Object.entries(input); @@ -558,7 +562,9 @@ async function swcMinify(input, sourceMap, minimizerOptions) { ...swcOptions, compress: typeof swcOptions.compress === "boolean" - ? {} + ? swcOptions.compress + ? {} + : false : { ...swcOptions.compress }, mangle: swcOptions.mangle == null @@ -588,17 +594,19 @@ async function swcMinify(input, sourceMap, minimizerOptions) { swcOptions.sourceMap = true; } - // More optimizations - if (typeof swcOptions.compress.ecma === "undefined") { - swcOptions.compress.ecma = swcOptions.ecma; - } + if (swcOptions.compress) { + // More optimizations + if (typeof swcOptions.compress.ecma === "undefined") { + swcOptions.compress.ecma = swcOptions.ecma; + } - // https://github.com/webpack/webpack/issues/16135 - if ( - swcOptions.ecma === 5 && - typeof swcOptions.compress.arrows === "undefined" - ) { - swcOptions.compress.arrows = false; + // https://github.com/webpack/webpack/issues/16135 + if ( + swcOptions.ecma === 5 && + typeof swcOptions.compress.arrows === "undefined" + ) { + swcOptions.compress.arrows = false; + } } const [[filename, code]] = Object.entries(input); diff --git a/test/__snapshots__/minify-option.test.js.snap b/test/__snapshots__/minify-option.test.js.snap index 4720acb..139f652 100644 --- a/test/__snapshots__/minify-option.test.js.snap +++ b/test/__snapshots__/minify-option.test.js.snap @@ -178,6 +178,19 @@ exports[`minify option should work using when the \`minify\` option is \`terserM exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and ECMA modules output: warnings 1`] = `Array []`; +exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to disable \`compress\` options: assets 1`] = ` +Object { + "main.js": "/*! For license information please see main.js.LICENSE.txt */ +(()=>{var r={791:r=>{const n=null&&2+2;r.exports=function r(){const n=2+2;console.log(n+1+2)}}};var n={};function o(t){var e=n[t];if(e!==undefined){return e.exports}var s=n[t]={exports:{}};r[t](s,s.exports,o);return s.exports}var t=o(791)})();", + "main.js.LICENSE.txt": "/* @preserve*/ +", +} +`; + +exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to disable \`compress\` options: errors 1`] = `Array []`; + +exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to disable \`compress\` options: warnings 1`] = `Array []`; + exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to set \`terser\` options: assets 1`] = ` Object { "main.js": "(()=>{var __webpack_modules__={791:module=>{module.exports=function(){console.log(7)}}},__webpack_module_cache__={};(function __webpack_require__(moduleId){var cachedModule=__webpack_module_cache__[moduleId];if(void 0!==cachedModule)return cachedModule.exports;var module=__webpack_module_cache__[moduleId]={exports:{}};return __webpack_modules__[moduleId](module,module.exports,__webpack_require__),module.exports})(791)})();", diff --git a/test/__snapshots__/terserOptions-option.test.js.snap b/test/__snapshots__/terserOptions-option.test.js.snap index d396f66..c40894f 100644 --- a/test/__snapshots__/terserOptions-option.test.js.snap +++ b/test/__snapshots__/terserOptions-option.test.js.snap @@ -12,7 +12,10 @@ exports[`terserOptions option should match snapshot for the "compress" option wi exports[`terserOptions option should match snapshot for the "compress" option with the "false" value: assets 1`] = ` Object { - "main.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};(function t(e){var n=o[e];if(void 0!==n)return n.exports;var s=o[e]={exports:{}};return r[e](s,s.exports,t),s.exports})(791)})();", + "main.js": "/*! For license information please see main.js.LICENSE.txt */ +(()=>{var r={791:r=>{const n=null&&2+2;r.exports=function r(){const n=2+2;console.log(n+1+2)}}};var n={};function o(t){var e=n[t];if(e!==undefined){return e.exports}var s=n[t]={exports:{}};r[t](s,s.exports,o);return s.exports}var t=o(791)})();", + "main.js.LICENSE.txt": "/* @preserve*/ +", } `; diff --git a/test/minify-option.test.js b/test/minify-option.test.js index d05cc1d..d86241f 100644 --- a/test/minify-option.test.js +++ b/test/minify-option.test.js @@ -447,6 +447,23 @@ describe("minify option", () => { expect(getWarnings(stats)).toMatchSnapshot("warnings"); }); + it("should work using when the `minify` option is `terserMinify` and allows to disable `compress` options", async () => { + const compiler = getCompiler(); + + new TerserPlugin({ + minify: TerserPlugin.terserMinify, + terserOptions: { + compress: 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 work using when the `minify` option is `uglifyJsMinify`", async () => { const compiler = getCompiler({ target: ["web", "es5"],