Skip to content

Commit b1a6374

Browse files
Fix: Support booleans in minify options (#858)
* Add fixture * Prevent boolean minify options from being replaced * Update snapshots * Add changeset
1 parent 8108800 commit b1a6374

File tree

7 files changed

+81
-2
lines changed

7 files changed

+81
-2
lines changed

.changeset/rude-books-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'microbundle': patch
3+
---
4+
5+
- Allow the minify options `compress` and `mangle` to be set as booleans

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,9 @@ function createConfig(options, entry, format, writeMeta) {
581581
// unsafe_arrows: true,
582582
passes: 10,
583583
},
584-
minifyOptions.compress || {},
584+
typeof minifyOptions.compress === 'boolean'
585+
? minifyOptions.compress
586+
: minifyOptions.compress || {},
585587
),
586588
format: {
587589
// By default, Terser wraps function arguments in extra parens to trigger eager parsing.
@@ -593,7 +595,10 @@ function createConfig(options, entry, format, writeMeta) {
593595
module: modern,
594596
ecma: modern ? 2017 : 5,
595597
toplevel: modern || format === 'cjs' || format === 'es',
596-
mangle: Object.assign({}, minifyOptions.mangle || {}),
598+
mangle:
599+
typeof minifyOptions.mangle === 'boolean'
600+
? minifyOptions.mangle
601+
: Object.assign({}, minifyOptions.mangle || {}),
597602
nameCache,
598603
}),
599604
nameCache && {

src/lib/terser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Normalize Terser options from microbundle's relaxed JSON format (mutates argument in-place)
22
export function normalizeMinifyOptions(minifyOptions) {
3+
// ignore normalization if "mangle" is a boolean:
4+
if (typeof minifyOptions.mangle === 'boolean') return;
5+
36
const mangle = minifyOptions.mangle || (minifyOptions.mangle = {});
47
let properties = mangle.properties;
58

test/__snapshots__/index.test.js.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,54 @@ exports[`fixtures build minify-config with microbundle 5`] = `
19151915
"
19161916
`;
19171917
1918+
exports[`fixtures build minify-config-boolean with microbundle 1`] = `
1919+
"Used script: microbundle
1920+
1921+
Directory tree:
1922+
1923+
minify-config-boolean
1924+
dist
1925+
minify-config-boolean.esm.js
1926+
minify-config-boolean.esm.js.map
1927+
minify-config-boolean.js
1928+
minify-config-boolean.js.map
1929+
minify-config-boolean.umd.js
1930+
minify-config-boolean.umd.js.map
1931+
package.json
1932+
src
1933+
index.js
1934+
two.js
1935+
1936+
1937+
Build \\"minifyConfigBoolean\\" to dist:
1938+
107 B: minify-config-boolean.js.gz
1939+
84 B: minify-config-boolean.js.br
1940+
114 B: minify-config-boolean.esm.js.gz
1941+
90 B: minify-config-boolean.esm.js.br
1942+
216 B: minify-config-boolean.umd.js.gz
1943+
159 B: minify-config-boolean.umd.js.br"
1944+
`;
1945+
1946+
exports[`fixtures build minify-config-boolean with microbundle 2`] = `6`;
1947+
1948+
exports[`fixtures build minify-config-boolean with microbundle 3`] = `
1949+
"var two={prop1:1,_prop2:2};function index(){return console.log(two.prop1),console.log(two._prop2),two}export default index;
1950+
//# sourceMappingURL=minify-config-boolean.esm.js.map
1951+
"
1952+
`;
1953+
1954+
exports[`fixtures build minify-config-boolean with microbundle 4`] = `
1955+
"var two={prop1:1,_prop2:2};module.exports=function(){return console.log(two.prop1),console.log(two._prop2),two};
1956+
//# sourceMappingURL=minify-config-boolean.js.map
1957+
"
1958+
`;
1959+
1960+
exports[`fixtures build minify-config-boolean with microbundle 5`] = `
1961+
"!function(global,factory){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=factory():\\"function\\"==typeof define&&define.amd?define(factory):(global||self).minifyConfigBoolean=factory()}(this,function(){var two={prop1:1,_prop2:2};return function(){return console.log(two.prop1),console.log(two._prop2),two}});
1962+
//# sourceMappingURL=minify-config-boolean.umd.js.map
1963+
"
1964+
`;
1965+
19181966
exports[`fixtures build minify-path-config with microbundle 1`] = `
19191967
"Used script: microbundle
19201968
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "minify-config-boolean",
3+
"minify": {
4+
"compress": true,
5+
"mangle": false
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { two } from './two';
2+
3+
export default function () {
4+
console.log(two.prop1);
5+
console.log(two._prop2);
6+
return two;
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const two = {
2+
prop1: 1,
3+
_prop2: 2,
4+
};

0 commit comments

Comments
 (0)