From 4580e5fac6879aecf7fd62d63dcb3687406fc50d Mon Sep 17 00:00:00 2001 From: zhaoyr <543434337@qq.com> Date: Mon, 16 May 2022 00:26:23 +0800 Subject: [PATCH 1/3] fix: css compilation problems when multiple entries --- .changeset/angry-worms-smell.md | 5 +++++ src/index.js | 10 +++------- test/fixtures/basic-multi-source-css/a.css | 5 +++++ test/fixtures/basic-multi-source-css/a.js | 7 +++++++ test/fixtures/basic-multi-source-css/b.css | 5 +++++ test/fixtures/basic-multi-source-css/b.js | 7 +++++++ test/fixtures/basic-multi-source-css/package.json | 7 +++++++ 7 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 .changeset/angry-worms-smell.md create mode 100644 test/fixtures/basic-multi-source-css/a.css create mode 100644 test/fixtures/basic-multi-source-css/a.js create mode 100644 test/fixtures/basic-multi-source-css/b.css create mode 100644 test/fixtures/basic-multi-source-css/b.js create mode 100644 test/fixtures/basic-multi-source-css/package.json diff --git a/.changeset/angry-worms-smell.md b/.changeset/angry-worms-smell.md new file mode 100644 index 00000000..7ec09757 --- /dev/null +++ b/.changeset/angry-worms-smell.md @@ -0,0 +1,5 @@ +--- +'microbundle': patch +--- + +Fix css compilation problems when multiple entries diff --git a/src/index.js b/src/index.js index a913c3b0..063efba4 100644 --- a/src/index.js +++ b/src/index.js @@ -111,12 +111,7 @@ export default async function microbundle(inputOptions) { for (let i = 0; i < options.entries.length; i++) { for (let j = 0; j < formats.length; j++) { steps.push( - createConfig( - options, - options.entries[i], - formats[j], - i === 0 && j === 0, - ), + createConfig(options, options.entries[i], formats[j], j === 0), ); } } @@ -333,6 +328,7 @@ function getMain({ options, entry, format }) { const shebang = {}; function createConfig(options, entry, format, writeMeta) { + const entryFileName = basename(entry, extname(entry)); let { pkg } = options; /** @type {(string|RegExp)[]} */ @@ -502,7 +498,7 @@ function createConfig(options, entry, format, writeMeta) { extract: !!writeMeta && options.css !== 'inline' && - options.output.replace(EXTENSION, '.css'), + resolve(outputDir, `${entryFileName}.css`), minimize: options.compress, sourceMap: options.sourcemap && options.css !== 'inline', }), diff --git a/test/fixtures/basic-multi-source-css/a.css b/test/fixtures/basic-multi-source-css/a.css new file mode 100644 index 00000000..317852fd --- /dev/null +++ b/test/fixtures/basic-multi-source-css/a.css @@ -0,0 +1,5 @@ +.a { + display: flex; + color: green; + background: pink; +} diff --git a/test/fixtures/basic-multi-source-css/a.js b/test/fixtures/basic-multi-source-css/a.js new file mode 100644 index 00000000..44b08a5e --- /dev/null +++ b/test/fixtures/basic-multi-source-css/a.js @@ -0,0 +1,7 @@ +import './a.css'; + +export default function () { + const el = document.createElement('div'); + el.className = 'a'; + return el; +} diff --git a/test/fixtures/basic-multi-source-css/b.css b/test/fixtures/basic-multi-source-css/b.css new file mode 100644 index 00000000..36de7313 --- /dev/null +++ b/test/fixtures/basic-multi-source-css/b.css @@ -0,0 +1,5 @@ +.b { + display: flex; + color: red; + background: blue; +} diff --git a/test/fixtures/basic-multi-source-css/b.js b/test/fixtures/basic-multi-source-css/b.js new file mode 100644 index 00000000..2d21df05 --- /dev/null +++ b/test/fixtures/basic-multi-source-css/b.js @@ -0,0 +1,7 @@ +import './b.css'; + +export default function () { + const el = document.createElement('div'); + el.className = 'b'; + return el; +} diff --git a/test/fixtures/basic-multi-source-css/package.json b/test/fixtures/basic-multi-source-css/package.json new file mode 100644 index 00000000..c0344448 --- /dev/null +++ b/test/fixtures/basic-multi-source-css/package.json @@ -0,0 +1,7 @@ +{ + "name": "basic-multi-source-css", + "source":[ + "./a.js", + "./b.js" + ] +} From 6c17bb47debecea4122b1c2ebb1078d1f8833d8d Mon Sep 17 00:00:00 2001 From: zhaoyr <543434337@qq.com> Date: Mon, 16 May 2022 12:53:46 +0800 Subject: [PATCH 2/3] test: update test snapshots --- test/__snapshots__/index.test.js.snap | 159 ++++++++++++++++++++------ 1 file changed, 126 insertions(+), 33 deletions(-) diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 0e44f6ea..30b3a9ad 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -432,14 +432,14 @@ Directory tree: basic-css dist - basic-css.css - basic-css.css.map basic-css.esm.mjs basic-css.esm.mjs.map basic-css.js basic-css.js.map basic-css.umd.js basic-css.umd.js.map + index.css + index.css.map package.json src index.js @@ -458,28 +458,28 @@ Build \\"basic-css\\" to dist: exports[`fixtures build basic-css with microbundle 2`] = `8`; exports[`fixtures build basic-css with microbundle 3`] = ` -".testing{display:flex;color:red;background:#00f} -/*# sourceMappingURL=basic-css.css.map */" -`; - -exports[`fixtures build basic-css with microbundle 4`] = ` "function e(){var e=document.createElement(\\"div\\");return e.className=\\"testing\\",e}export default e; //# sourceMappingURL=basic-css.esm.mjs.map " `; -exports[`fixtures build basic-css with microbundle 5`] = ` +exports[`fixtures build basic-css with microbundle 4`] = ` "module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"testing\\",e}; //# sourceMappingURL=basic-css.js.map " `; -exports[`fixtures build basic-css with microbundle 6`] = ` +exports[`fixtures build basic-css with microbundle 5`] = ` "!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).basicCss=n()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"testing\\",e}}); //# sourceMappingURL=basic-css.umd.js.map " `; +exports[`fixtures build basic-css with microbundle 6`] = ` +".testing{display:flex;color:red;background:#00f} +/*# sourceMappingURL=index.css.map */" +`; + exports[`fixtures build basic-dashed-external with microbundle 1`] = ` "Used script: microbundle @@ -701,6 +701,99 @@ exports[`fixtures build basic-multi-source with microbundle 8`] = ` " `; +exports[`fixtures build basic-multi-source-css with microbundle 1`] = ` +"Used script: microbundle + +Directory tree: + +basic-multi-source-css + a.css + a.js + b.css + b.js + dist + a.css + a.css.map + a.esm.mjs + a.esm.mjs.map + a.js + a.js.map + a.umd.js + a.umd.js.map + b.css + b.css.map + b.esm.mjs + b.esm.mjs.map + b.js + b.js.map + b.umd.js + b.umd.js.map + package.json + + +Build \\"basic-multi-source-css\\" to dist: +104 B: a.js.gz +61 B: a.js.br +105 B: a.esm.mjs.gz +67 B: a.esm.mjs.br +201 B: a.umd.js.gz +133 B: a.umd.js.br +104 B: b.js.gz +61 B: b.js.br +106 B: b.esm.mjs.gz +67 B: b.esm.mjs.br +201 B: b.umd.js.gz +134 B: b.umd.js.br" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 2`] = `16`; + +exports[`fixtures build basic-multi-source-css with microbundle 3`] = ` +".a{display:flex;color:green;background:pink} +/*# sourceMappingURL=a.css.map */" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 4`] = ` +"function e(){var e=document.createElement(\\"div\\");return e.className=\\"a\\",e}export default e; +//# sourceMappingURL=a.esm.mjs.map +" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 5`] = ` +"module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"a\\",e}; +//# sourceMappingURL=a.js.map +" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 6`] = ` +"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).basicMultiSourceCss=n()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"a\\",e}}); +//# sourceMappingURL=a.umd.js.map +" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 7`] = ` +".b{display:flex;color:red;background:#00f} +/*# sourceMappingURL=b.css.map */" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 8`] = ` +"function e(){var e=document.createElement(\\"div\\");return e.className=\\"b\\",e}export default e; +//# sourceMappingURL=b.esm.mjs.map +" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 9`] = ` +"module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"b\\",e}; +//# sourceMappingURL=b.js.map +" +`; + +exports[`fixtures build basic-multi-source-css with microbundle 10`] = ` +"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).basicMultiSourceCss=n()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"b\\",e}}); +//# sourceMappingURL=b.umd.js.map +" +`; + exports[`fixtures build basic-no-compress with microbundle 1`] = ` "Used script: microbundle --no-compress @@ -1194,10 +1287,10 @@ Directory tree: css-modules--false dist - css-modules--false.css css-modules--false.esm.mjs css-modules--false.js css-modules--false.umd.js + index.css package.json src index.js @@ -1216,23 +1309,23 @@ Build \\"css-modules--false\\" to dist: exports[`fixtures build css-modules--false with microbundle 2`] = `4`; -exports[`fixtures build css-modules--false with microbundle 3`] = `"body{display:flex;color:red;background:#00f}.test_class_that_shouldnt_be_scoped{background-color:#00f}.not_scoped_class{color:pink}"`; - -exports[`fixtures build css-modules--false with microbundle 4`] = ` +exports[`fixtures build css-modules--false with microbundle 3`] = ` "function t(){}export default t; " `; -exports[`fixtures build css-modules--false with microbundle 5`] = ` +exports[`fixtures build css-modules--false with microbundle 4`] = ` "module.exports=function(){}; " `; -exports[`fixtures build css-modules--false with microbundle 6`] = ` +exports[`fixtures build css-modules--false with microbundle 5`] = ` "!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=function(){}:\\"function\\"==typeof define&&define.amd?define(function(){return function(){}}):(e||self).cssModulesFalse=function(){}}(this); " `; +exports[`fixtures build css-modules--false with microbundle 6`] = `"body{display:flex;color:red;background:#00f}.test_class_that_shouldnt_be_scoped{background-color:#00f}.not_scoped_class{color:pink}"`; + exports[`fixtures build css-modules--null with microbundle 1`] = ` "Used script: microbundle --no-sourcemap @@ -1240,10 +1333,10 @@ Directory tree: css-modules--null dist - css-modules--null.css css-modules--null.esm.mjs css-modules--null.js css-modules--null.umd.js + index.css package.json src index.js @@ -1262,23 +1355,23 @@ Build \\"css-modules--null\\" to dist: exports[`fixtures build css-modules--null with microbundle 2`] = `4`; -exports[`fixtures build css-modules--null with microbundle 3`] = `"body{display:flex;color:red;background:#00f}.test_class_that_shouldnt_be_scoped{background-color:#00f}._2kWDE{color:pink}"`; - -exports[`fixtures build css-modules--null with microbundle 4`] = ` +exports[`fixtures build css-modules--null with microbundle 3`] = ` "function e(){var e=document.createElement(\\"div\\");return e.className=\\"_2kWDE\\",e}export default e; " `; -exports[`fixtures build css-modules--null with microbundle 5`] = ` +exports[`fixtures build css-modules--null with microbundle 4`] = ` "module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"_2kWDE\\",e}; " `; -exports[`fixtures build css-modules--null with microbundle 6`] = ` +exports[`fixtures build css-modules--null with microbundle 5`] = ` "!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).cssModulesNull=n()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"_2kWDE\\",e}}); " `; +exports[`fixtures build css-modules--null with microbundle 6`] = `"body{display:flex;color:red;background:#00f}.test_class_that_shouldnt_be_scoped{background-color:#00f}._2kWDE{color:pink}"`; + exports[`fixtures build css-modules--string with microbundle 1`] = ` "Used script: microbundle --no-sourcemap --css-modules '_contains_this_[hash]' @@ -1286,10 +1379,10 @@ Directory tree: css-modules--string dist - css-modules--string.css css-modules--string.esm.mjs css-modules--string.js css-modules--string.umd.js + index.css package.json src index.js @@ -1308,23 +1401,23 @@ Build \\"css-modules--string\\" to dist: exports[`fixtures build css-modules--string with microbundle 2`] = `4`; -exports[`fixtures build css-modules--string with microbundle 3`] = `"body{display:flex;color:red;background:#00f}._contains_this_81567d0efc15a456670452d3277e1a68{background-color:#00f}._contains_this_0a8c24df242c2cd708036873307aea94{color:pink}"`; - -exports[`fixtures build css-modules--string with microbundle 4`] = ` +exports[`fixtures build css-modules--string with microbundle 3`] = ` "function a(){var a=document.createElement(\\"div\\");return a.className=\\"_contains_this_0a8c24df242c2cd708036873307aea94 _contains_this_81567d0efc15a456670452d3277e1a68\\",a}export default a; " `; -exports[`fixtures build css-modules--string with microbundle 5`] = ` +exports[`fixtures build css-modules--string with microbundle 4`] = ` "module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"_contains_this_0a8c24df242c2cd708036873307aea94 _contains_this_81567d0efc15a456670452d3277e1a68\\",e}; " `; -exports[`fixtures build css-modules--string with microbundle 6`] = ` +exports[`fixtures build css-modules--string with microbundle 5`] = ` "!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).cssModulesString=n()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"_contains_this_0a8c24df242c2cd708036873307aea94 _contains_this_81567d0efc15a456670452d3277e1a68\\",e}}); " `; +exports[`fixtures build css-modules--string with microbundle 6`] = `"body{display:flex;color:red;background:#00f}._contains_this_81567d0efc15a456670452d3277e1a68{background-color:#00f}._contains_this_0a8c24df242c2cd708036873307aea94{color:pink}"`; + exports[`fixtures build css-modules--true with microbundle 1`] = ` "Used script: microbundle --no-sourcemap --css-modules true @@ -1332,10 +1425,10 @@ Directory tree: css-modules--true dist - css-modules--true.css css-modules--true.esm.mjs css-modules--true.js css-modules--true.umd.js + index.css package.json src index.js @@ -1354,23 +1447,23 @@ Build \\"css-modules--true\\" to dist: exports[`fixtures build css-modules--true with microbundle 2`] = `4`; -exports[`fixtures build css-modules--true with microbundle 3`] = `"body{display:flex;color:red;background:#00f}.test_class_that_should_be_scoped{background-color:#00f}._2kWDE{color:pink}"`; - -exports[`fixtures build css-modules--true with microbundle 4`] = ` +exports[`fixtures build css-modules--true with microbundle 3`] = ` "function e(){var e=document.createElement(\\"div\\");return e.className=\\"_2kWDE \\"+(void 0).test_class_that_should_be_scoped,e}export default e; " `; -exports[`fixtures build css-modules--true with microbundle 5`] = ` +exports[`fixtures build css-modules--true with microbundle 4`] = ` "module.exports=function(){var e=document.createElement(\\"div\\");return e.className=\\"_2kWDE \\"+(void 0).test_class_that_should_be_scoped,e}; " `; -exports[`fixtures build css-modules--true with microbundle 6`] = ` +exports[`fixtures build css-modules--true with microbundle 5`] = ` "!function(e,t){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=t():\\"function\\"==typeof define&&define.amd?define(t):(e||self).cssModulesTrue=t()}(this,function(){return function(){var e=document.createElement(\\"div\\");return e.className=\\"_2kWDE \\"+(void 0).test_class_that_should_be_scoped,e}}); " `; +exports[`fixtures build css-modules--true with microbundle 6`] = `"body{display:flex;color:red;background:#00f}.test_class_that_should_be_scoped{background-color:#00f}._2kWDE{color:pink}"`; + exports[`fixtures build custom-babelrc with microbundle 1`] = ` "Used script: microbundle From b0f22912ad4e6f215f272202f528311a38643be8 Mon Sep 17 00:00:00 2001 From: zhaoyr <543434337@qq.com> Date: Mon, 16 May 2022 13:12:14 +0800 Subject: [PATCH 3/3] fix: Fix problems related to use rollup-plugin-terser 1. The nameCache obtained in writebundle hook is not a reference to terseroptions 2. JSON.stringify(nameCache) requires converting regex to a string first. --- .changeset/bright-ravens-rule.md | 5 + src/index.js | 131 ++++++++++-------- test/__snapshots__/index.test.js.snap | 81 +++++++++++ test/fixtures/mangle-json-file/mangle.json | 3 +- test/fixtures/minify-path-config/minify.json | 3 +- .../minify.json | 3 +- .../multi-source-mangle-json-file/mangle.json | 17 +++ .../package.json | 7 + .../multi-source-mangle-json-file/src/a.js | 8 ++ .../src/a_prop.js | 5 + .../multi-source-mangle-json-file/src/b.js | 7 + .../src/b_prop.js | 5 + test/fixtures/terser-annotations/mangle.json | 3 +- 13 files changed, 215 insertions(+), 63 deletions(-) create mode 100644 .changeset/bright-ravens-rule.md create mode 100644 test/fixtures/multi-source-mangle-json-file/mangle.json create mode 100644 test/fixtures/multi-source-mangle-json-file/package.json create mode 100644 test/fixtures/multi-source-mangle-json-file/src/a.js create mode 100644 test/fixtures/multi-source-mangle-json-file/src/a_prop.js create mode 100644 test/fixtures/multi-source-mangle-json-file/src/b.js create mode 100644 test/fixtures/multi-source-mangle-json-file/src/b_prop.js diff --git a/.changeset/bright-ravens-rule.md b/.changeset/bright-ravens-rule.md new file mode 100644 index 00000000..ef8aa680 --- /dev/null +++ b/.changeset/bright-ravens-rule.md @@ -0,0 +1,5 @@ +--- +'microbundle': patch +--- + +Fix problems related to use rollup-plugin-terser diff --git a/src/index.js b/src/index.js index 063efba4..19f88f8d 100644 --- a/src/index.js +++ b/src/index.js @@ -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 = @@ -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; @@ -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, () => {}); } diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 30b3a9ad..468a3586 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -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 diff --git a/test/fixtures/mangle-json-file/mangle.json b/test/fixtures/mangle-json-file/mangle.json index f95de54f..d8003ddc 100644 --- a/test/fixtures/mangle-json-file/mangle.json +++ b/test/fixtures/mangle-json-file/mangle.json @@ -2,7 +2,8 @@ "minify": { "mangle": { "properties": { - "regex": "^_" + "regex": "^_", + "reserved": [] } } }, diff --git a/test/fixtures/minify-path-config/minify.json b/test/fixtures/minify-path-config/minify.json index f95de54f..d8003ddc 100644 --- a/test/fixtures/minify-path-config/minify.json +++ b/test/fixtures/minify-path-config/minify.json @@ -2,7 +2,8 @@ "minify": { "mangle": { "properties": { - "regex": "^_" + "regex": "^_", + "reserved": [] } } }, diff --git a/test/fixtures/minify-path-parent-dir-with-cwd/minify.json b/test/fixtures/minify-path-parent-dir-with-cwd/minify.json index f95de54f..d8003ddc 100644 --- a/test/fixtures/minify-path-parent-dir-with-cwd/minify.json +++ b/test/fixtures/minify-path-parent-dir-with-cwd/minify.json @@ -2,7 +2,8 @@ "minify": { "mangle": { "properties": { - "regex": "^_" + "regex": "^_", + "reserved": [] } } }, diff --git a/test/fixtures/multi-source-mangle-json-file/mangle.json b/test/fixtures/multi-source-mangle-json-file/mangle.json new file mode 100644 index 00000000..50bda52d --- /dev/null +++ b/test/fixtures/multi-source-mangle-json-file/mangle.json @@ -0,0 +1,17 @@ +{ + "minify": { + "mangle": { + "properties": { + "regex": "^_", + "reserved": [] + } + } + }, + "props": { + "props": { + "$_prop2": "__p2", + "$_prop4": "o", + "$_prop3": "p" + } + } +} diff --git a/test/fixtures/multi-source-mangle-json-file/package.json b/test/fixtures/multi-source-mangle-json-file/package.json new file mode 100644 index 00000000..02e98cbe --- /dev/null +++ b/test/fixtures/multi-source-mangle-json-file/package.json @@ -0,0 +1,7 @@ +{ + "name": "multi-source-mangle-json-file", + "source":[ + "./src/a.js", + "./src/b.js" + ] +} diff --git a/test/fixtures/multi-source-mangle-json-file/src/a.js b/test/fixtures/multi-source-mangle-json-file/src/a.js new file mode 100644 index 00000000..6fadbf62 --- /dev/null +++ b/test/fixtures/multi-source-mangle-json-file/src/a.js @@ -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; +} diff --git a/test/fixtures/multi-source-mangle-json-file/src/a_prop.js b/test/fixtures/multi-source-mangle-json-file/src/a_prop.js new file mode 100644 index 00000000..95464feb --- /dev/null +++ b/test/fixtures/multi-source-mangle-json-file/src/a_prop.js @@ -0,0 +1,5 @@ +export const obj = { + prop1: 1, + _prop2: 2, + _prop4: 4, +}; diff --git a/test/fixtures/multi-source-mangle-json-file/src/b.js b/test/fixtures/multi-source-mangle-json-file/src/b.js new file mode 100644 index 00000000..75ff3fe7 --- /dev/null +++ b/test/fixtures/multi-source-mangle-json-file/src/b.js @@ -0,0 +1,7 @@ +import { obj } from './b_prop'; + +export default function () { + console.log(obj.prop1); + console.log(obj._prop2); + return obj; +} diff --git a/test/fixtures/multi-source-mangle-json-file/src/b_prop.js b/test/fixtures/multi-source-mangle-json-file/src/b_prop.js new file mode 100644 index 00000000..b94223a1 --- /dev/null +++ b/test/fixtures/multi-source-mangle-json-file/src/b_prop.js @@ -0,0 +1,5 @@ +export const obj = { + prop1: 1, + _prop2: 2, + _prop3: 3, +}; diff --git a/test/fixtures/terser-annotations/mangle.json b/test/fixtures/terser-annotations/mangle.json index 1893c2de..331f3d31 100644 --- a/test/fixtures/terser-annotations/mangle.json +++ b/test/fixtures/terser-annotations/mangle.json @@ -3,5 +3,6 @@ "mangle": { "keep_fnames": true } - } + }, + "props": {} } \ No newline at end of file