diff --git a/lib/6to5/transformation/file.js b/lib/6to5/transformation/file.js index b20d46a9fc..393da69764 100644 --- a/lib/6to5/transformation/file.js +++ b/lib/6to5/transformation/file.js @@ -18,9 +18,10 @@ var each = require("lodash/collection/each"); var t = require("../types"); function File(opts) { - this.dynamicImportIds = {}; - this.dynamicImported = []; - this.dynamicImports = []; + this.dynamicImportedNoDefault = []; + this.dynamicImportIds = {}; + this.dynamicImported = []; + this.dynamicImports = []; this.dynamicData = {}; this.data = {}; @@ -273,7 +274,7 @@ File.prototype.get = function (key) { } }; -File.prototype.addImport = function (source, name) { +File.prototype.addImport = function (source, name, noDefault) { name = name || source; var id = this.dynamicImportIds[name]; @@ -283,7 +284,9 @@ File.prototype.addImport = function (source, name) { var specifiers = [t.importSpecifier(t.identifier("default"), id)]; var declar = t.importDeclaration(specifiers, t.literal(source)); declar._blockHoist = 3; + this.dynamicImported.push(declar); + if (noDefault) this.dynamicImportedNoDefault.push(declar); this.moduleFormatter.importSpecifier(specifiers[0], declar, this.dynamicImports); } diff --git a/lib/6to5/transformation/modules/amd.js b/lib/6to5/transformation/modules/amd.js index d05567668c..004670b6fd 100644 --- a/lib/6to5/transformation/modules/amd.js +++ b/lib/6to5/transformation/modules/amd.js @@ -4,10 +4,10 @@ module.exports = AMDFormatter; var DefaultFormatter = require("./_default"); var CommonFormatter = require("./common"); -var util = require("../../util"); -var t = require("../../types"); var includes = require("lodash/collection/includes"); var values = require("lodash/object/values"); +var util = require("../../util"); +var t = require("../../types"); function AMDFormatter() { CommonFormatter.apply(this, arguments); @@ -81,12 +81,12 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var key = t.getSpecifierName(specifier); var ref = this.getExternalReference(node); - if (includes(this.file.dynamicImported, node)) { + if (includes(this.file.dynamicImportedNoDefault, node)) { // Prevent unnecessary renaming of dynamic imports. this.ids[node.source.value] = ref; } else if (t.isImportBatchSpecifier(specifier)) { // import * as bar from "foo"; - } else if (t.isSpecifierDefault(specifier) && !this.noInteropRequireImport) { + } else if (!includes(this.file.dynamicImported, node) && t.isSpecifierDefault(specifier) && !this.noInteropRequireImport) { // import foo from "foo"; ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); } else { diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index d5c3d5d94e..4333c5294a 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -31,8 +31,8 @@ CommonJSFormatter.prototype.importSpecifier = function (specifier, node, nodes) // import foo from "foo"; if (t.isSpecifierDefault(specifier)) { - if (!includes(this.file.dynamicImported, node)) { - if (this.noInteropRequireImport) { + if (!includes(this.file.dynamicImportedNoDefault, node)) { + if (this.noInteropRequireImport || includes(this.file.dynamicImported, node)) { ref = t.memberExpression(ref, t.identifier("default")); } else { ref = t.callExpression(this.file.addHelper("interop-require"), [ref]); diff --git a/lib/6to5/transformation/templates/self-contained-helpers-head.js b/lib/6to5/transformation/templates/self-contained-helpers-head.js new file mode 100644 index 0000000000..78cc1934e6 --- /dev/null +++ b/lib/6to5/transformation/templates/self-contained-helpers-head.js @@ -0,0 +1,2 @@ +var helpers = exports.default = {}; +exports.__esModule = true; diff --git a/lib/6to5/transformation/transformer.js b/lib/6to5/transformation/transformer.js index 31bf351efb..4361e3784e 100644 --- a/lib/6to5/transformation/transformer.js +++ b/lib/6to5/transformation/transformer.js @@ -55,6 +55,8 @@ Transformer.prototype.normalize = function (transformer) { return; } + if (type === "enter" || type === "exit") return; + if (isFunction(fns)) fns = { enter: fns }; if (!isObject(fns)) return; diff --git a/lib/6to5/transformation/transformers/index.js b/lib/6to5/transformation/transformers/index.js index 118e50352d..155ca3042c 100644 --- a/lib/6to5/transformation/transformers/index.js +++ b/lib/6to5/transformation/transformers/index.js @@ -86,11 +86,12 @@ module.exports = { _declarations: require("./internal/declarations"), _aliasFunctions: require("./internal/alias-functions"), - _moduleFormatter: require("./internal/module-formatter"), "spec.typeofSymbol": require("./spec/typeof-symbol"), "spec.undefinedToVoid": require("./spec/undefined-to-void"), + _moduleFormatter: require("./internal/module-formatter"), + "es3.propertyLiterals": require("./es3/property-literals"), "es3.memberExpressionLiterals": require("./es3/member-expression-literals"), diff --git a/lib/6to5/transformation/transformers/internal/module-formatter.js b/lib/6to5/transformation/transformers/internal/module-formatter.js index da60d60a4b..d1982280c4 100644 --- a/lib/6to5/transformation/transformers/internal/module-formatter.js +++ b/lib/6to5/transformation/transformers/internal/module-formatter.js @@ -2,11 +2,9 @@ var useStrict = require("../../helpers/use-strict"); -exports.post = function (file) { +exports.Program = function (program, parent, scope, file) { if (!file.transformers["es6.modules"].canRun()) return; - var program = file.ast.program; - useStrict.wrap(program, function () { program.body = file.dynamicImports.concat(program.body); }); diff --git a/lib/6to5/transformation/transformers/other/bluebird-coroutines.js b/lib/6to5/transformation/transformers/other/bluebird-coroutines.js index b32a5f14bb..c626f07ec1 100644 --- a/lib/6to5/transformation/transformers/other/bluebird-coroutines.js +++ b/lib/6to5/transformation/transformers/other/bluebird-coroutines.js @@ -15,7 +15,7 @@ exports.Function = function (node, parent, scope, file) { return remapAsyncToGenerator( node, - t.memberExpression(file.addImport("bluebird"), t.identifier("coroutine")), + t.memberExpression(file.addImport("bluebird", null, true), t.identifier("coroutine")), scope ); }; diff --git a/lib/6to5/transformation/transformers/other/self-contained.js b/lib/6to5/transformation/transformers/other/self-contained.js index 0267ef16f9..0c040b5ab0 100644 --- a/lib/6to5/transformation/transformers/other/self-contained.js +++ b/lib/6to5/transformation/transformers/other/self-contained.js @@ -78,8 +78,8 @@ exports.manipulateOptions = function (opts) { if (opts.whitelist.length) opts.whitelist.push("es6.modules"); }; -exports.post = function (file) { - file.scope.traverse(file.ast, astVisitor, file); +exports.Program = function (node, parent, scope, file) { + scope.traverse(node, astVisitor, file); }; exports.pre = function (file) { diff --git a/lib/6to5/transformation/transformers/other/use-strict.js b/lib/6to5/transformation/transformers/other/use-strict.js index fa5462911b..92be48e0f1 100644 --- a/lib/6to5/transformation/transformers/other/use-strict.js +++ b/lib/6to5/transformation/transformers/other/use-strict.js @@ -3,8 +3,7 @@ var useStrict = require("../../helpers/use-strict"); var t = require("../../../types"); -exports.post = function (file) { - var program = file.ast.program; +exports.Program = function (program) { if (!useStrict.has(program)) { program.body.unshift(t.expressionStatement(t.literal("use strict"))); } diff --git a/lib/6to5/traversal/path.js b/lib/6to5/traversal/path.js index f80fda064e..08220853b0 100644 --- a/lib/6to5/traversal/path.js +++ b/lib/6to5/traversal/path.js @@ -109,7 +109,7 @@ TraversalPath.prototype.call = function (key) { if (!node) return; var opts = this.opts; - var fn = opts[key]; + var fn = opts[key] || opts; if (opts[node.type]) fn = opts[node.type][key] || fn; var replacement = fn.call(this, node, this.parent, this.scope, this.state); diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 510fcec17e..8f658885a4 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -109,6 +109,8 @@ exports.template = function (name, nodes, keepExpression) { traverse(template, templateVisitor, null, nodes); } + if (template.body.length > 1) return template.body; + var node = template.body[0]; if (!keepExpression && t.isExpressionStatement(node)) { diff --git a/packages/build-runtime.js b/packages/build-runtime.js index f900403827..be6955f64a 100644 --- a/packages/build-runtime.js +++ b/packages/build-runtime.js @@ -2,6 +2,7 @@ var buildHelpers = require("../lib/6to5/build-helpers"); var transform = require("../lib/6to5/transformation"); +var util = require("../lib/6to5/util"); var fs = require("fs"); var t = require("../lib/6to5/types"); var _ = require("lodash"); @@ -16,8 +17,14 @@ var writeFile = function (filename, content) { fs.writeFileSync(filename, content); }; -var readFile = function (filename) { - return fs.readFileSync(require.resolve(filename), "utf8"); +var readFile = function (filename, defaultify) { + var file = fs.readFileSync(require.resolve(filename), "utf8"); + + if (defaultify) { + file += '\nmodule.exports = { "default": module.exports, __esModule: true };\n'; + } + + return file; }; var updatePackage = function () { @@ -37,10 +44,10 @@ var selfContainify = function (code) { }; var buildHelpers2 = function () { - var body = []; + var body = util.template("self-contained-helpers-head"); var tree = t.program(body); - buildHelpers(body, t.identifier("exports")); + buildHelpers(body, t.identifier("helpers")); return transform.fromAst(tree, null, { optional: ["selfContained"] @@ -48,7 +55,7 @@ var buildHelpers2 = function () { }; writeFile("helpers.js", buildHelpers2()); -writeFile("core-js.js", readFile("core-js/library")); -writeFile("regenerator/index.js", readFile("regenerator-6to5/runtime-module")); +writeFile("core-js.js", readFile("core-js/library", true)); +writeFile("regenerator/index.js", readFile("regenerator-6to5/runtime-module", true)); writeFile("regenerator/runtime.js", selfContainify(readFile("regenerator-6to5/runtime"))); updatePackage(); diff --git a/test/fixtures/transformation/self-contained/aliased-constructors/expected.js b/test/fixtures/transformation/self-contained/aliased-constructors/expected.js index b517b90ea6..31ee1c4d38 100644 --- a/test/fixtures/transformation/self-contained/aliased-constructors/expected.js +++ b/test/fixtures/transformation/self-contained/aliased-constructors/expected.js @@ -1,7 +1,6 @@ "use strict"; -var _core = require("6to5-runtime/core-js"); - +var _core = require("6to5-runtime/core-js")["default"]; obj.constructor === Object; obj.constructor === _core.Promise; diff --git a/test/fixtures/transformation/self-contained/es6-for-of/expected.js b/test/fixtures/transformation/self-contained/es6-for-of/expected.js index 0fdc4cc4f5..bdb985ddf2 100644 --- a/test/fixtures/transformation/self-contained/es6-for-of/expected.js +++ b/test/fixtures/transformation/self-contained/es6-for-of/expected.js @@ -1,7 +1,6 @@ "use strict"; -var _core = require("6to5-runtime/core-js"); - +var _core = require("6to5-runtime/core-js")["default"]; for (var _iterator = _core.$for.getIterator(arr), _step; !(_step = _iterator.next()).done;) { var i = _step.value; } diff --git a/test/fixtures/transformation/self-contained/es7-array-comprehensions/expected.js b/test/fixtures/transformation/self-contained/es7-array-comprehensions/expected.js index 9ea3b708c6..f448a5f2f3 100644 --- a/test/fixtures/transformation/self-contained/es7-array-comprehensions/expected.js +++ b/test/fixtures/transformation/self-contained/es7-array-comprehensions/expected.js @@ -1,7 +1,6 @@ "use strict"; -var _core = require("6to5-runtime/core-js"); - +var _core = require("6to5-runtime/core-js")["default"]; var arr = (function () { var _arr = []; diff --git a/test/fixtures/transformation/self-contained/full/expected.js b/test/fixtures/transformation/self-contained/full/expected.js index 0dc0a4d34f..da668d006d 100644 --- a/test/fixtures/transformation/self-contained/full/expected.js +++ b/test/fixtures/transformation/self-contained/full/expected.js @@ -1,11 +1,8 @@ "use strict"; -var _regeneratorRuntime = require("6to5-runtime/regenerator"); - -var _to5Helpers = require("6to5-runtime/helpers"); - -var _core = require("6to5-runtime/core-js"); - +var _core = require("6to5-runtime/core-js")["default"]; +var _regeneratorRuntime = require("6to5-runtime/regenerator")["default"]; +var _to5Helpers = require("6to5-runtime/helpers")["default"]; 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) { diff --git a/test/fixtures/transformation/self-contained/modules-amd/expected.js b/test/fixtures/transformation/self-contained/modules-amd/expected.js index 2fecce6c30..527a0916cf 100644 --- a/test/fixtures/transformation/self-contained/modules-amd/expected.js +++ b/test/fixtures/transformation/self-contained/modules-amd/expected.js @@ -1,6 +1,6 @@ define(["exports", "foo", "6to5-runtime/helpers"], function (exports, _foo, _to5RuntimeHelpers) { "use strict"; - var _to5Helpers = _to5RuntimeHelpers; + var _to5Helpers = _to5RuntimeHelpers["default"]; var foo = _to5Helpers.interopRequire(_foo); -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/self-contained/modules-common/expected.js b/test/fixtures/transformation/self-contained/modules-common/expected.js index b44940490e..fc54eaa84a 100644 --- a/test/fixtures/transformation/self-contained/modules-common/expected.js +++ b/test/fixtures/transformation/self-contained/modules-common/expected.js @@ -1,5 +1,4 @@ "use strict"; -var _to5Helpers = require("6to5-runtime/helpers"); - -var foo = _to5Helpers.interopRequire(require("foo")); \ No newline at end of file +var _to5Helpers = require("6to5-runtime/helpers")["default"]; +var foo = _to5Helpers.interopRequire(require("foo")); diff --git a/test/fixtures/transformation/self-contained/modules-system/expected.js b/test/fixtures/transformation/self-contained/modules-system/expected.js index 25cbba4dc5..15b44e0942 100644 --- a/test/fixtures/transformation/self-contained/modules-system/expected.js +++ b/test/fixtures/transformation/self-contained/modules-system/expected.js @@ -4,10 +4,10 @@ System.register(["6to5-runtime/helpers"], function (_export) { var _to5Helpers; return { setters: [function (_to5RuntimeHelpers) { - _to5Helpers = _to5RuntimeHelpers; + _to5Helpers = _to5RuntimeHelpers["default"]; }], execute: function () { foo.apply(undefined, _to5Helpers.toConsumableArray(bar)); } }; -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/self-contained/modules-umd/expected.js b/test/fixtures/transformation/self-contained/modules-umd/expected.js index 99db3c218f..f77b369f4e 100644 --- a/test/fixtures/transformation/self-contained/modules-umd/expected.js +++ b/test/fixtures/transformation/self-contained/modules-umd/expected.js @@ -7,6 +7,6 @@ })(function (exports, _foo, _to5RuntimeHelpers) { "use strict"; - var _to5Helpers = _to5RuntimeHelpers; + var _to5Helpers = _to5RuntimeHelpers["default"]; var foo = _to5Helpers.interopRequire(_foo); -}); \ No newline at end of file +}); diff --git a/test/fixtures/transformation/self-contained/regenerator-runtime/expected.js b/test/fixtures/transformation/self-contained/regenerator-runtime/expected.js index 275ec24ac5..2329286ee6 100644 --- a/test/fixtures/transformation/self-contained/regenerator-runtime/expected.js +++ b/test/fixtures/transformation/self-contained/regenerator-runtime/expected.js @@ -1,7 +1,6 @@ "use strict"; -var _regeneratorRuntime = require("6to5-runtime/regenerator"); - +var _regeneratorRuntime = require("6to5-runtime/regenerator")["default"]; 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) { diff --git a/test/fixtures/transformation/self-contained/symbol-iterator-in/expected.js b/test/fixtures/transformation/self-contained/symbol-iterator-in/expected.js index 5c74f87cbf..0abc1db265 100644 --- a/test/fixtures/transformation/self-contained/symbol-iterator-in/expected.js +++ b/test/fixtures/transformation/self-contained/symbol-iterator-in/expected.js @@ -1,5 +1,4 @@ "use strict"; -var _core = require("6to5-runtime/core-js"); - +var _core = require("6to5-runtime/core-js")["default"]; _core.$for.isIterable(Object(arr));