Also test for "module" identifier if needed

This commit is contained in:
Lars Kappert
2015-01-05 14:57:52 +01:00
parent 520dd7e947
commit 56f1683f06
4 changed files with 9 additions and 3 deletions

View File

@@ -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
});

View File

@@ -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);
}
});