Merge pull request #190 from webpro/amd-module-id
Make AMD module id's optional
This commit is contained in:
@@ -18,6 +18,7 @@ commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NO
|
||||
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
||||
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
|
||||
commander.option("-c, --remove-comments", "Remove comments from the compiled code", false);
|
||||
commander.option("-a, --amd-module-ids", "Insert module id in AMD modules", false);
|
||||
|
||||
commander.on("--help", function(){
|
||||
var outKeys = function (title, obj) {
|
||||
@@ -90,6 +91,7 @@ exports.opts = {
|
||||
whitelist: commander.whitelist,
|
||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||
comments: !commander.removeComments,
|
||||
amdModuleIds: commander.amdModuleIds,
|
||||
runtime: commander.runtime,
|
||||
modules: commander.modules
|
||||
};
|
||||
|
||||
@@ -66,7 +66,7 @@ export function bar() {
|
||||
**Out**
|
||||
|
||||
```javascript
|
||||
define("filename", ["exports", "foo"], function (exports, _foo) {
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
exports.bar = bar;
|
||||
|
||||
var foo = _foo.default;
|
||||
@@ -77,6 +77,12 @@ define("filename", ["exports", "foo"], function (exports, _foo) {
|
||||
});
|
||||
```
|
||||
|
||||
You can optionally specify to include the module id (using the `--amd-module-id` argument):
|
||||
|
||||
```javascript
|
||||
define("filename", ["exports", "foo"], function (exports, _foo) {})
|
||||
```
|
||||
|
||||
### UMD
|
||||
|
||||
**In**
|
||||
@@ -94,7 +100,7 @@ export function bar() {
|
||||
```javascript
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("filename", ["exports", "foo"], factory);
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
|
||||
@@ -123,6 +123,11 @@ to5.transformFile("filename.js", options, function (err, result) {
|
||||
// Default: `sourceRoot` option.
|
||||
moduleRoot: "my-app",
|
||||
|
||||
// If truthy, insert an explicit id for each defined AMD module.
|
||||
// By default, AMD modules are anonymous.
|
||||
// Default: false
|
||||
amdModuleIds: true,
|
||||
|
||||
// Optionally replace all 6to5 helper declarations with a referenece to this
|
||||
// variable. If set to `true` then the default namespace is used "to5Runtime".
|
||||
// Default: false
|
||||
|
||||
@@ -11,7 +11,7 @@ var _ = require("lodash");
|
||||
|
||||
function File(opts) {
|
||||
this.opts = File.normaliseOptions(opts);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules);
|
||||
this.moduleFormatter = this.getModuleFormatter(this.opts.modules, this.opts);
|
||||
|
||||
this.declarations = {};
|
||||
this.uids = {};
|
||||
|
||||
@@ -29,10 +29,11 @@ AMDFormatter.prototype.transform = function (ast) {
|
||||
var params = _.values(this.ids);
|
||||
params.unshift(t.identifier("exports"));
|
||||
|
||||
var moduleName = this.getModuleName();
|
||||
|
||||
var container = t.functionExpression(null, params, t.blockStatement(body));
|
||||
var call = t.callExpression(t.identifier("define"), [t.literal(moduleName), names, container]);
|
||||
var defineArgs = [names, container];
|
||||
var moduleName = this.getModuleName();
|
||||
if (moduleName) defineArgs.unshift(t.literal(moduleName));
|
||||
var call = t.callExpression(t.identifier("define"), defineArgs);
|
||||
|
||||
program.body = [t.expressionStatement(call)];
|
||||
};
|
||||
@@ -42,6 +43,10 @@ AMDFormatter.prototype.getModuleName = function () {
|
||||
var filenameRelative = opts.filenameRelative;
|
||||
var moduleName = "";
|
||||
|
||||
if (!opts.amdModuleIds) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (opts.moduleRoot) {
|
||||
moduleName = opts.moduleRoot + "/";
|
||||
}
|
||||
|
||||
@@ -31,10 +31,12 @@ UMDFormatter.prototype.transform = function (ast) {
|
||||
|
||||
// runner
|
||||
|
||||
var defineArgs = [t.arrayExpression([t.literal("exports")].concat(names))];
|
||||
var moduleName = this.getModuleName();
|
||||
if (moduleName) defineArgs.unshift(t.literal(moduleName));
|
||||
|
||||
var runner = util.template("umd-runner-body", {
|
||||
AMD_ARGUMENTS: [t.literal(moduleName), t.arrayExpression([t.literal("exports")].concat(names))],
|
||||
AMD_ARGUMENTS: defineArgs,
|
||||
|
||||
COMMON_ARGUMENTS: names.map(function (name) {
|
||||
return t.callExpression(t.identifier("require"), [name]);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/exports-default/expected", ["exports"], function (exports) {
|
||||
define(["exports"], function (exports) {
|
||||
"use strict";
|
||||
|
||||
exports["default"] = 42;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/exports-from/expected", ["exports", "foo"], function (exports, _foo) {
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
(function (obj) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/exports-named/expected", ["exports"], function (exports) {
|
||||
define(["exports"], function (exports) {
|
||||
"use strict";
|
||||
|
||||
exports.foo = foo;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/exports-variable/expected", ["exports"], function (exports) {
|
||||
define(["exports"], function (exports) {
|
||||
"use strict";
|
||||
|
||||
exports.foo7 = foo7;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/hoist-function-exports/expected", ["exports", "./evens"], function (exports, _evens) {
|
||||
define(["exports", "./evens"], function (exports, _evens) {
|
||||
"use strict";
|
||||
|
||||
exports.nextOdd = nextOdd;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/imports-default/expected", ["exports", "foo"], function (exports, _foo) {
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var foo = _foo["default"];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/imports-glob/expected", ["exports", "foo"], function (exports, _foo) {
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var foo = _foo;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/imports-mixing/expected", ["exports", "foo"], function (exports, _foo) {
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var foo = _foo["default"];
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/imports-named/expected", ["exports", "foo"], function (exports, _foo) {
|
||||
define(["exports", "foo"], function (exports, _foo) {
|
||||
"use strict";
|
||||
|
||||
var bar = _foo.bar;
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
define("modules-amd/imports/expected", ["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
"use strict";
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define("modules-amd/overview/expected", ["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
define(["exports", "foo", "foo-bar", "./directory/foo-bar"], function (exports, _foo, _fooBar, _directoryFooBar) {
|
||||
"use strict";
|
||||
|
||||
var foo = _foo["default"];
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/exports-default/expected", ["exports"], factory);
|
||||
define(["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/exports-from/expected", ["exports", "foo"], factory);
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/exports-named/expected", ["exports"], factory);
|
||||
define(["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/exports-variable/expected", ["exports"], factory);
|
||||
define(["exports"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/hoist-function-exports/expected", ["exports", "./evens"], factory);
|
||||
define(["exports", "./evens"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("./evens"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/imports-default/expected", ["exports", "foo"], factory);
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/imports-glob/expected", ["exports", "foo"], factory);
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/imports-mixing/expected", ["exports", "foo"], factory);
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/imports-named/expected", ["exports", "foo"], factory);
|
||||
define(["exports", "foo"], factory);
|
||||
} else if (typeof exports !== "undefined") {
|
||||
factory(exports, require("foo"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/imports/expected", ["exports", "foo", "foo-bar", "./directory/foo-bar"], factory);
|
||||
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"));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
(function (factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define("modules-umd/overview/expected", ["exports", "foo", "foo-bar", "./directory/foo-bar"], factory);
|
||||
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"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user