Skip to content

Commit

Permalink
fix: allow disable compress options for terser and swc (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Aug 29, 2022
1 parent c5a9d41 commit 52c1aef
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
52 changes: 30 additions & 22 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ async function terserMinify(
...terserOptions,
compress:
typeof terserOptions.compress === "boolean"
? {}
? terserOptions.compress
? {}
: false
: { ...terserOptions.compress },
// ecma: terserOptions.ecma,
// ie8: terserOptions.ie8,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/minify-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)})();",
Expand Down
5 changes: 4 additions & 1 deletion test/__snapshots__/terserOptions-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
",
}
`;

Expand Down
17 changes: 17 additions & 0 deletions test/minify-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit 52c1aef

Please sign in to comment.