fix: systemjs - hoist named function exports (#3594)
This commit is contained in:
committed by
Henry Zhu
parent
dce9ee9d80
commit
83522c9cb5
@@ -74,6 +74,7 @@ export default function ({ types: t }) {
|
||||
let setters = [];
|
||||
let sources = [];
|
||||
let variableIds = [];
|
||||
let removedPaths = [];
|
||||
|
||||
function addExportName(key, val) {
|
||||
exportNames[key] = exportNames[key] || [];
|
||||
@@ -105,7 +106,7 @@ export default function ({ types: t }) {
|
||||
for (let path of body) {
|
||||
if (canHoist && path.isFunctionDeclaration()) {
|
||||
beforeBody.push(path.node);
|
||||
path.remove();
|
||||
removedPaths.push(path);
|
||||
} else if (path.isImportDeclaration()) {
|
||||
let source = path.node.source.value;
|
||||
pushModule(source, "imports", path.node.specifiers);
|
||||
@@ -135,7 +136,7 @@ export default function ({ types: t }) {
|
||||
path.replaceWithMultiple(nodes);
|
||||
} else {
|
||||
beforeBody = beforeBody.concat(nodes);
|
||||
path.remove();
|
||||
removedPaths.push(path);
|
||||
}
|
||||
} else {
|
||||
path.replaceWith(buildExportCall("default", declar.node));
|
||||
@@ -149,7 +150,16 @@ export default function ({ types: t }) {
|
||||
let nodes = [];
|
||||
let bindingIdentifiers;
|
||||
if (path.isFunction()) {
|
||||
bindingIdentifiers = { [declar.node.id.name]: declar.node.id };
|
||||
let node = declar.node;
|
||||
let name = node.id.name;
|
||||
if (canHoist) {
|
||||
addExportName(name, name);
|
||||
beforeBody.push(node);
|
||||
beforeBody.push(buildExportCall(name, node.id));
|
||||
removedPaths.push(path);
|
||||
} else {
|
||||
bindingIdentifiers = { [name]: node.id };
|
||||
}
|
||||
} else {
|
||||
bindingIdentifiers = declar.getBindingIdentifiers();
|
||||
}
|
||||
@@ -158,22 +168,22 @@ export default function ({ types: t }) {
|
||||
nodes.push(buildExportCall(name, t.identifier(name)));
|
||||
}
|
||||
path.insertAfter(nodes);
|
||||
}
|
||||
} else {
|
||||
let specifiers = path.node.specifiers;
|
||||
if (specifiers && specifiers.length) {
|
||||
if (path.node.source) {
|
||||
pushModule(path.node.source.value, "exports", specifiers);
|
||||
path.remove();
|
||||
} else {
|
||||
let nodes = [];
|
||||
|
||||
let specifiers = path.node.specifiers;
|
||||
if (specifiers && specifiers.length) {
|
||||
if (path.node.source) {
|
||||
pushModule(path.node.source.value, "exports", specifiers);
|
||||
path.remove();
|
||||
} else {
|
||||
let nodes = [];
|
||||
for (let specifier of specifiers) {
|
||||
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
|
||||
addExportName(specifier.local.name, specifier.exported.name);
|
||||
}
|
||||
|
||||
for (let specifier of specifiers) {
|
||||
nodes.push(buildExportCall(specifier.exported.name, specifier.local));
|
||||
addExportName(specifier.local.name, specifier.exported.name);
|
||||
path.replaceWithMultiple(nodes);
|
||||
}
|
||||
|
||||
path.replaceWithMultiple(nodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,6 +255,10 @@ export default function ({ types: t }) {
|
||||
scope: path.scope
|
||||
});
|
||||
|
||||
for (let path of removedPaths) {
|
||||
path.remove();
|
||||
}
|
||||
|
||||
path.node.body = [
|
||||
buildTemplate({
|
||||
SYSTEM_REGISTER: t.memberExpression(t.identifier(state.opts.systemGlobal || "System"), t.identifier("register")),
|
||||
|
||||
Reference in New Issue
Block a user