Skip to content

Commit

Permalink
fix: Fix problems related to use rollup-plugin-terser
Browse files Browse the repository at this point in the history
1. The nameCache obtained in writebundle hook is not a reference to terseroptions
2. JSON.stringify(nameCache) requires converting regex to a string first.
  • Loading branch information
zyrong committed May 16, 2022
1 parent 6c17bb4 commit b0f2291
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 63 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-ravens-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Fix problems related to use rollup-plugin-terser
131 changes: 72 additions & 59 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,15 @@ function createConfig(options, entry, format, writeMeta) {

// let rollupName = safeVariableName(basename(entry).replace(/\.js$/, ''));

let nameCache = {};
const bareNameCache = nameCache;
// Support "minify" field and legacy "mangle" field via package.json:
const rawMinifyValue = options.pkg.minify || options.pkg.mangle || {};
let minifyOptions = typeof rawMinifyValue === 'string' ? {} : rawMinifyValue;
const getNameCachePath =
typeof rawMinifyValue === 'string'
? () => resolve(options.cwd, rawMinifyValue)
: () => resolve(options.cwd, 'mangle.json');
let minifyOptions = typeof rawMinifyValue === 'string' ? {} : rawMinifyValue;
let endsWithNewLine = false;
let terserOptions = {};

const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx';
const emitDeclaration =
Expand All @@ -411,29 +411,6 @@ function createConfig(options, entry, format, writeMeta) {
const externalTest =
external.length === 0 ? id => false : id => externalPredicate.test(id);

let endsWithNewLine = false;

function loadNameCache() {
try {
const data = fs.readFileSync(getNameCachePath(), 'utf8');
endsWithNewLine = data.endsWith(EOL);
nameCache = JSON.parse(data);
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
if (nameCache.minify) {
minifyOptions = Object.assign(
{},
minifyOptions || {},
nameCache.minify,
);
}
} catch (e) {}
}
loadNameCache();

normalizeMinifyOptions(minifyOptions);

if (nameCache === bareNameCache) nameCache = null;

/** @type {false | import('rollup').RollupCache} */
let cache;
if (modern) cache = false;
Expand Down Expand Up @@ -597,43 +574,79 @@ function createConfig(options, entry, format, writeMeta) {
},
}),
options.compress !== false && [
terser({
compress: Object.assign(
{
keep_infinity: true,
pure_getters: true,
// Ideally we'd just get Terser to respect existing Arrow functions...
// unsafe_arrows: true,
passes: 10,
},
typeof minifyOptions.compress === 'boolean'
? minifyOptions.compress
: minifyOptions.compress || {},
),
format: {
// By default, Terser wraps function arguments in extra parens to trigger eager parsing.
// Whether this is a good idea is way too specific to guess, so we optimize for size by default:
wrap_func_args: false,
comments: /^\s*([@#]__[A-Z]+__\s*$|@cc_on)/,
preserve_annotations: true,
},
module: modern,
ecma: modern ? 2017 : 5,
toplevel: modern || format === 'cjs' || format === 'es',
mangle:
typeof minifyOptions.mangle === 'boolean'
? minifyOptions.mangle
: Object.assign({}, minifyOptions.mangle || {}),
nameCache,
}),
nameCache && {
terser(terserOptions),
{
// before hook
options: loadNameCache,
options() {
let nameCache = {};
const bareNameCache = nameCache;

function loadNameCache() {
try {
const data = fs.readFileSync(getNameCachePath(), 'utf8');
endsWithNewLine = data.endsWith(EOL);
nameCache = JSON.parse(data);
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
if (nameCache.minify) {
minifyOptions = Object.assign(
{},
minifyOptions || {},
nameCache.minify,
);
}
} catch (e) {}
}
loadNameCache();

normalizeMinifyOptions(minifyOptions);

if (nameCache === bareNameCache) nameCache = null;

Object.entries({
compress: Object.assign(
{
keep_infinity: true,
pure_getters: true,
// Ideally we'd just get Terser to respect existing Arrow functions...
// unsafe_arrows: true,
passes: 10,
},
typeof minifyOptions.compress === 'boolean'
? minifyOptions.compress
: minifyOptions.compress || {},
),
format: {
// By default, Terser wraps function arguments in extra parens to trigger eager parsing.
// Whether this is a good idea is way too specific to guess, so we optimize for size by default:
wrap_func_args: false,
comments: /^\s*([@#]__[A-Z]+__\s*$|@cc_on)/,
preserve_annotations: true,
},
module: modern,
ecma: modern ? 2017 : 5,
toplevel: modern || format === 'cjs' || format === 'es',
mangle:
typeof minifyOptions.mangle === 'boolean'
? minifyOptions.mangle
: Object.assign({}, minifyOptions.mangle || {}),
nameCache,
}).forEach(([key, value]) => {
terserOptions[key] = value;
});
},
// after hook
writeBundle() {
if (writeMeta && nameCache) {
if (writeMeta && terserOptions.nameCache) {
try {
if (
terserOptions.nameCache.minify.mangle.properties.regex
) {
terserOptions.nameCache.minify.mangle.properties.regex =
terserOptions.nameCache.minify.mangle.properties.regex.source;
}
} catch (error) {}
let filename = getNameCachePath();
let json = JSON.stringify(nameCache, null, 2);
let json = JSON.stringify(terserOptions.nameCache, null, 2);
if (endsWithNewLine) json += EOL;
fs.writeFile(filename, json, () => {});
}
Expand Down
81 changes: 81 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,87 @@ exports[`fixtures build modern-generators with microbundle 6`] = `
"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 1`] = `
"Used script: microbundle
Directory tree:
multi-source-mangle-json-file
dist
a.esm.mjs
a.esm.mjs.map
a.js
a.js.map
a.umd.js
a.umd.js.map
b.esm.mjs
b.esm.mjs.map
b.js
b.js.map
b.umd.js
b.umd.js.map
mangle.json
package.json
src
a.js
a_prop.js
b.js
b_prop.js
Build \\"multi-source-mangle-json-file\\" to dist:
110 B: a.js.gz
76 B: a.js.br
112 B: a.esm.mjs.gz
84 B: a.esm.mjs.br
212 B: a.umd.js.gz
173 B: a.umd.js.br
107 B: b.js.gz
95 B: b.js.br
110 B: b.esm.mjs.gz
100 B: b.esm.mjs.br
209 B: b.umd.js.gz
170 B: b.umd.js.br"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 2`] = `12`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 3`] = `
"var o={prop1:1,__p2:2,o:4};function p(){return console.log(o.prop1),console.log(o.__p2),console.log(o.o),o}export default p;
//# sourceMappingURL=a.esm.mjs.map
"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 4`] = `
"var o={prop1:1,__p2:2,o:4};module.exports=function(){return console.log(o.prop1),console.log(o.__p2),console.log(o.o),o};
//# sourceMappingURL=a.js.map
"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 5`] = `
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=o():\\"function\\"==typeof define&&define.amd?define(o):(e||self).multiSourceMangleJsonFile=o()}(this,function(){var e={prop1:1,__p2:2,o:4};return function(){return console.log(e.prop1),console.log(e.__p2),console.log(e.o),e}});
//# sourceMappingURL=a.umd.js.map
"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 6`] = `
"var o={prop1:1,__p2:2,p:3};function p(){return console.log(o.prop1),console.log(o.__p2),o}export default p;
//# sourceMappingURL=b.esm.mjs.map
"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 7`] = `
"var o={prop1:1,__p2:2,p:3};module.exports=function(){return console.log(o.prop1),console.log(o.__p2),o};
//# sourceMappingURL=b.js.map
"
`;
exports[`fixtures build multi-source-mangle-json-file with microbundle 8`] = `
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=o():\\"function\\"==typeof define&&define.amd?define(o):(e||self).multiSourceMangleJsonFile=o()}(this,function(){var e={prop1:1,__p2:2,p:3};return function(){return console.log(e.prop1),console.log(e.__p2),e}});
//# sourceMappingURL=b.umd.js.map
"
`;
exports[`fixtures build name-custom-amd with microbundle 1`] = `
"Used script: microbundle
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/mangle-json-file/mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"minify": {
"mangle": {
"properties": {
"regex": "^_"
"regex": "^_",
"reserved": []
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/minify-path-config/minify.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"minify": {
"mangle": {
"properties": {
"regex": "^_"
"regex": "^_",
"reserved": []
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/minify-path-parent-dir-with-cwd/minify.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"minify": {
"mangle": {
"properties": {
"regex": "^_"
"regex": "^_",
"reserved": []
}
}
},
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/multi-source-mangle-json-file/mangle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"minify": {
"mangle": {
"properties": {
"regex": "^_",
"reserved": []
}
}
},
"props": {
"props": {
"$_prop2": "__p2",
"$_prop4": "o",
"$_prop3": "p"
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/multi-source-mangle-json-file/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "multi-source-mangle-json-file",
"source":[
"./src/a.js",
"./src/b.js"
]
}
8 changes: 8 additions & 0 deletions test/fixtures/multi-source-mangle-json-file/src/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { obj } from './a_prop';

export default function () {
console.log(obj.prop1);
console.log(obj._prop2);
console.log(obj._prop4);
return obj;
}
5 changes: 5 additions & 0 deletions test/fixtures/multi-source-mangle-json-file/src/a_prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const obj = {
prop1: 1,
_prop2: 2,
_prop4: 4,
};
7 changes: 7 additions & 0 deletions test/fixtures/multi-source-mangle-json-file/src/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { obj } from './b_prop';

export default function () {
console.log(obj.prop1);
console.log(obj._prop2);
return obj;
}
5 changes: 5 additions & 0 deletions test/fixtures/multi-source-mangle-json-file/src/b_prop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const obj = {
prop1: 1,
_prop2: 2,
_prop3: 3,
};
3 changes: 2 additions & 1 deletion test/fixtures/terser-annotations/mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"mangle": {
"keep_fnames": true
}
}
},
"props": {}
}

0 comments on commit b0f2291

Please sign in to comment.