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 1/4] 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; + } +}; From e9a024e58a14c8fdc452425714c8adec83b01544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 02:38:31 -0200 Subject: [PATCH 2/4] Add simple test for externalRuntime transformer Just import a module namespace and see what happens. --- .../transformation/optional-external-runtime/basic/actual.js | 1 + .../optional-external-runtime/basic/expected.js | 5 +++++ .../transformation/optional-external-runtime/options.json | 4 ++++ 3 files changed, 10 insertions(+) create mode 100644 test/fixtures/transformation/optional-external-runtime/basic/actual.js create mode 100644 test/fixtures/transformation/optional-external-runtime/basic/expected.js create mode 100644 test/fixtures/transformation/optional-external-runtime/options.json diff --git a/test/fixtures/transformation/optional-external-runtime/basic/actual.js b/test/fixtures/transformation/optional-external-runtime/basic/actual.js new file mode 100644 index 0000000000..9e9515cece --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/basic/actual.js @@ -0,0 +1 @@ +import * as foo from "someModule"; diff --git a/test/fixtures/transformation/optional-external-runtime/basic/expected.js b/test/fixtures/transformation/optional-external-runtime/basic/expected.js new file mode 100644 index 0000000000..b10bbe6f09 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/basic/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime"); + +var foo = _to5Runtime.interopRequireWildcard(require("someModule")); diff --git a/test/fixtures/transformation/optional-external-runtime/options.json b/test/fixtures/transformation/optional-external-runtime/options.json new file mode 100644 index 0000000000..44c5164a67 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/options.json @@ -0,0 +1,4 @@ +{ + "runtime": "6to5-runtime", + "optional": ["externalRuntime"] +} From 84ee0efe32c1ad17b869c2f7a52d451c09734379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 12:13:08 -0200 Subject: [PATCH 3/4] Add test for externalRuntime with AMD modules --- .../transformation/optional-external-runtime/amd/actual.js | 1 + .../optional-external-runtime/amd/expected.js | 6 ++++++ .../optional-external-runtime/amd/options.json | 3 +++ .../optional-external-runtime/{basic => commonjs}/actual.js | 0 .../{basic => commonjs}/expected.js | 0 5 files changed, 10 insertions(+) create mode 100644 test/fixtures/transformation/optional-external-runtime/amd/actual.js create mode 100644 test/fixtures/transformation/optional-external-runtime/amd/expected.js create mode 100644 test/fixtures/transformation/optional-external-runtime/amd/options.json rename test/fixtures/transformation/optional-external-runtime/{basic => commonjs}/actual.js (100%) rename test/fixtures/transformation/optional-external-runtime/{basic => commonjs}/expected.js (100%) diff --git a/test/fixtures/transformation/optional-external-runtime/amd/actual.js b/test/fixtures/transformation/optional-external-runtime/amd/actual.js new file mode 100644 index 0000000000..09493dc9be --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/amd/actual.js @@ -0,0 +1 @@ +import foo from "someModule"; diff --git a/test/fixtures/transformation/optional-external-runtime/amd/expected.js b/test/fixtures/transformation/optional-external-runtime/amd/expected.js new file mode 100644 index 0000000000..20e759af12 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/amd/expected.js @@ -0,0 +1,6 @@ +define(["exports", "6to5-runtime", "someModule"], function (exports, _to5Runtime2, _someModule) { + "use strict"; + + var _to5Runtime = _to5Runtime2; + var foo = _to5Runtime.interopRequire(_someModule); +}); diff --git a/test/fixtures/transformation/optional-external-runtime/amd/options.json b/test/fixtures/transformation/optional-external-runtime/amd/options.json new file mode 100644 index 0000000000..b76b119f33 --- /dev/null +++ b/test/fixtures/transformation/optional-external-runtime/amd/options.json @@ -0,0 +1,3 @@ +{ + "modules": "amd" +} diff --git a/test/fixtures/transformation/optional-external-runtime/basic/actual.js b/test/fixtures/transformation/optional-external-runtime/commonjs/actual.js similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/basic/actual.js rename to test/fixtures/transformation/optional-external-runtime/commonjs/actual.js diff --git a/test/fixtures/transformation/optional-external-runtime/basic/expected.js b/test/fixtures/transformation/optional-external-runtime/commonjs/expected.js similarity index 100% rename from test/fixtures/transformation/optional-external-runtime/basic/expected.js rename to test/fixtures/transformation/optional-external-runtime/commonjs/expected.js From e985d8b25de9f3b281a8a700c34c64099acc74b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alu=C3=ADsio=20Augusto=20Silva=20Gon=C3=A7alves?= Date: Wed, 14 Jan 2015 14:00:33 -0200 Subject: [PATCH 4/4] Don't use interopRequire for dynamic imports They don't need it at all and this also allows use of `externalRuntime` without fear of the runtime ending up being loaded after it's used. --- lib/6to5/transformation/modules/amd.js | 2 +- lib/6to5/transformation/modules/common.js | 3 ++- .../transformers/optional-external-runtime.js | 1 - .../optional-bluebird-coroutines/expression/expected.js | 6 +----- .../optional-bluebird-coroutines/statement/expected.js | 6 +----- .../optional-core-aliasing/aliased-constructors/expected.js | 6 +----- .../optional-core-aliasing/es6-destructuring/expected.js | 6 +----- .../optional-core-aliasing/es6-for-of/expected.js | 6 +----- .../optional-core-aliasing/es6-spread/expected.js | 6 +----- .../es7-array-comprehensions/expected.js | 6 +----- 10 files changed, 10 insertions(+), 38 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index d97ba66118..0eb61407b2 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) || node._noInteropRequire) { + if (t.isImportBatchSpecifier(specifier) || _.contains(this.file.dynamicImports, node)) { // 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 c44f34078f..950b35790e 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -6,6 +6,7 @@ var DefaultFormatter = require("./_default"); var traverse = require("../../traverse"); var util = require("../../util"); var t = require("../../types"); +var _ = require("lodash"); function CommonJSFormatter(file) { DefaultFormatter.apply(this, arguments); @@ -27,7 +28,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) // import foo from "foo"; if (t.isSpecifierDefault(specifier)) { var ref = util.template("require", {MODULE_NAME: node.source}); - if (!node._noInteropRequire) { + if (!_.contains(this.file.dynamicImports, node)) { ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); } nodes.push(t.variableDeclaration("var", [t.variableDeclarator(variableName, ref)])); diff --git a/lib/6to5/transformation/transformers/optional-external-runtime.js b/lib/6to5/transformation/transformers/optional-external-runtime.js index a2f52667cf..ff39454e4d 100644 --- a/lib/6to5/transformation/transformers/optional-external-runtime.js +++ b/lib/6to5/transformation/transformers/optional-external-runtime.js @@ -6,6 +6,5 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { file.opts.runtime = file.addImport(file.opts.runtime, "to5Runtime").name; - file.dynamicImports[file.dynamicImports.length - 1]._noInteropRequire = true; } }; diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js index 13943a98de..ed68858b19 100644 --- a/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js +++ b/test/fixtures/transformation/optional-bluebird-coroutines/expression/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _bluebird = _interopRequire(require("bluebird")); +var _bluebird = require("bluebird"); var foo = _bluebird.coroutine(function* () { var wat = yield bar(); diff --git a/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js b/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js index 9b200b29f0..c5dbaaf5d1 100644 --- a/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js +++ b/test/fixtures/transformation/optional-bluebird-coroutines/statement/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _bluebird = _interopRequire(require("bluebird")); +var _bluebird = require("bluebird"); var foo = _bluebird.coroutine(function* foo() { var wat = yield bar(); diff --git a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js b/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js index 7c6e3f9ba2..8c6ff60ed1 100644 --- a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); obj.constructor === Object; obj.constructor === _core.Promise; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js index fc43ff5423..15e3ce78b9 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js @@ -16,11 +16,7 @@ var _slicedToArray = function (arr, i) { } }; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); var _ref = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js index 3bf1f53a05..2669fd4d0c 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { var i = _step.value; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js index 4d00347582..2a41ccd997 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js @@ -4,10 +4,6 @@ var _toArray = function (arr) { return Array.isArray(arr) ? arr : _core.Array.from(arr); }; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); var lyrics = ["head", "and", "toes"].concat(_toArray(parts)); diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js index 01333ca8f9..0bf67a612a 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js @@ -1,10 +1,6 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); +var _core = require("core-js/library"); var arr = (function () { var _arr = [];