diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 3940c9d18d..6d06ddbe46 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -87,7 +87,11 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var key = t.getSpecifierName(specifier); var ref = this._push(node); - if (t.isImportBatchSpecifier(specifier)) { + if (_.contains(this.file.dynamicImports, node)) { + // Prevent unnecessary renaming of dynamic imports. + this.ids[node.source.value] = key; + return; + } else if (t.isImportBatchSpecifier(specifier)) { // 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..73653c8d04 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); @@ -26,13 +27,13 @@ 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 (!_.contains(this.file.dynamicImports, node)) { + 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 a98b0fe820..5a8d63e74d 100644 --- a/lib/6to5/transformation/transform.js +++ b/lib/6to5/transformation/transform.js @@ -96,7 +96,7 @@ _.each({ _moduleFormatter: require("./transformers/_module-formatter"), typeofSymbol: require("./transformers/optional-typeof-symbol"), - coreAliasing: require("./transformers/optional-core-aliasing"), + selfContained: require("./transformers/optional-self-contained"), undefinedToVoid: require("./transformers/optional-undefined-to-void"), undeclaredVariableCheck: require("./transformers/optional-undeclared-variable-check"), diff --git a/lib/6to5/transformation/transformers/optional-core-aliasing.js b/lib/6to5/transformation/transformers/optional-self-contained.js similarity index 81% rename from lib/6to5/transformation/transformers/optional-core-aliasing.js rename to lib/6to5/transformation/transformers/optional-self-contained.js index 90bb4621a7..5d4ec3ff1e 100644 --- a/lib/6to5/transformation/transformers/optional-core-aliasing.js +++ b/lib/6to5/transformation/transformers/optional-self-contained.js @@ -23,7 +23,9 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { - file._coreId = file.addImport("core-js/library", "core"); + file.opts.runtime = file.addImport("6to5-runtime/helpers", "to5Runtime").name; + file._coreId = file.addImport("6to5-runtime/core-js", "core"); + file._regeneratorId = file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); }, exit: function (ast, file) { @@ -67,3 +69,9 @@ exports.ast = { }); } }; + +exports.Identifier = function (node, parent, scope, context, file) { + if (node.name === "regeneratorRuntime" && t.isReferenced(node, parent)) { + node.name = file._regeneratorId; + } +}; 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 deleted file mode 100644 index 7c6e3f9ba2..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/expected.js +++ /dev/null @@ -1,15 +0,0 @@ -"use strict"; - -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); - -obj.constructor === Object; -obj.constructor === _core.Promise; - -_core.Symbol(); -_core.Symbol("test"); - -new _core.Map(); diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js deleted file mode 100644 index 10aa8799ed..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/actual.js +++ /dev/null @@ -1 +0,0 @@ -var [a, [b], [c], d] = ["hello", [", ", "junk"], ["world"]]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js deleted file mode 100644 index fc43ff5423..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-destructuring/expected.js +++ /dev/null @@ -1,34 +0,0 @@ -"use strict"; - -var _slicedToArray = function (arr, i) { - if (Array.isArray(arr)) { - return arr; - } else { - var _arr = []; - - for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { - _arr.push(_step.value); - - if (i && _arr.length === i) break; - } - - return _arr; - } -}; - -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); - -var _ref = ["hello", [", ", "junk"], ["world"]]; - -var a = _ref[0]; -var _ref$1 = _slicedToArray(_ref[1], 1); - -var b = _ref$1[0]; -var _ref$2 = _slicedToArray(_ref[2], 1); - -var c = _ref$2[0]; -var d = _ref[3]; 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 deleted file mode 100644 index 3bf1f53a05..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/expected.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; - -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(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/actual.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js deleted file mode 100644 index 5ebb80da66..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/actual.js +++ /dev/null @@ -1 +0,0 @@ -var lyrics = ["head", "and", "toes", ...parts]; diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js b/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js deleted file mode 100644 index 4d00347582..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es6-spread/expected.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; - -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 lyrics = ["head", "and", "toes"].concat(_toArray(parts)); diff --git a/test/fixtures/transformation/optional-core-aliasing/options.json b/test/fixtures/transformation/optional-core-aliasing/options.json deleted file mode 100644 index 371b5f0b20..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "optional": ["coreAliasing"], - "experimental": true -} diff --git a/test/fixtures/transformation/optional-core-aliasing/aliased-constructors/actual.js b/test/fixtures/transformation/optional-self-contained/aliased-constructors/actual.js similarity index 100% rename from test/fixtures/transformation/optional-core-aliasing/aliased-constructors/actual.js rename to test/fixtures/transformation/optional-self-contained/aliased-constructors/actual.js diff --git a/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js new file mode 100644 index 0000000000..2242afb8f8 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js @@ -0,0 +1,15 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime/helpers"); + +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + +obj.constructor === Object; +obj.constructor === _core.Promise; + +_core.Symbol(); +_core.Symbol("test"); + +new _core.Map(); diff --git a/test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js b/test/fixtures/transformation/optional-self-contained/es6-for-of/actual.js similarity index 100% rename from test/fixtures/transformation/optional-core-aliasing/es6-for-of/actual.js rename to test/fixtures/transformation/optional-self-contained/es6-for-of/actual.js diff --git a/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js new file mode 100644 index 0000000000..4d781e1d60 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime/helpers"); + +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + +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/es7-array-comprehensions/actual.js b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/actual.js similarity index 100% rename from test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/actual.js rename to test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/actual.js diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js similarity index 58% rename from test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js rename to test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js index 01333ca8f9..374a18cb98 100644 --- a/test/fixtures/transformation/optional-core-aliasing/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js @@ -1,10 +1,10 @@ "use strict"; -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; +var _to5Runtime = require("6to5-runtime/helpers"); -var _core = _interopRequire(require("core-js/library")); +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); var arr = (function () { var _arr = []; diff --git a/test/fixtures/transformation/optional-self-contained/full/actual.js b/test/fixtures/transformation/optional-self-contained/full/actual.js new file mode 100644 index 0000000000..3e8b1bcdbc --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/full/actual.js @@ -0,0 +1,6 @@ +import foo, * as bar from "someModule"; + +export const myWord = Symbol("abc"); +export function* giveWord () { + yield myWord; +} diff --git a/test/fixtures/transformation/optional-self-contained/full/expected.js b/test/fixtures/transformation/optional-self-contained/full/expected.js new file mode 100644 index 0000000000..2fe954e8df --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/full/expected.js @@ -0,0 +1,27 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime/helpers"); + +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + +var giveWord = _regeneratorRuntime.mark(function giveWord() { + return _regeneratorRuntime.wrap(function giveWord$(context$1$0) { + while (1) switch (context$1$0.prev = context$1$0.next) { + case 0: + context$1$0.next = 2; + return myWord; + case 2: + case "end": + return context$1$0.stop(); + } + }, giveWord, this); +}); + +exports.giveWord = giveWord; +var foo = _to5Runtime.interopRequire(require("someModule")); + +var bar = _to5Runtime.interopRequireWildcard(require("someModule")); + +var myWord = exports.myWord = _core.Symbol("abc"); diff --git a/test/fixtures/transformation/optional-self-contained/options.json b/test/fixtures/transformation/optional-self-contained/options.json new file mode 100644 index 0000000000..079cf304ad --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/options.json @@ -0,0 +1,4 @@ +{ + "optional": ["selfContained"], + "experimental": true +} diff --git a/test/fixtures/transformation/optional-self-contained/regenerator-runtime/actual.js b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/actual.js new file mode 100644 index 0000000000..ae6cc4d8c8 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/actual.js @@ -0,0 +1,2 @@ +void function* () { +}; diff --git a/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js new file mode 100644 index 0000000000..e7fa729a14 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/regenerator-runtime/expected.js @@ -0,0 +1,17 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime/helpers"); + +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + +void _regeneratorRuntime.mark(function callee$0$0() { + return _regeneratorRuntime.wrap(function callee$0$0$(context$1$0) { + while (1) switch (context$1$0.prev = context$1$0.next) { + case 0: + case "end": + return context$1$0.stop(); + } + }, callee$0$0, this); +}); diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/actual.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/actual.js new file mode 100644 index 0000000000..09493dc9be --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/actual.js @@ -0,0 +1 @@ +import foo from "someModule"; diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js new file mode 100644 index 0000000000..292e6b96cf --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js @@ -0,0 +1,5 @@ +define(["exports", "6to5-runtime/helpers", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime, _core, _regeneratorRuntime, _someModule) { + "use strict"; + + var foo = _to5Runtime.interopRequire(_someModule); +}); diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/options.json b/test/fixtures/transformation/optional-self-contained/runtime-amd/options.json new file mode 100644 index 0000000000..b76b119f33 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/options.json @@ -0,0 +1,3 @@ +{ + "modules": "amd" +} diff --git a/test/fixtures/transformation/optional-self-contained/runtime-commonjs/actual.js b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/actual.js new file mode 100644 index 0000000000..9e9515cece --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/actual.js @@ -0,0 +1 @@ +import * as foo from "someModule"; diff --git a/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js new file mode 100644 index 0000000000..d7a8de73f8 --- /dev/null +++ b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _to5Runtime = require("6to5-runtime/helpers"); + +var _core = require("6to5-runtime/core-js"); + +var _regeneratorRuntime = require("6to5-runtime/regenerator"); + +var foo = _to5Runtime.interopRequireWildcard(require("someModule"));