hoist export default to very top
This commit is contained in:
@@ -124,9 +124,9 @@ DefaultFormatter.prototype._pushStatement = function (ref, nodes) {
|
||||
return ref;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._hoistExport = function (declar, assign) {
|
||||
DefaultFormatter.prototype._hoistExport = function (declar, assign, priority) {
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
assign._blockHoist = true;
|
||||
assign._blockHoist = priority || 1;
|
||||
}
|
||||
|
||||
return assign;
|
||||
@@ -139,17 +139,18 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node,
|
||||
if (node.source) {
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
nodes.push(this._exportsWildcard(getRef()));
|
||||
nodes.push(this._exportsWildcard(getRef(), node));
|
||||
} else {
|
||||
// export { foo } from "test";
|
||||
nodes.push(this._exportsAssign(
|
||||
t.getSpecifierName(specifier),
|
||||
t.memberExpression(getRef(), specifier.id)
|
||||
t.memberExpression(getRef(), specifier.id),
|
||||
node
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(this._exportsAssign(t.getSpecifierName(specifier), specifier.id));
|
||||
nodes.push(this._exportsAssign(t.getSpecifierName(specifier), specifier.id, node));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -181,7 +182,7 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
for (var i in declar.declarations) {
|
||||
var decl = declar.declarations[i];
|
||||
|
||||
decl.init = this._exportsAssign(decl.id, decl.init).expression;
|
||||
decl.init = this._exportsAssign(decl.id, decl.init, node).expression;
|
||||
|
||||
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
|
||||
if (i === "0") t.inherits(newDeclar, declar);
|
||||
@@ -195,7 +196,7 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
nodes.push(declar);
|
||||
}
|
||||
|
||||
assign = this._exportsAssign(id, ref);
|
||||
assign = this._exportsAssign(id, ref, node);
|
||||
|
||||
nodes.push(assign);
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
}, true);
|
||||
|
||||
// hoist to the top if this default is a function
|
||||
nodes.push(this._hoistExport(declar, assign));
|
||||
nodes.push(this._hoistExport(declar, assign, 2));
|
||||
} else {
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
var _ = require("lodash");
|
||||
|
||||
exports.BlockStatement =
|
||||
exports.Program = {
|
||||
exit: function (node) {
|
||||
var unshift = [];
|
||||
var hasChange = false;
|
||||
for (var i in node.body) {
|
||||
var bodyNode = node.body[i];
|
||||
if (bodyNode && bodyNode._blockHoist) hasChange = true;
|
||||
}
|
||||
if (!hasChange) return;
|
||||
|
||||
node.body = node.body.filter(function (bodyNode) {
|
||||
if (bodyNode._blockHoist) {
|
||||
unshift.push(bodyNode);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
var nodePriorities = _.groupBy(node.body, function (bodyNode) {
|
||||
return bodyNode._blockHoist || 0;
|
||||
});
|
||||
|
||||
node.body = unshift.concat(node.body);
|
||||
node.body = _.flatten(_.values(nodePriorities).reverse());
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user