Skip to content

Commit 52c1aef

Browse files
fix: allow disable compress options for terser and swc (#514)
1 parent c5a9d41 commit 52c1aef

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

src/utils.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ async function terserMinify(
228228
...terserOptions,
229229
compress:
230230
typeof terserOptions.compress === "boolean"
231-
? {}
231+
? terserOptions.compress
232+
? {}
233+
: false
232234
: { ...terserOptions.compress },
233235
// ecma: terserOptions.ecma,
234236
// ie8: terserOptions.ie8,
@@ -281,17 +283,19 @@ async function terserMinify(
281283
);
282284
}
283285

284-
// More optimizations
285-
if (typeof terserOptions.compress.ecma === "undefined") {
286-
terserOptions.compress.ecma = terserOptions.ecma;
287-
}
286+
if (terserOptions.compress) {
287+
// More optimizations
288+
if (typeof terserOptions.compress.ecma === "undefined") {
289+
terserOptions.compress.ecma = terserOptions.ecma;
290+
}
288291

289-
// https://github.com/webpack/webpack/issues/16135
290-
if (
291-
terserOptions.ecma === 5 &&
292-
typeof terserOptions.compress.arrows === "undefined"
293-
) {
294-
terserOptions.compress.arrows = false;
292+
// https://github.com/webpack/webpack/issues/16135
293+
if (
294+
terserOptions.ecma === 5 &&
295+
typeof terserOptions.compress.arrows === "undefined"
296+
) {
297+
terserOptions.compress.arrows = false;
298+
}
295299
}
296300

297301
const [[filename, code]] = Object.entries(input);
@@ -558,7 +562,9 @@ async function swcMinify(input, sourceMap, minimizerOptions) {
558562
...swcOptions,
559563
compress:
560564
typeof swcOptions.compress === "boolean"
561-
? {}
565+
? swcOptions.compress
566+
? {}
567+
: false
562568
: { ...swcOptions.compress },
563569
mangle:
564570
swcOptions.mangle == null
@@ -588,17 +594,19 @@ async function swcMinify(input, sourceMap, minimizerOptions) {
588594
swcOptions.sourceMap = true;
589595
}
590596

591-
// More optimizations
592-
if (typeof swcOptions.compress.ecma === "undefined") {
593-
swcOptions.compress.ecma = swcOptions.ecma;
594-
}
597+
if (swcOptions.compress) {
598+
// More optimizations
599+
if (typeof swcOptions.compress.ecma === "undefined") {
600+
swcOptions.compress.ecma = swcOptions.ecma;
601+
}
595602

596-
// https://github.com/webpack/webpack/issues/16135
597-
if (
598-
swcOptions.ecma === 5 &&
599-
typeof swcOptions.compress.arrows === "undefined"
600-
) {
601-
swcOptions.compress.arrows = false;
603+
// https://github.com/webpack/webpack/issues/16135
604+
if (
605+
swcOptions.ecma === 5 &&
606+
typeof swcOptions.compress.arrows === "undefined"
607+
) {
608+
swcOptions.compress.arrows = false;
609+
}
602610
}
603611

604612
const [[filename, code]] = Object.entries(input);

test/__snapshots__/minify-option.test.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ exports[`minify option should work using when the \`minify\` option is \`terserM
178178
179179
exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and ECMA modules output: warnings 1`] = `Array []`;
180180
181+
exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to disable \`compress\` options: assets 1`] = `
182+
Object {
183+
"main.js": "/*! For license information please see main.js.LICENSE.txt */
184+
(()=>{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)})();",
185+
"main.js.LICENSE.txt": "/* @preserve*/
186+
",
187+
}
188+
`;
189+
190+
exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to disable \`compress\` options: errors 1`] = `Array []`;
191+
192+
exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to disable \`compress\` options: warnings 1`] = `Array []`;
193+
181194
exports[`minify option should work using when the \`minify\` option is \`terserMinify\` and allows to set \`terser\` options: assets 1`] = `
182195
Object {
183196
"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)})();",

test/__snapshots__/terserOptions-option.test.js.snap

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ exports[`terserOptions option should match snapshot for the "compress" option wi
1212

1313
exports[`terserOptions option should match snapshot for the "compress" option with the "false" value: assets 1`] = `
1414
Object {
15-
"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)})();",
15+
"main.js": "/*! For license information please see main.js.LICENSE.txt */
16+
(()=>{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)})();",
17+
"main.js.LICENSE.txt": "/* @preserve*/
18+
",
1619
}
1720
`;
1821

test/minify-option.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,23 @@ describe("minify option", () => {
447447
expect(getWarnings(stats)).toMatchSnapshot("warnings");
448448
});
449449

450+
it("should work using when the `minify` option is `terserMinify` and allows to disable `compress` options", async () => {
451+
const compiler = getCompiler();
452+
453+
new TerserPlugin({
454+
minify: TerserPlugin.terserMinify,
455+
terserOptions: {
456+
compress: false,
457+
},
458+
}).apply(compiler);
459+
460+
const stats = await compile(compiler);
461+
462+
expect(readsAssets(compiler, stats)).toMatchSnapshot("assets");
463+
expect(getErrors(stats)).toMatchSnapshot("errors");
464+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
465+
});
466+
450467
it("should work using when the `minify` option is `uglifyJsMinify`", async () => {
451468
const compiler = getCompiler({
452469
target: ["web", "es5"],

0 commit comments

Comments
 (0)