From cedd31db4ba264879a10b363f97d2884300df8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 21 Apr 2020 17:26:32 +0200 Subject: [PATCH] [Fix] Generate webpack-compatible output with static string arg Closes #88. --- src/utils.js | 22 ++++-- .../basic-import/expected.6.es2015.js | 4 +- test/fixtures/basic-import/expected.6.js | 2 +- .../basic-import/expected.6.noInterop.js | 2 +- .../basic-import/expected.7.es2015.js | 4 +- test/fixtures/basic-import/expected.7.js | 2 +- .../basic-import/expected.7.noInterop.js | 2 +- .../chained-import/expected.6.es2015.js | 20 +++--- test/fixtures/chained-import/expected.6.js | 4 +- .../chained-import/expected.6.noInterop.js | 4 +- .../chained-import/expected.7.es2015.js | 20 +++--- test/fixtures/chained-import/expected.7.js | 4 +- .../chained-import/expected.7.noInterop.js | 4 +- test/fixtures/dynamic-argument/actual.js | 12 +++- .../dynamic-argument/expected.6.es2015.js | 72 ++++++++++++++++++- test/fixtures/dynamic-argument/expected.6.js | 14 +++- .../dynamic-argument/expected.6.noInterop.js | 14 +++- .../dynamic-argument/expected.7.es2015.js | 61 +++++++++++++++- test/fixtures/dynamic-argument/expected.7.js | 11 ++- .../dynamic-argument/expected.7.noInterop.js | 11 ++- .../import-with-comment/expected.6.es2015.js | 8 +-- .../import-with-comment/expected.6.js | 4 +- .../expected.6.noInterop.js | 4 +- .../import-with-comment/expected.7.es2015.js | 8 +-- .../import-with-comment/expected.7.js | 4 +- .../expected.7.noInterop.js | 4 +- .../nested-import/expected.6.es2015.js | 4 +- test/fixtures/nested-import/expected.6.js | 2 +- .../nested-import/expected.6.noInterop.js | 2 +- .../nested-import/expected.7.es2015.js | 4 +- test/fixtures/nested-import/expected.7.js | 2 +- .../nested-import/expected.7.noInterop.js | 2 +- test/fixtures/template-argument/actual.js | 5 ++ .../template-argument/expected.6.es2015.js | 13 ++++ test/fixtures/template-argument/expected.6.js | 5 ++ .../template-argument/expected.6.noInterop.js | 5 ++ .../template-argument/expected.7.es2015.js | 19 +++++ test/fixtures/template-argument/expected.7.js | 3 + .../template-argument/expected.7.noInterop.js | 3 + test/index.js | 4 +- 40 files changed, 311 insertions(+), 83 deletions(-) create mode 100644 test/fixtures/template-argument/actual.js create mode 100644 test/fixtures/template-argument/expected.6.es2015.js create mode 100644 test/fixtures/template-argument/expected.6.js create mode 100644 test/fixtures/template-argument/expected.6.noInterop.js create mode 100644 test/fixtures/template-argument/expected.7.es2015.js create mode 100644 test/fixtures/template-argument/expected.7.js create mode 100644 test/fixtures/template-argument/expected.7.noInterop.js diff --git a/src/utils.js b/src/utils.js index cff988c..26b9799 100644 --- a/src/utils.js +++ b/src/utils.js @@ -15,11 +15,22 @@ export function getImportSource(t, callNode) { } export function createDynamicImportTransform({ template, types: t }) { - const buildImport = template('Promise.resolve(SOURCE).then(s => INTEROP(require(s)))'); - const buildImportNoInterop = template('Promise.resolve(SOURCE).then(s => require(s))'); + const builders = { + static: { + interop: template('Promise.resolve().then(() => INTEROP(require(SOURCE)))'), + noInterop: template('Promise.resolve().then(() => require(SOURCE))'), + }, + dynamic: { + interop: template('Promise.resolve(SOURCE).then(s => INTEROP(require(s)))'), + noInterop: template('Promise.resolve(SOURCE).then(s => require(s))'), + }, + }; const visited = typeof WeakSet === 'function' && new WeakSet(); + const isString = (node) => t.isStringLiteral(node) + || (t.isTemplateLiteral(node) && node.expressions.length === 0); + return (context, path) => { if (visited) { if (visited.has(path)) { @@ -27,11 +38,14 @@ export function createDynamicImportTransform({ template, types: t }) { } visited.add(path); } + const SOURCE = getImportSource(t, path.parent); + const builder = isString(SOURCE) ? builders.static : builders.dynamic; + const newImport = context.opts.noInterop - ? buildImportNoInterop({ SOURCE }) - : buildImport({ SOURCE, INTEROP: context.addHelper('interopRequireWildcard') }); + ? builder.noInterop({ SOURCE }) + : builder.interop({ SOURCE, INTEROP: context.addHelper('interopRequireWildcard') }); path.parentPath.replaceWith(newImport); }; diff --git a/test/fixtures/basic-import/expected.6.es2015.js b/test/fixtures/basic-import/expected.6.es2015.js index 5810006..39643cc 100644 --- a/test/fixtures/basic-import/expected.6.es2015.js +++ b/test/fixtures/basic-import/expected.6.es2015.js @@ -1,3 +1,3 @@ -var testModule = Promise.resolve('test-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +var testModule = Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module')); }); diff --git a/test/fixtures/basic-import/expected.6.js b/test/fixtures/basic-import/expected.6.js index 1b2fd4d..268cd06 100644 --- a/test/fixtures/basic-import/expected.6.js +++ b/test/fixtures/basic-import/expected.6.js @@ -1 +1 @@ -const testModule = Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s))); +const testModule = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); diff --git a/test/fixtures/basic-import/expected.6.noInterop.js b/test/fixtures/basic-import/expected.6.noInterop.js index c3b46b3..53febba 100644 --- a/test/fixtures/basic-import/expected.6.noInterop.js +++ b/test/fixtures/basic-import/expected.6.noInterop.js @@ -1 +1 @@ -const testModule = Promise.resolve('test-module').then(s => require(s)); +const testModule = Promise.resolve().then(() => require('test-module')); diff --git a/test/fixtures/basic-import/expected.7.es2015.js b/test/fixtures/basic-import/expected.7.es2015.js index 5810006..39643cc 100644 --- a/test/fixtures/basic-import/expected.7.es2015.js +++ b/test/fixtures/basic-import/expected.7.es2015.js @@ -1,3 +1,3 @@ -var testModule = Promise.resolve('test-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +var testModule = Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module')); }); diff --git a/test/fixtures/basic-import/expected.7.js b/test/fixtures/basic-import/expected.7.js index 1b2fd4d..268cd06 100644 --- a/test/fixtures/basic-import/expected.7.js +++ b/test/fixtures/basic-import/expected.7.js @@ -1 +1 @@ -const testModule = Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s))); +const testModule = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); diff --git a/test/fixtures/basic-import/expected.7.noInterop.js b/test/fixtures/basic-import/expected.7.noInterop.js index c3b46b3..53febba 100644 --- a/test/fixtures/basic-import/expected.7.noInterop.js +++ b/test/fixtures/basic-import/expected.7.noInterop.js @@ -1 +1 @@ -const testModule = Promise.resolve('test-module').then(s => require(s)); +const testModule = Promise.resolve().then(() => require('test-module')); diff --git a/test/fixtures/chained-import/expected.6.es2015.js b/test/fixtures/chained-import/expected.6.es2015.js index c0846af..c625d11 100644 --- a/test/fixtures/chained-import/expected.6.es2015.js +++ b/test/fixtures/chained-import/expected.6.es2015.js @@ -1,15 +1,15 @@ -Promise.resolve('test-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module')); }).then(function () { - return Promise.resolve('test-module-2').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); + return Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module-2')); }); }); -Promise.all([Promise.resolve('test-1').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); -}), Promise.resolve('test-2').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); -}), Promise.resolve('test-3').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.all([Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-1')); +}), Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-2')); +}), Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-3')); })]).then(function () {}); diff --git a/test/fixtures/chained-import/expected.6.js b/test/fixtures/chained-import/expected.6.js index 93880cb..abeb3aa 100644 --- a/test/fixtures/chained-import/expected.6.js +++ b/test/fixtures/chained-import/expected.6.js @@ -1,3 +1,3 @@ -Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s))).then(() => Promise.resolve('test-module-2').then(s => babelHelpers.interopRequireWildcard(require(s)))); +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))).then(() => Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module-2')))); -Promise.all([Promise.resolve('test-1').then(s => babelHelpers.interopRequireWildcard(require(s))), Promise.resolve('test-2').then(s => babelHelpers.interopRequireWildcard(require(s))), Promise.resolve('test-3').then(s => babelHelpers.interopRequireWildcard(require(s)))]).then(() => {}); +Promise.all([Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-1'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-2'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-3')))]).then(() => {}); diff --git a/test/fixtures/chained-import/expected.6.noInterop.js b/test/fixtures/chained-import/expected.6.noInterop.js index 73b3d3e..d9582eb 100644 --- a/test/fixtures/chained-import/expected.6.noInterop.js +++ b/test/fixtures/chained-import/expected.6.noInterop.js @@ -1,3 +1,3 @@ -Promise.resolve('test-module').then(s => require(s)).then(() => Promise.resolve('test-module-2').then(s => require(s))); +Promise.resolve().then(() => require('test-module')).then(() => Promise.resolve().then(() => require('test-module-2'))); -Promise.all([Promise.resolve('test-1').then(s => require(s)), Promise.resolve('test-2').then(s => require(s)), Promise.resolve('test-3').then(s => require(s))]).then(() => {}); +Promise.all([Promise.resolve().then(() => require('test-1')), Promise.resolve().then(() => require('test-2')), Promise.resolve().then(() => require('test-3'))]).then(() => {}); diff --git a/test/fixtures/chained-import/expected.7.es2015.js b/test/fixtures/chained-import/expected.7.es2015.js index aaf4e92..ab502ea 100644 --- a/test/fixtures/chained-import/expected.7.es2015.js +++ b/test/fixtures/chained-import/expected.7.es2015.js @@ -1,14 +1,14 @@ -Promise.resolve('test-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module')); }).then(function () { - return Promise.resolve('test-module-2').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); + return Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module-2')); }); }); -Promise.all([Promise.resolve('test-1').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); -}), Promise.resolve('test-2').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); -}), Promise.resolve('test-3').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.all([Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-1')); +}), Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-2')); +}), Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-3')); })]).then(function () {}); diff --git a/test/fixtures/chained-import/expected.7.js b/test/fixtures/chained-import/expected.7.js index 773400e..298cd06 100644 --- a/test/fixtures/chained-import/expected.7.js +++ b/test/fixtures/chained-import/expected.7.js @@ -1,2 +1,2 @@ -Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s))).then(() => Promise.resolve('test-module-2').then(s => babelHelpers.interopRequireWildcard(require(s)))); -Promise.all([Promise.resolve('test-1').then(s => babelHelpers.interopRequireWildcard(require(s))), Promise.resolve('test-2').then(s => babelHelpers.interopRequireWildcard(require(s))), Promise.resolve('test-3').then(s => babelHelpers.interopRequireWildcard(require(s)))]).then(() => {}); +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))).then(() => Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module-2')))); +Promise.all([Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-1'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-2'))), Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-3')))]).then(() => {}); diff --git a/test/fixtures/chained-import/expected.7.noInterop.js b/test/fixtures/chained-import/expected.7.noInterop.js index 412c7f1..39364d9 100644 --- a/test/fixtures/chained-import/expected.7.noInterop.js +++ b/test/fixtures/chained-import/expected.7.noInterop.js @@ -1,2 +1,2 @@ -Promise.resolve('test-module').then(s => require(s)).then(() => Promise.resolve('test-module-2').then(s => require(s))); -Promise.all([Promise.resolve('test-1').then(s => require(s)), Promise.resolve('test-2').then(s => require(s)), Promise.resolve('test-3').then(s => require(s))]).then(() => {}); +Promise.resolve().then(() => require('test-module')).then(() => Promise.resolve().then(() => require('test-module-2'))); +Promise.all([Promise.resolve().then(() => require('test-1')), Promise.resolve().then(() => require('test-2')), Promise.resolve().then(() => require('test-3'))]).then(() => {}); diff --git a/test/fixtures/dynamic-argument/actual.js b/test/fixtures/dynamic-argument/actual.js index 8efb7b5..e1992e5 100644 --- a/test/fixtures/dynamic-argument/actual.js +++ b/test/fixtures/dynamic-argument/actual.js @@ -1,4 +1,10 @@ -const MODULE = Object('test-module'); - import(MODULE); -import(`test-${MODULE}`); + +let i = 0; +import(i++); + +import(fn()); + +async () => import(await "x"); + +function* f() { import(yield "x"); } diff --git a/test/fixtures/dynamic-argument/expected.6.es2015.js b/test/fixtures/dynamic-argument/expected.6.es2015.js index 08b44d2..656c14c 100644 --- a/test/fixtures/dynamic-argument/expected.6.es2015.js +++ b/test/fixtures/dynamic-argument/expected.6.es2015.js @@ -1,8 +1,74 @@ -var MODULE = Object('test-module'); +var _this = this; -Promise.resolve('' + String(MODULE)).then(function (s) { +var _marked = /*#__PURE__*/regeneratorRuntime.mark(f); + +Promise.resolve("" + String(MODULE)).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); +}); + +var i = 0; +Promise.resolve("" + i++).then(function (s) { return babelHelpers.interopRequireWildcard(require(s)); }); -Promise.resolve('test-' + String(MODULE)).then(function (s) { + +Promise.resolve("" + String(fn())).then(function (s) { return babelHelpers.interopRequireWildcard(require(s)); }); + +babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { + return regeneratorRuntime.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _context.t0 = Promise; + _context.t1 = String; + _context.next = 4; + return "x"; + + case 4: + _context.t2 = _context.sent; + _context.t3 = (0, _context.t1)(_context.t2); + _context.t4 = "" + _context.t3; + + _context.t5 = function (s) { + return babelHelpers.interopRequireWildcard(require(s)); + }; + + return _context.abrupt("return", _context.t0.resolve.call(_context.t0, _context.t4).then(_context.t5)); + + case 9: + case "end": + return _context.stop(); + } + } + }, _callee, _this); +})); + +function f() { + return regeneratorRuntime.wrap(function f$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + _context2.t0 = Promise; + _context2.t1 = String; + _context2.next = 4; + return "x"; + + case 4: + _context2.t2 = _context2.sent; + _context2.t3 = (0, _context2.t1)(_context2.t2); + _context2.t4 = "" + _context2.t3; + + _context2.t5 = function (s) { + return babelHelpers.interopRequireWildcard(require(s)); + }; + + _context2.t0.resolve.call(_context2.t0, _context2.t4).then(_context2.t5); + + case 9: + case "end": + return _context2.stop(); + } + } + }, _marked, this); +} diff --git a/test/fixtures/dynamic-argument/expected.6.js b/test/fixtures/dynamic-argument/expected.6.js index fb558be..ca5d680 100644 --- a/test/fixtures/dynamic-argument/expected.6.js +++ b/test/fixtures/dynamic-argument/expected.6.js @@ -1,4 +1,12 @@ -const MODULE = Object('test-module'); - Promise.resolve(`${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); -Promise.resolve(`test-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +let i = 0; +Promise.resolve(`${i++}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +Promise.resolve(`${fn()}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +async () => Promise.resolve(`${await "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +function* f() { + Promise.resolve(`${yield "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); +} diff --git a/test/fixtures/dynamic-argument/expected.6.noInterop.js b/test/fixtures/dynamic-argument/expected.6.noInterop.js index 5e795e7..1b2ddd4 100644 --- a/test/fixtures/dynamic-argument/expected.6.noInterop.js +++ b/test/fixtures/dynamic-argument/expected.6.noInterop.js @@ -1,4 +1,12 @@ -const MODULE = Object('test-module'); - Promise.resolve(`${MODULE}`).then(s => require(s)); -Promise.resolve(`test-${MODULE}`).then(s => require(s)); + +let i = 0; +Promise.resolve(`${i++}`).then(s => require(s)); + +Promise.resolve(`${fn()}`).then(s => require(s)); + +async () => Promise.resolve(`${await "x"}`).then(s => require(s)); + +function* f() { + Promise.resolve(`${yield "x"}`).then(s => require(s)); +} diff --git a/test/fixtures/dynamic-argument/expected.7.es2015.js b/test/fixtures/dynamic-argument/expected.7.es2015.js index d112682..dd715e5 100644 --- a/test/fixtures/dynamic-argument/expected.7.es2015.js +++ b/test/fixtures/dynamic-argument/expected.7.es2015.js @@ -1,7 +1,64 @@ -var MODULE = Object('test-module'); +var _marked = /*#__PURE__*/regeneratorRuntime.mark(f); + Promise.resolve("".concat(MODULE)).then(function (s) { return babelHelpers.interopRequireWildcard(require(s)); }); -Promise.resolve("test-".concat(MODULE)).then(function (s) { +var i = 0; +Promise.resolve("".concat(i++)).then(function (s) { return babelHelpers.interopRequireWildcard(require(s)); }); +Promise.resolve("".concat(fn())).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); +}); + +/*#__PURE__*/ +babelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { + return regeneratorRuntime.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _context.t0 = Promise; + _context.t1 = ""; + _context.next = 4; + return "x"; + + case 4: + _context.t2 = _context.sent; + _context.t3 = _context.t1.concat.call(_context.t1, _context.t2); + return _context.abrupt("return", _context.t0.resolve.call(_context.t0, _context.t3).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); + })); + + case 7: + case "end": + return _context.stop(); + } + } + }, _callee); +})); + +function f() { + return regeneratorRuntime.wrap(function f$(_context2) { + while (1) { + switch (_context2.prev = _context2.next) { + case 0: + _context2.t0 = Promise; + _context2.t1 = ""; + _context2.next = 4; + return "x"; + + case 4: + _context2.t2 = _context2.sent; + _context2.t3 = _context2.t1.concat.call(_context2.t1, _context2.t2); + + _context2.t0.resolve.call(_context2.t0, _context2.t3).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); + }); + + case 7: + case "end": + return _context2.stop(); + } + } + }, _marked); +} diff --git a/test/fixtures/dynamic-argument/expected.7.js b/test/fixtures/dynamic-argument/expected.7.js index fd1e26c..173d1bd 100644 --- a/test/fixtures/dynamic-argument/expected.7.js +++ b/test/fixtures/dynamic-argument/expected.7.js @@ -1,3 +1,10 @@ -const MODULE = Object('test-module'); Promise.resolve(`${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); -Promise.resolve(`test-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); +let i = 0; +Promise.resolve(`${i++}`).then(s => babelHelpers.interopRequireWildcard(require(s))); +Promise.resolve(`${fn()}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +async () => Promise.resolve(`${await "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +function* f() { + Promise.resolve(`${yield "x"}`).then(s => babelHelpers.interopRequireWildcard(require(s))); +} diff --git a/test/fixtures/dynamic-argument/expected.7.noInterop.js b/test/fixtures/dynamic-argument/expected.7.noInterop.js index 4bcb227..3aab83e 100644 --- a/test/fixtures/dynamic-argument/expected.7.noInterop.js +++ b/test/fixtures/dynamic-argument/expected.7.noInterop.js @@ -1,3 +1,10 @@ -const MODULE = Object('test-module'); Promise.resolve(`${MODULE}`).then(s => require(s)); -Promise.resolve(`test-${MODULE}`).then(s => require(s)); +let i = 0; +Promise.resolve(`${i++}`).then(s => require(s)); +Promise.resolve(`${fn()}`).then(s => require(s)); + +async () => Promise.resolve(`${await "x"}`).then(s => require(s)); + +function* f() { + Promise.resolve(`${yield "x"}`).then(s => require(s)); +} diff --git a/test/fixtures/import-with-comment/expected.6.es2015.js b/test/fixtures/import-with-comment/expected.6.es2015.js index 20f75fc..ef0bf55 100644 --- a/test/fixtures/import-with-comment/expected.6.es2015.js +++ b/test/fixtures/import-with-comment/expected.6.es2015.js @@ -1,6 +1,6 @@ -Promise.resolve('my-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('my-module')); }); -Promise.resolve('my-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('my-module')); }); diff --git a/test/fixtures/import-with-comment/expected.6.js b/test/fixtures/import-with-comment/expected.6.js index b3d514e..25c2490 100644 --- a/test/fixtures/import-with-comment/expected.6.js +++ b/test/fixtures/import-with-comment/expected.6.js @@ -1,2 +1,2 @@ -Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s))); -Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s))); +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); diff --git a/test/fixtures/import-with-comment/expected.6.noInterop.js b/test/fixtures/import-with-comment/expected.6.noInterop.js index 3a43811..59473f3 100644 --- a/test/fixtures/import-with-comment/expected.6.noInterop.js +++ b/test/fixtures/import-with-comment/expected.6.noInterop.js @@ -1,2 +1,2 @@ -Promise.resolve('my-module').then(s => require(s)); -Promise.resolve('my-module').then(s => require(s)); +Promise.resolve().then(() => require('my-module')); +Promise.resolve().then(() => require('my-module')); diff --git a/test/fixtures/import-with-comment/expected.7.es2015.js b/test/fixtures/import-with-comment/expected.7.es2015.js index 20f75fc..ef0bf55 100644 --- a/test/fixtures/import-with-comment/expected.7.es2015.js +++ b/test/fixtures/import-with-comment/expected.7.es2015.js @@ -1,6 +1,6 @@ -Promise.resolve('my-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('my-module')); }); -Promise.resolve('my-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('my-module')); }); diff --git a/test/fixtures/import-with-comment/expected.7.js b/test/fixtures/import-with-comment/expected.7.js index b3d514e..25c2490 100644 --- a/test/fixtures/import-with-comment/expected.7.js +++ b/test/fixtures/import-with-comment/expected.7.js @@ -1,2 +1,2 @@ -Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s))); -Promise.resolve('my-module').then(s => babelHelpers.interopRequireWildcard(require(s))); +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('my-module'))); diff --git a/test/fixtures/import-with-comment/expected.7.noInterop.js b/test/fixtures/import-with-comment/expected.7.noInterop.js index 3a43811..59473f3 100644 --- a/test/fixtures/import-with-comment/expected.7.noInterop.js +++ b/test/fixtures/import-with-comment/expected.7.noInterop.js @@ -1,2 +1,2 @@ -Promise.resolve('my-module').then(s => require(s)); -Promise.resolve('my-module').then(s => require(s)); +Promise.resolve().then(() => require('my-module')); +Promise.resolve().then(() => require('my-module')); diff --git a/test/fixtures/nested-import/expected.6.es2015.js b/test/fixtures/nested-import/expected.6.es2015.js index 5b74655..3688bab 100644 --- a/test/fixtures/nested-import/expected.6.es2015.js +++ b/test/fixtures/nested-import/expected.6.es2015.js @@ -1,6 +1,6 @@ function getModule(path) { - return Promise.resolve('test-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); + return Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module')); }); } diff --git a/test/fixtures/nested-import/expected.6.js b/test/fixtures/nested-import/expected.6.js index bafbc16..1dcd38a 100644 --- a/test/fixtures/nested-import/expected.6.js +++ b/test/fixtures/nested-import/expected.6.js @@ -1,5 +1,5 @@ function getModule(path) { - return Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s))); + return Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); } getModule().then(() => {}); diff --git a/test/fixtures/nested-import/expected.6.noInterop.js b/test/fixtures/nested-import/expected.6.noInterop.js index 76bbbd0..dbd114e 100644 --- a/test/fixtures/nested-import/expected.6.noInterop.js +++ b/test/fixtures/nested-import/expected.6.noInterop.js @@ -1,5 +1,5 @@ function getModule(path) { - return Promise.resolve('test-module').then(s => require(s)); + return Promise.resolve().then(() => require('test-module')); } getModule().then(() => {}); diff --git a/test/fixtures/nested-import/expected.7.es2015.js b/test/fixtures/nested-import/expected.7.es2015.js index 5b74655..3688bab 100644 --- a/test/fixtures/nested-import/expected.7.es2015.js +++ b/test/fixtures/nested-import/expected.7.es2015.js @@ -1,6 +1,6 @@ function getModule(path) { - return Promise.resolve('test-module').then(function (s) { - return babelHelpers.interopRequireWildcard(require(s)); + return Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require('test-module')); }); } diff --git a/test/fixtures/nested-import/expected.7.js b/test/fixtures/nested-import/expected.7.js index bafbc16..1dcd38a 100644 --- a/test/fixtures/nested-import/expected.7.js +++ b/test/fixtures/nested-import/expected.7.js @@ -1,5 +1,5 @@ function getModule(path) { - return Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s))); + return Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module'))); } getModule().then(() => {}); diff --git a/test/fixtures/nested-import/expected.7.noInterop.js b/test/fixtures/nested-import/expected.7.noInterop.js index 76bbbd0..dbd114e 100644 --- a/test/fixtures/nested-import/expected.7.noInterop.js +++ b/test/fixtures/nested-import/expected.7.noInterop.js @@ -1,5 +1,5 @@ function getModule(path) { - return Promise.resolve('test-module').then(s => require(s)); + return Promise.resolve().then(() => require('test-module')); } getModule().then(() => {}); diff --git a/test/fixtures/template-argument/actual.js b/test/fixtures/template-argument/actual.js new file mode 100644 index 0000000..6cb90c5 --- /dev/null +++ b/test/fixtures/template-argument/actual.js @@ -0,0 +1,5 @@ +import(`1`); + +import(tag`2`); + +import(`3-${MODULE}`); diff --git a/test/fixtures/template-argument/expected.6.es2015.js b/test/fixtures/template-argument/expected.6.es2015.js new file mode 100644 index 0000000..4e11efe --- /dev/null +++ b/test/fixtures/template-argument/expected.6.es2015.js @@ -0,0 +1,13 @@ +var _templateObject = babelHelpers.taggedTemplateLiteral(["2"], ["2"]); + +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require("1")); +}); + +Promise.resolve("" + String(tag(_templateObject))).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); +}); + +Promise.resolve("3-" + String(MODULE)).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); +}); diff --git a/test/fixtures/template-argument/expected.6.js b/test/fixtures/template-argument/expected.6.js new file mode 100644 index 0000000..0bc6701 --- /dev/null +++ b/test/fixtures/template-argument/expected.6.js @@ -0,0 +1,5 @@ +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`1`))); + +Promise.resolve(`${tag`2`}`).then(s => babelHelpers.interopRequireWildcard(require(s))); + +Promise.resolve(`3-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); diff --git a/test/fixtures/template-argument/expected.6.noInterop.js b/test/fixtures/template-argument/expected.6.noInterop.js new file mode 100644 index 0000000..88085a2 --- /dev/null +++ b/test/fixtures/template-argument/expected.6.noInterop.js @@ -0,0 +1,5 @@ +Promise.resolve().then(() => require(`1`)); + +Promise.resolve(`${tag`2`}`).then(s => require(s)); + +Promise.resolve(`3-${MODULE}`).then(s => require(s)); diff --git a/test/fixtures/template-argument/expected.7.es2015.js b/test/fixtures/template-argument/expected.7.es2015.js new file mode 100644 index 0000000..b8fa654 --- /dev/null +++ b/test/fixtures/template-argument/expected.7.es2015.js @@ -0,0 +1,19 @@ +function _templateObject() { + var data = babelHelpers.taggedTemplateLiteral(["2"]); + + _templateObject = function _templateObject() { + return data; + }; + + return data; +} + +Promise.resolve().then(function () { + return babelHelpers.interopRequireWildcard(require("1")); +}); +Promise.resolve("".concat(tag(_templateObject()))).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); +}); +Promise.resolve("3-".concat(MODULE)).then(function (s) { + return babelHelpers.interopRequireWildcard(require(s)); +}); diff --git a/test/fixtures/template-argument/expected.7.js b/test/fixtures/template-argument/expected.7.js new file mode 100644 index 0000000..ee36c25 --- /dev/null +++ b/test/fixtures/template-argument/expected.7.js @@ -0,0 +1,3 @@ +Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require(`1`))); +Promise.resolve(`${tag`2`}`).then(s => babelHelpers.interopRequireWildcard(require(s))); +Promise.resolve(`3-${MODULE}`).then(s => babelHelpers.interopRequireWildcard(require(s))); diff --git a/test/fixtures/template-argument/expected.7.noInterop.js b/test/fixtures/template-argument/expected.7.noInterop.js new file mode 100644 index 0000000..aa540e1 --- /dev/null +++ b/test/fixtures/template-argument/expected.7.noInterop.js @@ -0,0 +1,3 @@ +Promise.resolve().then(() => require(`1`)); +Promise.resolve(`${tag`2`}`).then(s => require(s)); +Promise.resolve(`3-${MODULE}`).then(s => require(s)); diff --git a/test/index.js b/test/index.js index 6c6db75..fa7159a 100644 --- a/test/index.js +++ b/test/index.js @@ -80,7 +80,9 @@ test('babel-plugin-dynamic-import-node', (t) => { st.end(); }); - if (version === 6 && !process.env.OVERWRITE) { + if (version === 6 && !process.env.OVERWRITE + // The es2015 and env presets have two different output with async functions + && folderName !== 'dynamic-argument') { t.test(`babel ${version} - works with ${folderName} and the es2015 preset`, (st) => { const result = testPlugin( version,