Skip to content

Commit

Permalink
[Fix] Generate webpack-compatible output with static string arg
Browse files Browse the repository at this point in the history
Closes #88.
  • Loading branch information
nicolo-ribaudo authored and ljharb committed Apr 21, 2020
1 parent 6937e76 commit cedd31d
Show file tree
Hide file tree
Showing 40 changed files with 311 additions and 83 deletions.
22 changes: 18 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,37 @@ 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)) {
return;
}
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);
};
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/basic-import/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -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'));
});
2 changes: 1 addition & 1 deletion test/fixtures/basic-import/expected.6.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const testModule = Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
const testModule = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module')));
2 changes: 1 addition & 1 deletion test/fixtures/basic-import/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const testModule = Promise.resolve('test-module').then(s => require(s));
const testModule = Promise.resolve().then(() => require('test-module'));
4 changes: 2 additions & 2 deletions test/fixtures/basic-import/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -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'));
});
2 changes: 1 addition & 1 deletion test/fixtures/basic-import/expected.7.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const testModule = Promise.resolve('test-module').then(s => babelHelpers.interopRequireWildcard(require(s)));
const testModule = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require('test-module')));
2 changes: 1 addition & 1 deletion test/fixtures/basic-import/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const testModule = Promise.resolve('test-module').then(s => require(s));
const testModule = Promise.resolve().then(() => require('test-module'));
20 changes: 10 additions & 10 deletions test/fixtures/chained-import/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -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 () {});
4 changes: 2 additions & 2 deletions test/fixtures/chained-import/expected.6.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/fixtures/chained-import/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -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(() => {});
20 changes: 10 additions & 10 deletions test/fixtures/chained-import/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -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 () {});
4 changes: 2 additions & 2 deletions test/fixtures/chained-import/expected.7.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/fixtures/chained-import/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -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(() => {});
12 changes: 9 additions & 3 deletions test/fixtures/dynamic-argument/actual.js
Original file line number Diff line number Diff line change
@@ -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"); }
72 changes: 69 additions & 3 deletions test/fixtures/dynamic-argument/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 11 additions & 3 deletions test/fixtures/dynamic-argument/expected.6.js
Original file line number Diff line number Diff line change
@@ -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)));
}
14 changes: 11 additions & 3 deletions test/fixtures/dynamic-argument/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -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));
}
61 changes: 59 additions & 2 deletions test/fixtures/dynamic-argument/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 9 additions & 2 deletions test/fixtures/dynamic-argument/expected.7.js
Original file line number Diff line number Diff line change
@@ -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)));
}
11 changes: 9 additions & 2 deletions test/fixtures/dynamic-argument/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -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));
}
8 changes: 4 additions & 4 deletions test/fixtures/import-with-comment/expected.6.es2015.js
Original file line number Diff line number Diff line change
@@ -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'));
});
4 changes: 2 additions & 2 deletions test/fixtures/import-with-comment/expected.6.js
Original file line number Diff line number Diff line change
@@ -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')));
4 changes: 2 additions & 2 deletions test/fixtures/import-with-comment/expected.6.noInterop.js
Original file line number Diff line number Diff line change
@@ -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'));
8 changes: 4 additions & 4 deletions test/fixtures/import-with-comment/expected.7.es2015.js
Original file line number Diff line number Diff line change
@@ -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'));
});
4 changes: 2 additions & 2 deletions test/fixtures/import-with-comment/expected.7.js
Original file line number Diff line number Diff line change
@@ -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')));
4 changes: 2 additions & 2 deletions test/fixtures/import-with-comment/expected.7.noInterop.js
Original file line number Diff line number Diff line change
@@ -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'));
Loading

0 comments on commit cedd31d

Please sign in to comment.