Make AMD module id's optional

This commit is contained in:
Lars Kappert
2014-11-18 22:04:41 +01:00
parent 68ef2d545e
commit 8db466c698
3 changed files with 10 additions and 5 deletions

View File

@@ -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-id", "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,
amdModuleId: commander.amdModuleId,
runtime: commander.runtime,
modules: commander.modules
};

View File

@@ -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 = {};
@@ -58,7 +58,7 @@ File.normaliseOptions = function (opts) {
return opts;
};
File.prototype.getModuleFormatter = function (type) {
File.prototype.getModuleFormatter = function (type, opts) {
var ModuleFormatter = transform.moduleFormatters[type];
if (!ModuleFormatter) {
@@ -70,7 +70,7 @@ File.prototype.getModuleFormatter = function (type) {
throw new ReferenceError("unknown module formatter type " + type);
}
return new ModuleFormatter(this);
return new ModuleFormatter(this, opts);
};
File.prototype.parseShebang = function (code) {

View File

@@ -5,9 +5,10 @@ var util = require("../../util");
var t = require("../../types");
var _ = require("lodash");
function AMDFormatter(file) {
function AMDFormatter(file, opts) {
this.file = file;
this.ids = {};
this.insertModuleId = opts.amdModuleId;
}
util.inherits(AMDFormatter, CommonJSFormatter);
@@ -32,7 +33,9 @@ AMDFormatter.prototype.transform = function (ast) {
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];
if( this.insertModuleId ) defineArgs.unshift(t.literal(moduleName));
var call = t.callExpression(t.identifier("define"), defineArgs);
program.body = [t.expressionStatement(call)];
};