From 3534815ddabecc080cdec42cd1f6009a81a48ec9 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 11 Jun 2021 15:36:21 -0400 Subject: [PATCH] Bugfix: preserve Terser annotations during Babel pass (#848) * Bugfix: preserve Terser annotations during Babel pass We were stripping out everything except `__PURE__`, but Terser has a number of annotations using that format - these were previously being ignored. * Create late-bags-argue.md * Add test for Terser annotations --- .changeset/late-bags-argue.md | 5 ++ src/lib/babel-custom.js | 2 +- test/__snapshots__/index.test.js.snap | 48 +++++++++++++++++++ test/fixtures/terser-annotations/mangle.json | 7 +++ test/fixtures/terser-annotations/package.json | 3 ++ test/fixtures/terser-annotations/src/index.js | 13 +++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 .changeset/late-bags-argue.md create mode 100644 test/fixtures/terser-annotations/mangle.json create mode 100644 test/fixtures/terser-annotations/package.json create mode 100644 test/fixtures/terser-annotations/src/index.js diff --git a/.changeset/late-bags-argue.md b/.changeset/late-bags-argue.md new file mode 100644 index 0000000..0f22180 --- /dev/null +++ b/.changeset/late-bags-argue.md @@ -0,0 +1,5 @@ +--- +"microbundle": patch +--- + +Bugfix: preserve Terser annotations like `/*@__NOINLINE__*/` during Babel pass diff --git a/src/lib/babel-custom.js b/src/lib/babel-custom.js index 00ffb2d..a79dc10 100644 --- a/src/lib/babel-custom.js +++ b/src/lib/babel-custom.js @@ -196,7 +196,7 @@ export default () => { babelOptions.generatorOpts = { minified: true, compact: true, - shouldPrintComment: comment => /[@#]__PURE__/.test(comment), + shouldPrintComment: comment => /[@#]__[A-Z]+__/.test(comment), }; } diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index fe22ceb..0fd65c5 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -2619,6 +2619,54 @@ exports[`fixtures build shebang with microbundle 5`] = ` " `; +exports[`fixtures build terser-annotations with microbundle 1`] = ` +"Used script: microbundle + +Directory tree: + +terser-annotations + dist + terser-annotations.esm.js + terser-annotations.esm.js.map + terser-annotations.js + terser-annotations.js.map + terser-annotations.umd.js + terser-annotations.umd.js.map + mangle.json + package.json + src + index.js + + +Build \\"terserAnnotations\\" to dist: +112 B: terser-annotations.js.gz +86 B: terser-annotations.js.br +117 B: terser-annotations.esm.js.gz +88 B: terser-annotations.esm.js.br +200 B: terser-annotations.umd.js.gz +159 B: terser-annotations.umd.js.br" +`; + +exports[`fixtures build terser-annotations with microbundle 2`] = `6`; + +exports[`fixtures build terser-annotations with microbundle 3`] = ` +"function shouldBePreserved(e,n){return e-n}function main(e,n){return{inlined:function(e,n){return e+n}(e,n),preserved:shouldBePreserved(e,n)}}export default main; +//# sourceMappingURL=terser-annotations.esm.js.map +" +`; + +exports[`fixtures build terser-annotations with microbundle 4`] = ` +"function shouldBePreserved(e,r){return e-r}module.exports=function(e,r){return{inlined:function(e,r){return e+r}(e,r),preserved:shouldBePreserved(e,r)}}; +//# sourceMappingURL=terser-annotations.js.map +" +`; + +exports[`fixtures build terser-annotations with microbundle 5`] = ` +"!function(e,n){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?module.exports=n():\\"function\\"==typeof define&&define.amd?define(n):(e||self).terserAnnotations=n()}(this,function(){function shouldBePreserved(e,n){return e-n}return function(e,n){return{inlined:function(e,n){return e+n}(e,n),preserved:shouldBePreserved(e,n)}}}); +//# sourceMappingURL=terser-annotations.umd.js.map +" +`; + exports[`fixtures build ts-custom-declaration with microbundle 1`] = ` "Used script: microbundle --generateTypes false diff --git a/test/fixtures/terser-annotations/mangle.json b/test/fixtures/terser-annotations/mangle.json new file mode 100644 index 0000000..1893c2d --- /dev/null +++ b/test/fixtures/terser-annotations/mangle.json @@ -0,0 +1,7 @@ +{ + "minify": { + "mangle": { + "keep_fnames": true + } + } +} \ No newline at end of file diff --git a/test/fixtures/terser-annotations/package.json b/test/fixtures/terser-annotations/package.json new file mode 100644 index 0000000..85bea0b --- /dev/null +++ b/test/fixtures/terser-annotations/package.json @@ -0,0 +1,3 @@ +{ + "name": "terser-annotations" +} diff --git a/test/fixtures/terser-annotations/src/index.js b/test/fixtures/terser-annotations/src/index.js new file mode 100644 index 0000000..16c6811 --- /dev/null +++ b/test/fixtures/terser-annotations/src/index.js @@ -0,0 +1,13 @@ +function shouldBeInlined(a, b) { + return a + b; +} + +function shouldBePreserved(a, b) { + return a - b; +} + +export default function main(a, b) { + const inlined = /*@__INLINE__*/ shouldBeInlined(a, b); + const preserved = /*@__NOINLINE__*/ shouldBePreserved(a, b); + return { inlined, preserved }; +}