clean up module formatters and fix interopRequireWildcard helper
This commit is contained in:
@@ -12,6 +12,8 @@ function DefaultFormatter(file) {
|
||||
this.file = file;
|
||||
this.ids = object();
|
||||
|
||||
this.hasNonDefaultExports = false;
|
||||
|
||||
this.hasLocalExports = false;
|
||||
this.hasLocalImports = false;
|
||||
|
||||
@@ -27,8 +29,9 @@ function DefaultFormatter(file) {
|
||||
|
||||
DefaultFormatter.prototype.bumpImportOccurences = function (node) {
|
||||
var source = node.source.value;
|
||||
this.localImportOccurences[source] = this.localImportOccurences[source] || 0;
|
||||
this.localImportOccurences[source] += node.specifiers.length;
|
||||
var occurs = this.localImportOccurences;
|
||||
occurs[source] = occurs[source] || 0;
|
||||
occurs[source] += node.specifiers.length;
|
||||
};
|
||||
|
||||
var exportsVisitor = {
|
||||
@@ -36,10 +39,15 @@ var exportsVisitor = {
|
||||
var declar = node && node.declaration;
|
||||
if (t.isExportDeclaration(node)) {
|
||||
formatter.hasLocalImports = true;
|
||||
|
||||
if (declar && t.isStatement(declar)) {
|
||||
_.extend(formatter.localExports, t.getIds(declar, true));
|
||||
}
|
||||
|
||||
if (!node.default) {
|
||||
formatter.hasNonDefaultExports = true;
|
||||
}
|
||||
|
||||
if (node.source) {
|
||||
formatter.bumpImportOccurences(node);
|
||||
}
|
||||
@@ -189,14 +197,14 @@ DefaultFormatter.prototype._hoistExport = function (declar, assign, priority) {
|
||||
return assign;
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype.push = function (node, nodes) {
|
||||
DefaultFormatter.prototype.getExternalReference = function (node, nodes) {
|
||||
var ids = this.ids;
|
||||
var id = node.source.value;
|
||||
|
||||
if (ids[id]) {
|
||||
return ids[id];
|
||||
} else {
|
||||
return this.ids[id] = this._push(node, nodes);
|
||||
return this.ids[id] = this._getExternalReference(node, nodes);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -205,11 +213,11 @@ DefaultFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
if (node.specifiers.length === 1) inherits = node;
|
||||
|
||||
if (node.source) {
|
||||
var ref = this.push(node, nodes);
|
||||
var ref = this.getExternalReference(node, nodes);
|
||||
|
||||
if (t.isExportBatchSpecifier(specifier)) {
|
||||
// export * from "foo";
|
||||
nodes.push(this._exportsWildcard(ref, node));
|
||||
nodes.push(this.buildExportsWildcard(ref, node));
|
||||
} else {
|
||||
var ref;
|
||||
if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) {
|
||||
@@ -220,7 +228,7 @@ DefaultFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
}
|
||||
|
||||
// export { foo } from "test";
|
||||
nodes.push(this._exportsAssign(
|
||||
nodes.push(this.buildExportsAssignment(
|
||||
t.getSpecifierName(specifier),
|
||||
ref,
|
||||
node
|
||||
@@ -228,18 +236,18 @@ DefaultFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||
}
|
||||
} else {
|
||||
// export { foo };
|
||||
nodes.push(this._exportsAssign(t.getSpecifierName(specifier), specifier.id, node));
|
||||
nodes.push(this.buildExportsAssignment(t.getSpecifierName(specifier), specifier.id, node));
|
||||
}
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportsWildcard = function (objectIdentifier) {
|
||||
DefaultFormatter.prototype.buildExportsWildcard = function (objectIdentifier) {
|
||||
return t.expressionStatement(t.callExpression(this.file.addHelper("defaults"), [
|
||||
t.identifier("exports"),
|
||||
t.callExpression(this.file.addHelper("interop-require-wildcard"), [objectIdentifier])
|
||||
]));
|
||||
};
|
||||
|
||||
DefaultFormatter.prototype._exportsAssign = function (id, init) {
|
||||
DefaultFormatter.prototype.buildExportsAssignment = function (id, init) {
|
||||
return util.template("exports-assign", {
|
||||
VALUE: init,
|
||||
KEY: id
|
||||
@@ -261,7 +269,7 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
for (var i = 0; i < declar.declarations.length; i++) {
|
||||
var decl = declar.declarations[i];
|
||||
|
||||
decl.init = this._exportsAssign(decl.id, decl.init, node).expression;
|
||||
decl.init = this.buildExportsAssignment(decl.id, decl.init, node).expression;
|
||||
|
||||
var newDeclar = t.variableDeclaration(declar.kind, [decl]);
|
||||
if (i === 0) t.inherits(newDeclar, declar);
|
||||
@@ -275,7 +283,7 @@ DefaultFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
nodes.push(declar);
|
||||
}
|
||||
|
||||
assign = this._exportsAssign(id, ref, node);
|
||||
assign = this.buildExportsAssignment(id, ref, node);
|
||||
|
||||
nodes.push(assign);
|
||||
|
||||
|
||||
@@ -67,17 +67,17 @@ AMDFormatter.prototype.getModuleName = function () {
|
||||
}
|
||||
};
|
||||
|
||||
AMDFormatter.prototype._push = function (node) {
|
||||
AMDFormatter.prototype._getExternalReference = function (node) {
|
||||
return this.file.generateUidIdentifier(node.source.value);
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.importDeclaration = function (node) {
|
||||
this.push(node);
|
||||
this.getExternalReference(node);
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var key = t.getSpecifierName(specifier);
|
||||
var ref = this.push(node);
|
||||
var ref = this.getExternalReference(node);
|
||||
|
||||
if (_.contains(this.file.dynamicImported, node)) {
|
||||
// Prevent unnecessary renaming of dynamic imports.
|
||||
@@ -99,7 +99,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
};
|
||||
|
||||
AMDFormatter.prototype.exportDeclaration = function (node) {
|
||||
if (node.default && !this.noInteropRequire) {
|
||||
if (node.default && !this.noInteropRequire && !this.hasNonDefaultExports) {
|
||||
this.passModuleArg = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,23 +8,13 @@ var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
var _ = require("lodash");
|
||||
|
||||
var visitor = {
|
||||
enter: function (node, parent, scope, context, state) {
|
||||
if (t.isExportDeclaration(node) && !node.default) {
|
||||
state.hasNonDefaultExports = true;
|
||||
context.stop();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function CommonJSFormatter(file) {
|
||||
DefaultFormatter.apply(this, arguments);
|
||||
|
||||
var state = { hasNonDefaultExports: false };
|
||||
traverse(file.ast, visitor, file.scope, state);
|
||||
|
||||
this.insertedModuleDeclaration = false;
|
||||
this.hasNonDefaultExports = state.hasNonDefaultExports;
|
||||
if (this.hasNonDefaultExports) {
|
||||
file.ast.program.body.push(util.template("exports-module-declaration", true));
|
||||
}
|
||||
}
|
||||
|
||||
util.inherits(CommonJSFormatter, DefaultFormatter);
|
||||
@@ -32,7 +22,7 @@ util.inherits(CommonJSFormatter, DefaultFormatter);
|
||||
CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||
var variableName = t.getSpecifierName(specifier);
|
||||
|
||||
var ref = this.push(node, nodes);
|
||||
var ref = this.getExternalReference(node, nodes);
|
||||
|
||||
// import foo from "foo";
|
||||
if (t.isSpecifierDefault(specifier)) {
|
||||
@@ -71,34 +61,25 @@ CommonJSFormatter.prototype.importDeclaration = function (node, nodes) {
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) {
|
||||
if (node.default && !this.noInteropRequire) {
|
||||
if (node.default && !this.noInteropRequire && !this.hasNonDefaultExports) {
|
||||
var declar = node.declaration;
|
||||
var assign;
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
if (this.hasNonDefaultExports) {
|
||||
if (!this.insertedModuleDeclaration) {
|
||||
nodes.push(util.template("exports-module-declaration", true));
|
||||
this.insertedModuleDeclaration = true;
|
||||
}
|
||||
} else {
|
||||
var assign = util.template("exports-default-assign", {
|
||||
VALUE: this._pushStatement(declar, nodes)
|
||||
}, true);
|
||||
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
if (t.isFunctionDeclaration(declar)) {
|
||||
// we can hoist this assignment to the top of the file
|
||||
assign._blockHoist = 3;
|
||||
}
|
||||
|
||||
nodes.push(assign);
|
||||
return;
|
||||
}
|
||||
|
||||
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
|
||||
};
|
||||
|
||||
CommonJSFormatter.prototype._push = function (node, nodes) {
|
||||
CommonJSFormatter.prototype._getExternalReference = function (node, nodes) {
|
||||
var source = node.source.value;
|
||||
var call = t.callExpression(t.identifier("require"), [node.source]);
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ SystemFormatter.prototype._addImportSource = function (node, exportNode) {
|
||||
return node;
|
||||
};
|
||||
|
||||
SystemFormatter.prototype._exportsWildcard = function (objectIdentifier, node) {
|
||||
SystemFormatter.prototype.buildExportsWildcard = function (objectIdentifier, node) {
|
||||
var leftIdentifier = this.file.generateUidIdentifier("key");
|
||||
var valIdentifier = t.memberExpression(objectIdentifier, leftIdentifier, true);
|
||||
|
||||
@@ -40,7 +40,7 @@ SystemFormatter.prototype._exportsWildcard = function (objectIdentifier, node) {
|
||||
return this._addImportSource(t.forInStatement(left, right, block), node);
|
||||
};
|
||||
|
||||
SystemFormatter.prototype._exportsAssign = function (id, init, node) {
|
||||
SystemFormatter.prototype.buildExportsAssignment = function (id, init, node) {
|
||||
var call = this.buildExportCall(t.literal(id.name), init, true);
|
||||
return this._addImportSource(call, node);
|
||||
};
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
(function (obj) {
|
||||
return obj && obj.__esModule ? obj.default : { default: obj };
|
||||
return obj && obj.__esModule ? obj : { default: obj };
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user