fix: systemjs - hoist named function exports (#3594)

This commit is contained in:
Aliaksei Sapach 2016-07-27 01:06:42 +03:00 committed by Henry Zhu
parent dce9ee9d80
commit 83522c9cb5
6 changed files with 76 additions and 30 deletions

View File

@ -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")),

View File

@ -0,0 +1,9 @@
export function a() {
alert("a");
}
function b() {
a();
}
b();

View File

@ -0,0 +1,20 @@
System.register([], function (_export, _context) {
"use strict";
function a() {
alert("a");
}
_export("a", a);
function b() {
a();
}
return {
setters: [],
execute: function () {
b();
}
};
});

View File

@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-es2015-modules-systemjs"]
}

View File

@ -1,6 +1,14 @@
System.register([], function (_export, _context) {
"use strict";
function foo() {}
_export("foo", foo);
function foo2(bar) {}
_export("foo2", foo2);
return {
setters: [],
execute: function () {
@ -17,14 +25,6 @@ System.register([], function (_export, _context) {
_export("default", foo);
_export("bar", bar);
function foo() {}
_export("foo", foo);
function foo2(bar) {}
_export("foo2", foo2);
}
};
});

View File

@ -2,17 +2,17 @@ System.register(["./evens"], function (_export, _context) {
"use strict";
var isEven, p, a, i, j, isOdd;
function nextOdd(n) {
return _export("p", p = isEven(n) ? n + 1 : n + 2);
}
_export("nextOdd", nextOdd);
return {
setters: [function (_evens) {
isEven = _evens.isEven;
}],
execute: function () {
function nextOdd(n) {
return _export("p", p = isEven(n) ? n + 1 : n + 2);
}
_export("nextOdd", nextOdd);
_export("p", p = 5);
_export("p", p);