From fdd1451d530e7d907b0092815adaffef4232e66a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 14 Nov 2014 17:19:39 +1100 Subject: [PATCH] expose moduleName option - closes #158 --- doc/usage.md | 8 +++++++- lib/6to5/transformation/modules/amd.js | 3 ++- lib/6to5/transformation/modules/common.js | 13 +++++++++++-- lib/6to5/transformation/modules/umd.js | 3 ++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/doc/usage.md b/doc/usage.md index 554583d337..7c290a34e1 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -113,7 +113,13 @@ to5.transformFile("filename.js", options, function (err, result) { // 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 - runtime: true + runtime: true, + + // Optionally declare a function that all import filenames will be piped + // through + moduleName: function (name) { + + } } ``` diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index b567f6ad33..3a6d928bf2 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -15,12 +15,13 @@ util.inherits(AMDFormatter, CommonJSFormatter); AMDFormatter.prototype.transform = function (ast) { var program = ast.program; var body = program.body; + var self = this; // build an array of module names var names = [t.literal("exports")]; _.each(this.ids, function (id, name) { - names.push(t.literal(name)); + names.push(t.literal(self._moduleName(name))); }); names = t.arrayExpression(names); diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index b4950484b3..0b4c092e08 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -7,12 +7,21 @@ function CommonJSFormatter(file) { this.file = file; } +CommonJSFormatter.prototype._moduleName = function (name) { + var fn = this.file.opts.moduleName; + if (fn) { + return fn(name); + } else { + return name; + } +}; + CommonJSFormatter.prototype.import = function (node, nodes) { // import "foo"; nodes.push(util.template("require", { //inherits: node, - MODULE_NAME: node.source.raw + MODULE_NAME: this._moduleName(node.source.raw) }, true)); }; @@ -33,7 +42,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) //inherits: node.specifiers.length === 1 && node, VARIABLE_NAME: variableName, - MODULE_NAME: node.source.raw, + MODULE_NAME: this._moduleName(node.source.raw), KEY: specifier.id })); }; diff --git a/lib/6to5/transformation/modules/umd.js b/lib/6to5/transformation/modules/umd.js index 69499f61e6..f6fd1e2151 100644 --- a/lib/6to5/transformation/modules/umd.js +++ b/lib/6to5/transformation/modules/umd.js @@ -15,12 +15,13 @@ util.inherits(UMDFormatter, AMDFormatter); UMDFormatter.prototype.transform = function (ast) { var program = ast.program; var body = program.body; + var self = this; // build an array of module names var names = []; _.each(this.ids, function (id, name) { - names.push(t.literal(name)); + names.push(t.literal(self._moduleName(name))); }); // factory