commit
92359520cd
@ -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 () {
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
@ -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;
|
||||
});
|
||||
module.exports = Foo;
|
||||
});
|
||||
|
||||
@ -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;
|
||||
});
|
||||
module.exports = test;
|
||||
});
|
||||
|
||||
@ -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;
|
||||
});
|
||||
module.exports = Foo;
|
||||
});
|
||||
|
||||
@ -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;
|
||||
});
|
||||
module.exports = test;
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user