Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrapped assets importing their own namespace #7978

Merged
merged 3 commits into from Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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 @@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the asset is wrapped, won't obj already be a parcelRequire?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, we have this condition:

resolvedAsset !== parentAsset);

which will make isWrapped itself false in this self-import case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I don't remember why...

} else {
return obj;
}
} else if (
(!staticExports || isWrapped || !symbol) &&
resolvedAsset !== parentAsset
Expand Down