From 52ffc65a0641a21a71af34cbb412e9ed05c4714f Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 29 Dec 2014 01:55:30 +1100 Subject: [PATCH] make `system` module formatter modules anonymous by default - fixes #347 --- CHANGELOG.md | 4 ++++ bin/6to5/index.js | 5 +++-- doc/usage.md | 6 +++--- lib/6to5/file.js | 1 + lib/6to5/transformation/modules/amd.js | 2 +- lib/6to5/transformation/modules/system.js | 11 ++++++----- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a2a2f372a..54839e0afb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Gaps between patch versions are faulty/broken releases. +## 2.2.0 + + * Make `system` module formatter modules anonymous by default. + ## 2.1.0 * Add `cache` option to register hook. diff --git a/bin/6to5/index.js b/bin/6to5/index.js index a1c88877ba..77263e50ee 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -20,7 +20,8 @@ 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.option("-a, --amd-module-ids", "Insert module id in AMD modules", false); // todo: remove in 3.0.0 +commander.option("-m, --module-ids", "Insert module id in modules", false); commander.on("--help", function(){ var outKeys = function (title, obj) { @@ -89,9 +90,9 @@ if (errors.length) { exports.opts = { sourceMapName: commander.outFile, - amdModuleIds: commander.amdModuleIds, experimental: commander.experimental, playground: commander.playground, + moduleIds: commander.amdModuleIds || commander.moduleIds, blacklist: commander.blacklist, whitelist: commander.whitelist, sourceMap: commander.sourceMaps || commander.sourceMapsInline, diff --git a/doc/usage.md b/doc/usage.md index 00564619e6..84917bc0b0 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -152,10 +152,10 @@ result.ast; // Default: `sourceRoot` option. moduleRoot: "my-app", - // If truthy, insert an explicit id for each defined AMD module. - // By default, AMD modules are anonymous. + // If truthy, insert an explicit id for each defined AMD/System module. + // By default, AMD/System modules are anonymous. // Default: false - amdModuleIds: true, + moduleIds: true, // Optionally replace all 6to5 helper declarations with a referenece to this // variable. If set to `true` then the default namespace is used "to5Runtime". diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 370bdf1826..3f055a7456 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -35,6 +35,7 @@ File.normaliseOptions = function (opts) { experimental: false, playground: false, whitespace: true, + moduleIds: opts.amdModuleIds || false, blacklist: [], whitelist: [], sourceMap: false, diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index f40d2de60b..48ba860fe5 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -55,7 +55,7 @@ AMDFormatter.prototype.transform = function (ast) { */ AMDFormatter.prototype.getModuleName = function () { - if (this.file.opts.amdModuleIds) { + if (this.file.opts.moduleIds) { return DefaultFormatter.prototype.getModuleName.apply(this, arguments); } else { return null; diff --git a/lib/6to5/transformation/modules/system.js b/lib/6to5/transformation/modules/system.js index e7a320618b..f921c05e14 100644 --- a/lib/6to5/transformation/modules/system.js +++ b/lib/6to5/transformation/modules/system.js @@ -12,14 +12,10 @@ function SystemFormatter(file) { this.noInteropRequire = true; AMDFormatter.apply(this, arguments); - - this.moduleNameLiteral = t.literal(this.getModuleName()); } util.inherits(SystemFormatter, AMDFormatter); -SystemFormatter.prototype.getModuleName = DefaultFormatter.prototype.getModuleName; - SystemFormatter.prototype._exportsWildcard = function (objectIdentifier) { var leftIdentifier = t.identifier("i"); var valIdentifier = t.memberExpression(objectIdentifier, leftIdentifier, true); @@ -67,8 +63,11 @@ SystemFormatter.prototype.buildRunnerSetters = function () { SystemFormatter.prototype.transform = function (ast) { var program = ast.program; + var moduleName = this.getModuleName(); + var moduleNameLiteral = t.literal(moduleName); + var runner = util.template("system", { - MODULE_NAME: this.moduleNameLiteral, + MODULE_NAME: moduleNameLiteral, MODULE_DEPENDENCIES: t.arrayExpression(this.buildDependencyLiterals()), EXPORT_IDENTIFIER: this.exportIdentifier, SETTERS: this.buildRunnerSetters(), @@ -76,6 +75,8 @@ SystemFormatter.prototype.transform = function (ast) { }, true); var handlerBody = runner.expression.arguments[2].body.body; + if (!moduleName) runner.expression.arguments.shift(); + var returnStatement = handlerBody.pop(); // hoist up function declarations for circular references