From cfd95a13987bc675c6ad2b775550e33c02d1adb3 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 22 Jan 2015 01:37:28 +1100 Subject: [PATCH] only require imports that are necessary in selfContained --- lib/6to5/transformation/modules/amd.js | 2 +- lib/6to5/transformation/modules/common.js | 2 +- .../transformers/_module-formatter.js | 5 +++++ .../transformation/transformers/es6/modules.js | 11 ++--------- .../transformers/optional/self-contained.js | 16 ++++++++++++---- .../optional/undeclared-variable-check.js | 2 +- .../aliased-constructors/expected.js | 4 ---- .../es6-for-of/expected.js | 4 ---- .../es7-array-comprehensions/expected.js | 4 ---- .../optional-self-contained/full/expected.js | 10 +++++----- .../runtime-amd/expected.js | 4 ++-- .../runtime-commonjs/expected.js | 8 ++------ 12 files changed, 31 insertions(+), 41 deletions(-) diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index 6d06ddbe46..2de2e8c798 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 (_.contains(this.file.dynamicImports, node)) { + if (_.contains(this.file.dynamicImported, node)) { // Prevent unnecessary renaming of dynamic imports. this.ids[node.source.value] = key; return; diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index b65ee6e3e9..ac495947ec 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -33,7 +33,7 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) var ref = util.template("require", { MODULE_NAME: node.source }); - if (!_.contains(this.file.dynamicImports, node)) { + if (!_.contains(this.file.dynamicImported, 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/_module-formatter.js b/lib/6to5/transformation/transformers/_module-formatter.js index 4dcbe1c27f..1d2749aeac 100644 --- a/lib/6to5/transformation/transformers/_module-formatter.js +++ b/lib/6to5/transformation/transformers/_module-formatter.js @@ -1,11 +1,16 @@ "use strict"; +var useStrict = require("../helpers/use-strict"); var transform = require("../transform"); exports.ast = { exit: function (ast, file) { if (!transform.transformers["es6.modules"].canRun(file)) return; + useStrict.wrap(ast.program, function () { + ast.program.body = file.dynamicImports.concat(ast.program.body); + }); + if (file.moduleFormatter.transform) { file.moduleFormatter.transform(ast); } diff --git a/lib/6to5/transformation/transformers/es6/modules.js b/lib/6to5/transformation/transformers/es6/modules.js index adcbe02d9c..8fd20b0d1f 100644 --- a/lib/6to5/transformation/transformers/es6/modules.js +++ b/lib/6to5/transformation/transformers/es6/modules.js @@ -2,12 +2,6 @@ var t = require("../../../types"); -exports.ast = { - before: function (ast, file) { - ast.program.body = file.dynamicImports.concat(ast.program.body); - } -}; - exports.ImportDeclaration = function (node, parent, scope, context, file) { var nodes = []; @@ -20,8 +14,7 @@ exports.ImportDeclaration = function (node, parent, scope, context, file) { } if (nodes.length === 1) { - // inherit `_blockHoist` - // this for `_blockHoist` in File.prototype.addImport + // inherit `_blockHoist` - this is for `_blockHoist` in File.prototype.addImport nodes[0]._blockHoist = node._blockHoist; } @@ -40,7 +33,7 @@ exports.ExportDeclaration = function (node, parent, scope, context, file) { } file.moduleFormatter.exportDeclaration(node, nodes, parent); - } else { + } else if (node.specifiers) { for (var i = 0; i < node.specifiers.length; i++) { file.moduleFormatter.exportSpecifier(node.specifiers[i], node, nodes, parent); } diff --git a/lib/6to5/transformation/transformers/optional/self-contained.js b/lib/6to5/transformation/transformers/optional/self-contained.js index 6c524d0219..332a3b2d23 100644 --- a/lib/6to5/transformation/transformers/optional/self-contained.js +++ b/lib/6to5/transformation/transformers/optional/self-contained.js @@ -62,12 +62,20 @@ exports.optional = true; exports.ast = { enter: function (ast, file) { - file.set("runtimeIdentifier", file.addImport("6to5-runtime/helpers", "to5Runtime")); - file.set("coreIdentifier", file.addImport("6to5-runtime/core-js", "core")); - file.set("regeneratorIdentifier", file.addImport("6to5-runtime/regenerator", "regeneratorRuntime")); + file.setDynamic("runtimeIdentifier", function () { + return file.addImport("6to5-runtime/helpers", "to5Helpers"); + }); + + file.setDynamic("coreIdentifier", function () { + return file.addImport("6to5-runtime/core-js", "core"); + }); + + file.setDynamic("regeneratorIdentifier", function () { + return file.addImport("6to5-runtime/regenerator", "regeneratorRuntime"); + }); }, - exit: function (ast, file) { + after: function (ast, file) { traverse(ast, astVisitor, null, file); } }; diff --git a/lib/6to5/transformation/transformers/optional/undeclared-variable-check.js b/lib/6to5/transformation/transformers/optional/undeclared-variable-check.js index ad6dbeb06c..4655979211 100644 --- a/lib/6to5/transformation/transformers/optional/undeclared-variable-check.js +++ b/lib/6to5/transformation/transformers/optional/undeclared-variable-check.js @@ -2,6 +2,6 @@ exports.optional = true; exports.Identifier = function (node, parent, scope, context, file) { if (!scope.has(node.name, true)) { - throw file.errorWithNode(node, "Reference to undeclared variable"); + throw file.errorWithNode(node, "Reference to undeclared variable", ReferenceError); } }; diff --git a/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js index 2242afb8f8..b517b90ea6 100644 --- a/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js +++ b/test/fixtures/transformation/optional-self-contained/aliased-constructors/expected.js @@ -1,11 +1,7 @@ "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; 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 index 4d781e1d60..0fdc4cc4f5 100644 --- a/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es6-for-of/expected.js @@ -1,11 +1,7 @@ "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-self-contained/es7-array-comprehensions/expected.js b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js index 374a18cb98..9ea3b708c6 100644 --- a/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/optional-self-contained/es7-array-comprehensions/expected.js @@ -1,11 +1,7 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/helpers"); - 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/expected.js b/test/fixtures/transformation/optional-self-contained/full/expected.js index 2fe954e8df..afb25b3150 100644 --- a/test/fixtures/transformation/optional-self-contained/full/expected.js +++ b/test/fixtures/transformation/optional-self-contained/full/expected.js @@ -1,11 +1,11 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/helpers"); - -var _core = require("6to5-runtime/core-js"); +var _to5Helpers = require("6to5-runtime/helpers"); var _regeneratorRuntime = require("6to5-runtime/regenerator"); +var _core = require("6to5-runtime/core-js"); + 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) { @@ -20,8 +20,8 @@ var giveWord = _regeneratorRuntime.mark(function giveWord() { }); exports.giveWord = giveWord; -var foo = _to5Runtime.interopRequire(require("someModule")); +var foo = _to5Helpers.interopRequire(require("someModule")); -var bar = _to5Runtime.interopRequireWildcard(require("someModule")); +var bar = _to5Helpers.interopRequireWildcard(require("someModule")); var myWord = exports.myWord = _core.Symbol("abc"); diff --git a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js index 292e6b96cf..89885bdb66 100644 --- a/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js +++ b/test/fixtures/transformation/optional-self-contained/runtime-amd/expected.js @@ -1,5 +1,5 @@ -define(["exports", "6to5-runtime/helpers", "6to5-runtime/core-js", "6to5-runtime/regenerator", "someModule"], function (exports, _to5Runtime, _core, _regeneratorRuntime, _someModule) { +define(["exports", "someModule", "6to5-runtime/helpers"], function (exports, _someModule, _to5Helpers) { "use strict"; - var foo = _to5Runtime.interopRequire(_someModule); + var foo = _to5Helpers.interopRequire(_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 index d7a8de73f8..ca5d8978bb 100644 --- a/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js +++ b/test/fixtures/transformation/optional-self-contained/runtime-commonjs/expected.js @@ -1,9 +1,5 @@ "use strict"; -var _to5Runtime = require("6to5-runtime/helpers"); +var _to5Helpers = require("6to5-runtime/helpers"); -var _core = require("6to5-runtime/core-js"); - -var _regeneratorRuntime = require("6to5-runtime/regenerator"); - -var foo = _to5Runtime.interopRequireWildcard(require("someModule")); +var foo = _to5Helpers.interopRequireWildcard(require("someModule"));