From f1f7321590246f8ce98f4b282bbc8bf8771c2bb0 Mon Sep 17 00:00:00 2001 From: David Arvelo Date: Sun, 16 Nov 2014 02:38:00 -0500 Subject: [PATCH] Generate moduleNames for AMD/UMD --- lib/6to5/transformation/modules/amd.js | 29 +++++++++++++++++++++++++- lib/6to5/transformation/modules/umd.js | 4 +++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index b567f6ad33..098c67e8ef 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -29,12 +29,39 @@ AMDFormatter.prototype.transform = function (ast) { var params = _.values(this.ids); params.unshift(t.identifier("exports")); + var moduleName = this.getModuleName(this.file); + var container = t.functionExpression(null, params, t.blockStatement(body)); - var call = t.callExpression(t.identifier("define"), [names, container]); + var call = t.callExpression(t.identifier("define"), [t.literal(moduleName), names, container]); program.body = [t.expressionStatement(call)]; }; +AMDFormatter.prototype.getModuleName = function (file) { + var opts = file.opts, + filenameRelative = opts.filenameRelative, + sourceRootRegEx, + moduleName; + + if (opts.moduleRoot) { + moduleName = opts.moduleRoot + '/'; + } + + if (!opts.filenameRelative) { + return moduleName + opts.filename.replace(/^\//, ''); + } + + if (opts.sourceRoot) { + sourceRootRegEx = new RegExp('^' + opts.sourceRoot + '\/?'); + filenameRelative = filenameRelative.replace(sourceRootRegEx, ''); + } + + filenameRelative = filenameRelative.replace(/\.js$/, ''); + moduleName += filenameRelative; + + return moduleName; +}; + AMDFormatter.prototype._push = function (node) { var id = node.source.value; var ids = this.ids; diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index 69499f61e6..1da118ed1d 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -32,8 +32,10 @@ UMDFormatter.prototype.transform = function (ast) { // runner + var moduleName = this.getModuleName(this.file); + var runner = util.template("umd-runner-body", { - AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")].concat(names)), + AMD_ARGUMENTS: [t.literal(moduleName), t.arrayExpression([t.literal("exports")].concat(names))], COMMON_ARGUMENTS: names.map(function (name) { return t.callExpression(t.identifier("require"), [name]);