diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index bb4b4137ae..af44fe85ea 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -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 () { diff --git a/lib/6to5/transformation/templates/amd-export-default-assign.js b/lib/6to5/transformation/templates/amd-export-default-assign.js new file mode 100644 index 0000000000..071592cf40 --- /dev/null +++ b/lib/6to5/transformation/templates/amd-export-default-assign.js @@ -0,0 +1 @@ +exports = Object.assign(exports["default"], exports); diff --git a/lib/6to5/transformation/templates/exports-default.js b/lib/6to5/transformation/templates/exports-default.js new file mode 100644 index 0000000000..2e903c2d4c --- /dev/null +++ b/lib/6to5/transformation/templates/exports-default.js @@ -0,0 +1 @@ +exports = VALUE;