Add custom export declaration fn for AMD modules

This commit is contained in:
Lars Kappert 2015-01-04 11:46:28 +01:00
parent 61c3e0a3de
commit 7ac98c1532
3 changed files with 35 additions and 0 deletions

View File

@ -96,6 +96,39 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
]));
};
AMDFormatter.prototype.exportDeclaration = function (node, nodes) {
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);
}
}
DefaultFormatter.prototype.exportDeclaration.apply(this, arguments);
};
AMDFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
var self = this;
return this._exportSpecifier(function () {

View File

@ -0,0 +1 @@
exports = Object.assign(exports["default"], exports);

View File

@ -0,0 +1 @@
exports = VALUE;