Skip to content

Commit

Permalink
Fix compiling duplicate ns imports to lazy CommonJS (#15941)
Browse files Browse the repository at this point in the history
* Fix compiling duplicate ns imports to lazy CommonJS

* Better types

* whops
  • Loading branch information
nicolo-ribaudo committed Sep 7, 2023
1 parent b9a2244 commit ab36c3e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
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());

0 comments on commit ab36c3e

Please sign in to comment.