Skip to content

Commit

Permalink
Fix wrapped assets importing their own namespace (#7978)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Apr 21, 2022
1 parent 2f39f29 commit e1b55c6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
@@ -0,0 +1,4 @@
if (Date.now() > 0) {
const { f } = require("./b.js");
output = f();
}
@@ -0,0 +1,9 @@
import * as ns from "./b.js";

export function f() {
return foo(ns).f === f;
}

function foo(x) {
return x;
}
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -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(
Expand Down
10 changes: 9 additions & 1 deletion packages/packagers/js/src/ScopeHoistingPackager.js
Expand Up @@ -815,7 +815,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
Expand Down

0 comments on commit e1b55c6

Please sign in to comment.