diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index bb4b4137ae..bfa4bfcf7c 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -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"); @@ -30,12 +31,15 @@ AMDFormatter.prototype.transform = function (ast) { // build an array of module names - var names = [t.literal("exports")].concat(this.buildDependencyLiterals()); + var names = [t.literal("exports")]; + if (this.passModuleArg) names.push(t.literal("module")); + names = names.concat(this.buildDependencyLiterals()); names = t.arrayExpression(names); // build up define container var params = _.values(this.ids); + if (this.passModuleArg) params.unshift(t.identifier("module")); params.unshift(t.identifier("exports")); var container = t.functionExpression(null, params, t.blockStatement(body)); @@ -96,6 +100,14 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { ])); }; +AMDFormatter.prototype.exportDeclaration = function (node) { + if (node.default) { + this.passModuleArg = true; + } + + CommonFormatter.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/modules/common.js b/lib/6to5/transformation/modules/common.js index 77224cd148..7364d6a3fd 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -61,7 +61,7 @@ CommonJSFormatter.prototype.importDeclaration = function (node, nodes) { }; CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) { - if (node.default) { + if (node.default && !this.exportIdentifier) { var declar = node.declaration; var assign; diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index e23c7c6fc2..299a4aee22 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -25,22 +25,37 @@ UMDFormatter.prototype.transform = function (ast) { // factory var ids = _.values(this.ids); - var args = [t.identifier("exports")].concat(ids); + var args = [t.identifier("exports")]; + if (this.passModuleArg) args.push(t.identifier("module")); + args = args.concat(ids); var factory = t.functionExpression(null, args, t.blockStatement(body)); // runner - var defineArgs = [t.arrayExpression([t.literal("exports")].concat(names))]; + var defineArgs = [t.literal("exports")]; + if (this.passModuleArg) defineArgs.push(t.literal("module")); + defineArgs = defineArgs.concat(names); + defineArgs = [t.arrayExpression(defineArgs)]; + + // typeof exports !== "undefined" && typeof module !== "undefined" + var testExports = t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier("exports"), true), t.literal("undefined")), + testModule = t.binaryExpression("!==", t.unaryExpression("typeof", t.identifier("module"), true), t.literal("undefined")), + commonTests = this.passModuleArg ? t.logicalExpression("&&", testExports, testModule) : testExports; + + var commonArgs = [t.identifier("exports")]; + if (this.passModuleArg) commonArgs.push(t.identifier("module")); + commonArgs = commonArgs.concat(names.map(function (name) { + return t.callExpression(t.identifier("require"), [name]); + })); + var moduleName = this.getModuleName(); if (moduleName) defineArgs.unshift(t.literal(moduleName)); var runner = util.template("umd-runner-body", { AMD_ARGUMENTS: defineArgs, - - COMMON_ARGUMENTS: names.map(function (name) { - return t.callExpression(t.identifier("require"), [name]); - }) + COMMON_TEST: commonTests, + COMMON_ARGUMENTS: commonArgs }); // diff --git a/lib/6to5/transformation/templates/umd-runner-body.js b/lib/6to5/transformation/templates/umd-runner-body.js index f3c9659b5b..725995b904 100644 --- a/lib/6to5/transformation/templates/umd-runner-body.js +++ b/lib/6to5/transformation/templates/umd-runner-body.js @@ -1,7 +1,7 @@ (function (factory) { if (typeof define === "function" && define.amd) { define(AMD_ARGUMENTS, factory); - } else if (typeof exports !== "undefined") { - factory(exports, COMMON_ARGUMENTS); + } else if (COMMON_TEST) { + factory(COMMON_ARGUMENTS); } }); diff --git a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js index 7d36f6bb48..a97787be5e 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js @@ -1,17 +1,17 @@ -define(["exports"], function (exports) { +define(["exports", "module"], function (exports, module) { "use strict"; - exports["default"] = foo; - exports["default"] = 42; - exports["default"] = {}; - exports["default"] = []; - exports["default"] = foo; - exports["default"] = function () {}; + module.exports = foo; + module.exports = 42; + module.exports = {}; + module.exports = []; + module.exports = foo; + module.exports = function () {}; - exports["default"] = function () {}; + module.exports = function () {}; function foo() {} var Foo = function Foo() {}; - exports["default"] = Foo; -}); \ No newline at end of file + module.exports = Foo; +}); diff --git a/test/fixtures/transformation/es6-modules-amd/overview/expected.js b/test/fixtures/transformation/es6-modules-amd/overview/expected.js index a0b839c896..d1c203ae88 100644 --- a/test/fixtures/transformation/es6-modules-amd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/overview/expected.js @@ -1,4 +1,4 @@ -define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) { +define(["exports", "module", "foo", "foo-bar", "./directory/foo-bar"], function (exports, module, _foo, _fooBar, _directoryFooBar) { "use strict"; var _interopRequire = function (obj) { @@ -13,5 +13,5 @@ define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, exports.test = test; var test = exports.test = 5; - exports["default"] = test; -}); \ No newline at end of file + module.exports = test; +}); diff --git a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js index 4eb8ccf35d..384b5e8f43 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -1,23 +1,23 @@ (function (factory) { if (typeof define === "function" && define.amd) { - define(["exports"], factory); - } else if (typeof exports !== "undefined") { - factory(exports); + define(["exports", "module"], factory); + } else if (typeof exports !== "undefined" && typeof module !== "undefined") { + factory(exports, module); } -})(function (exports) { +})(function (exports, module) { "use strict"; - exports["default"] = foo; - exports["default"] = 42; - exports["default"] = {}; - exports["default"] = []; - exports["default"] = foo; - exports["default"] = function () {}; + module.exports = foo; + module.exports = 42; + module.exports = {}; + module.exports = []; + module.exports = foo; + module.exports = function () {}; - exports["default"] = function () {}; + module.exports = function () {}; function foo() {} var Foo = function Foo() {}; - exports["default"] = Foo; -}); \ No newline at end of file + module.exports = Foo; +}); diff --git a/test/fixtures/transformation/es6-modules-umd/overview/expected.js b/test/fixtures/transformation/es6-modules-umd/overview/expected.js index c719a30063..73f835715e 100644 --- a/test/fixtures/transformation/es6-modules-umd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/overview/expected.js @@ -1,10 +1,10 @@ (function (factory) { if (typeof define === "function" && define.amd) { - define(["exports", "foo", "foo-bar", "./directory/foo-bar"], factory); - } else if (typeof exports !== "undefined") { - factory(exports, require("foo"), require("foo-bar"), require("./directory/foo-bar")); + define(["exports", "module", "foo", "foo-bar", "./directory/foo-bar"], factory); + } else if (typeof exports !== "undefined" && typeof module !== "undefined") { + factory(exports, module, require("foo"), require("foo-bar"), require("./directory/foo-bar")); } -})(function (exports, _foo, _fooBar, _directoryFooBar) { +})(function (exports, module, _foo, _fooBar, _directoryFooBar) { "use strict"; var _interopRequire = function (obj) { @@ -19,5 +19,5 @@ exports.test = test; var test = exports.test = 5; - exports["default"] = test; -}); \ No newline at end of file + module.exports = test; +});