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 compiling duplicate ns imports to lazy CommonJS #15941

Merged
merged 3 commits into from
Sep 7, 2023
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
10 changes: 7 additions & 3 deletions packages/babel-helper-module-transforms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ export function buildNamespaceInitStatements(
) {
const statements = [];

let srcNamespace: t.Node = identifier(sourceMetadata.name);
if (sourceMetadata.lazy) srcNamespace = callExpression(srcNamespace, []);
const srcNamespaceId = identifier(sourceMetadata.name);

for (const localName of sourceMetadata.importsNamespace) {
if (localName === sourceMetadata.name) continue;
Expand All @@ -221,10 +220,15 @@ export function buildNamespaceInitStatements(
statements.push(
template.statement`var NAME = SOURCE;`({
NAME: localName,
SOURCE: cloneNode(srcNamespace),
SOURCE: cloneNode(srcNamespaceId),
}),
);
}

const srcNamespace = sourceMetadata.lazy
? callExpression(srcNamespaceId, [])
: srcNamespaceId;

if (constantReexports) {
statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, true));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as foo from "foo";
import { bar } from "foo";

console.log(foo, bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

function foo() {
const data = babelHelpers.interopRequireWildcard(require("foo"));
foo = function () {
return data;
};
return data;
}
console.log(foo(), foo().bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as foo from "foo";
import * as foo2 from "foo";

console.log(foo, foo2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

function foo() {
const data = babelHelpers.interopRequireWildcard(require("foo"));
foo = function () {
return data;
};
return data;
}
var foo2 = foo;
console.log(foo(), foo2());