From e8237910e89218d86225233493a74e75664da2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 01:39:37 -0200 Subject: [PATCH] Add a transformer to import the runtime from a file The `externalRuntime` optional transformer can be used in conjunction with the `runtime` option to import and use the runtime from a module instead of polluting the global environment. --- lib/6to5/transformation/modules/amd.js | 2 +- lib/6to5/transformation/modules/common.js | 12 +++++------- lib/6to5/transformation/transform.js | 1 + .../transformers/optional-external-runtime.js | 11 +++++++++++ 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 lib/6to5/transformation/transformers/optional-external-runtime.js diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 3940c9d18d..d97ba66118 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -87,7 +87,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var key = t.getSpecifierName(specifier); var ref = this._push(node); - if (t.isImportBatchSpecifier(specifier)) { + if (t.isImportBatchSpecifier(specifier) || node._noInteropRequire) { // import * as bar from "foo"; } else if (t.isSpecifierDefault(specifier) && !this.noInteropRequire) { // import foo from "foo"; diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 8e6044f2e9..c44f34078f 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -26,13 +26,11 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) // import foo from "foo"; if (t.isSpecifierDefault(specifier)) { - nodes.push(t.variableDeclaration("var", [ - t.variableDeclarator(variableName, - t.callExpression(this.file.addHelper("interop-require"), [util.template("require", { - MODULE_NAME: node.source - })]) - ) - ])); + var ref = util.template("require", {MODULE_NAME: node.source}); + if (!node._noInteropRequire) { + ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); + } + nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)])); } else { if (specifier.type === "ImportBatchSpecifier") { // import * as bar from "foo"; diff --git a/lib/6to5/transformation/transform.js b/lib/6to5/transformation/transform.js index 2a5cbb26a2..a1c7a104ac 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -95,6 +95,7 @@ _.each({ _moduleFormatter: require("./transformers/_module-formatter"), typeofSymbol: require("./transformers/optional-typeof-symbol"), + externalRuntime: require("./transformers/optional-external-runtime"), coreAliasing: require("./transformers/optional-core-aliasing"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), diff --git a/lib/6to5/transformation/transformers/optional-external-runtime.js b/lib/6to5/transformation/transformers/optional-external-runtime.js new file mode 100644 index 0000000000..a2f52667cf --- /dev/null +++ b/lib/6to5/transformation/transformers/optional-external-runtime.js @@ -0,0 +1,11 @@ +exports.optional = true; + +// In theory, it would be more appropriate to do this in `manipulateOptions`, +// but we need an identifier for the import and we can't get that before the +// AST is built. +exports.ast = { + enter: function (ast, file) { + file.opts.runtime = file.addImport(file.opts.runtime, "to5Runtime").name; + file.dynamicImports[file.dynamicImports.length - 1]._noInteropRequire = true; + } +};