Simplify AMD export declaration fn to re-use CommonJS one

This commit is contained in:
Lars Kappert 2015-01-04 12:53:05 +01:00
parent 941a8cc5ea
commit af4009694a

View File

@ -1,6 +1,7 @@
module.exports = AMDFormatter;
var DefaultFormatter = require("./_default");
var CommonFormatter = require("./common");
var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
@ -96,37 +97,12 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
]));
};
AMDFormatter.prototype.exportDeclaration = function (node, nodes) {
AMDFormatter.prototype.exportDeclaration = function (node) {
if (node.default) {
var declar = node.declaration;
var assign;
// exports = VALUE;
var templateName = "exports-default";
if (t.isFunction(declar) || !this.hasNonDefaultExports) {
assign = util.template(templateName, {
VALUE: this._pushStatement(declar, nodes)
}, true);
// hoist to the top if this default is a function
nodes.push(this._hoistExport(declar, assign, 3));
return;
} else {
// this export isn't a function so we can't hoist it to the top so we need to set it
// at the very end of the file with something like:
//
// module.exports = Object.assign(exports["default"], exports)
//
assign = util.template("amd-export-default-assign", true);
assign._blockHoist = 0;
nodes.push(assign);
}
this.passModuleArg = true;
}
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
CommonFormatter.prototype.exportDeclaration.apply(this, arguments);
};
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {