Skip to content

Commit

Permalink
fix lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jan 11, 2024
1 parent cdc742b commit b8c7d64
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
34 changes: 19 additions & 15 deletions packages/babel-helper-module-transforms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,27 @@ export function buildNamespaceInitStatements(
);
}
if (sourceMetadata.reexportAll) {
let exportNameListName;
if (metadata.implicitAssignmentExports === undefined) {
// __esModule, default
if (metadata.exportNameList.length > 2) {
exportNameListName =
metadata.programPath.scope.generateUidIdentifier("exportNames");
statements.push(
variableDeclaration("var", [
variableDeclarator(
exportNameListName,
valueToNode(metadata.exportNameList),
),
]),
);
}
}
const statement = buildNamespaceReexport(
metadata,
cloneNode(srcNamespace),
constantReexports,
exportNameListName,
buildExportStar,
);
statement.loc = sourceMetadata.reexportAll.loc;
Expand Down Expand Up @@ -404,25 +421,12 @@ function buildNamespaceReexport(
metadata: ModuleMetadata,
namespace: t.Expression,
constantReexports: boolean,
exportNameListName?: t.Identifier,
buildExportStar?: (body: t.Statement[]) => t.Statement,
) {
const { programPath, exportNameList } = metadata;

if (metadata.implicitAssignmentExports === undefined) {
let exportNameListName;
if (metadata.exportNameList.length > 2) {
exportNameListName =
programPath.scope.generateUidIdentifier("exportNames");
programPath.unshiftContainer(
"body",
variableDeclaration("var", [
variableDeclarator(
exportNameListName,
valueToNode(metadata.exportNameList),
),
]),
);
}
return (
constantReexports
? template.statement`
Expand Down Expand Up @@ -460,7 +464,7 @@ function buildNamespaceReexport(
VERIFY_NAME_LIST: exportNameListName
? template`
if (Object.prototype.hasOwnProperty.call(EXPORTS_LIST, key)) return;
`({ EXPORTS_LIST: exportNameListName })
`({ EXPORTS_LIST: t.cloneNode(exportNameListName) })
: null,
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "foo";

export var a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [["transform-modules-commonjs", { "lazy": true }]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.a = void 0;
var _foo = require("foo");
var _exportNames = ["default", "__esModule", "a"];
Object.keys(_foo).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _foo[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _foo[key];
}
});
});
var a;

0 comments on commit b8c7d64

Please sign in to comment.