From 56f1683f06eb73954d0fe5aaf52cbe06d7f7fc90 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Mon, 5 Jan 2015 14:57:52 +0100 Subject: [PATCH] Also test for "module" identifier if needed --- lib/6to5/transformation/modules/umd.js | 6 ++++++ lib/6to5/transformation/templates/umd-runner-body.js | 2 +- .../es6-modules-umd/exports-default/expected.js | 2 +- .../transformation/es6-modules-umd/overview/expected.js | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index bf7d285b07..1a502a24a1 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -38,6 +38,11 @@ UMDFormatter.prototype.transform = function (ast) { defineArgs = defineArgs.concat(names); defineArgs = [t.arrayExpression(defineArgs)]; + // typeof exports !== "undefined" && typeof module !== "undefined" + var testExports = t.binaryExpression("!==", t.typeOfExpression(t.identifier("exports")), t.literal("undefined")), + testModule = t.binaryExpression("!==", t.typeOfExpression(t.identifier("module")), 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) { @@ -49,6 +54,7 @@ UMDFormatter.prototype.transform = function (ast) { var runner = util.template("umd-runner-body", { AMD_ARGUMENTS: defineArgs, + 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 a97dce38dc..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") { + } else if (COMMON_TEST) { factory(COMMON_ARGUMENTS); } }); 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 618b6fd147..f61ef367a2 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -3,7 +3,7 @@ (function (factory) { if (typeof define === "function" && define.amd) { define(["exports", "module"], factory); - } else if (typeof exports !== "undefined") { + } else if (typeof exports !== "undefined" && typeof module !== "undefined") { factory(exports, module); } })(function (exports, module) { diff --git a/test/fixtures/transformation/es6-modules-umd/overview/expected.js b/test/fixtures/transformation/es6-modules-umd/overview/expected.js index af8ba6172f..594322dac5 100644 --- a/test/fixtures/transformation/es6-modules-umd/overview/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/overview/expected.js @@ -3,7 +3,7 @@ (function (factory) { if (typeof define === "function" && define.amd) { define(["exports", "module", "foo", "foo-bar", "./directory/foo-bar"], factory); - } else if (typeof exports !== "undefined") { + } else if (typeof exports !== "undefined" && typeof module !== "undefined") { factory(exports, module, require("foo"), require("foo-bar"), require("./directory/foo-bar")); } })(function (exports, module, _foo, _fooBar, _directoryFooBar) {