From 17dd027a7da4a5e4a74cdf4a47a8d40c66639f0e Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Tue, 19 Apr 2022 21:49:15 +0200 Subject: [PATCH 1/2] Add test --- .../es6/import-namespace-wrapped-self/a.js | 4 ++++ .../es6/import-namespace-wrapped-self/b.js | 9 +++++++++ .../core/integration-tests/test/scope-hoisting.js | 12 ++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/b.js diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/a.js new file mode 100644 index 00000000000..0e8a5bac648 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/a.js @@ -0,0 +1,4 @@ +if (Date.now() > 0) { + const { f } = require("./b.js"); + output = f(); +} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/b.js new file mode 100644 index 00000000000..cb25c85b7b4 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/import-namespace-wrapped-self/b.js @@ -0,0 +1,9 @@ +import * as ns from "./b.js"; + +export function f() { + return foo(ns).f === f; +} + +function foo(x) { + return x; +} diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 39592f262c4..518a72f52e1 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -1245,6 +1245,18 @@ describe('scope hoisting', function () { assert.deepEqual(output, 1); }); + it('supports wrapped assets importing their own namespace', async function () { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/es6/import-namespace-wrapped-self/a.js', + ), + ); + + let output = await run(b); + assert.strictEqual(output, true); + }); + it('supports importing a namespace from a transpiled CommonJS module', async function () { let b = await bundle( path.join( From 29f07f3e8b868946bd28affc25ed1788423682d2 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Tue, 19 Apr 2022 21:53:53 +0200 Subject: [PATCH 2/2] Fix --- packages/packagers/js/src/ScopeHoistingPackager.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/packagers/js/src/ScopeHoistingPackager.js b/packages/packagers/js/src/ScopeHoistingPackager.js index c87451a3c19..661a1ee302d 100644 --- a/packages/packagers/js/src/ScopeHoistingPackager.js +++ b/packages/packagers/js/src/ScopeHoistingPackager.js @@ -806,7 +806,15 @@ ${code} if (imported === '*' || exportSymbol === '*' || isDefaultInterop) { // Resolve to the namespace object if requested or this is a CJS default interop reqiure. - return obj; + if ( + parentAsset === resolvedAsset && + this.wrappedAssets.has(resolvedAsset.id) + ) { + // Directly use module.exports for wrapped assets importing themselves. + return 'module.exports'; + } else { + return obj; + } } else if ( (!staticExports || isWrapped || !symbol) && resolvedAsset !== parentAsset