From 391c1a98e86ec8ef6cb0007a0013f1286451a638 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 19 Nov 2022 16:25:10 +0100 Subject: [PATCH 1/2] Add test --- .../es6/retarget-namespace-single/index.js | 5 +++++ .../es6/retarget-namespace-single/library/a.js | 1 + .../es6/retarget-namespace-single/library/b.js | 1 + .../es6/retarget-namespace-single/library/c.js | 1 + .../retarget-namespace-single/library/package.json | 3 +++ .../core/integration-tests/test/scope-hoisting.js | 11 +++++++++++ packages/core/test-utils/src/utils.js | 2 +- 7 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/index.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/b.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/c.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/package.json diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/index.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/index.js new file mode 100644 index 00000000000..461f6e483ef --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/index.js @@ -0,0 +1,5 @@ +import { v } from "./library/a.js"; + +import * as y from "./library/a.js"; + +output = [v, sideEffectNoop(y).v]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/a.js new file mode 100644 index 00000000000..d165e999022 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/a.js @@ -0,0 +1 @@ +export * from "./b.js"; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/b.js new file mode 100644 index 00000000000..0e07370072f --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/b.js @@ -0,0 +1 @@ +export {v} from "./c.js"; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/c.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/c.js new file mode 100644 index 00000000000..098cc0c9e17 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/c.js @@ -0,0 +1 @@ +export const v = 123; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/package.json new file mode 100644 index 00000000000..1b95642997c --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/retarget-namespace-single/library/package.json @@ -0,0 +1,3 @@ +{ + "sideEffects": false +} diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index fcbe8e2b4d9..ec15fdaaeae 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -2224,6 +2224,17 @@ describe('scope hoisting', function () { ]); }); + it('should correctly retarget dependencies when both namespace and indvidual export are used', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/es6/retarget-namespace-single/index.js', + ), + ); + let output = await run(b); + assert.deepEqual(output, [123, 123]); + }); + it('should correctly handle circular dependencies', async function () { let b = await bundle( path.join(__dirname, '/integration/scope-hoisting/es6/circular/a.mjs'), diff --git a/packages/core/test-utils/src/utils.js b/packages/core/test-utils/src/utils.js index eb2788ccd3a..8a6a3c4ffcc 100644 --- a/packages/core/test-utils/src/utils.js +++ b/packages/core/test-utils/src/utils.js @@ -338,7 +338,7 @@ export async function runBundles( // A utility to prevent optimizers from removing side-effect-free code needed for testing // $FlowFixMe[prop-missing] - ctx.sideEffectNoop = () => {}; + ctx.sideEffectNoop = v => v; vm.createContext(ctx); let esmOutput; From acfe54b37beb06f5466ddbaaed288927790d3a5c Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 19 Nov 2022 16:28:22 +0100 Subject: [PATCH 2/2] Fix --- packages/core/core/src/BundleGraph.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index f1c3c9c277c..36a39888997 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -215,6 +215,10 @@ export default class BundleGraph { node.usedSymbolsUp.size > 0 && // Only perform rewriting if the dependency only points to a single asset (e.g. CSS modules) !hasAmbiguousSymbols && + // It doesn't make sense to retarget dependencies where `*` is used, because the + // retargeting won't enable any benefits in that case (apart from potentially even more + // code being generated). + !node.usedSymbolsUp.has('*') && // TODO We currently can't rename imports in async imports, e.g. from // (parcelRequire("...")).then(({ a }) => a); // to