Skip to content

Commit

Permalink
Improve dynamic import spec compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSodaSea committed Dec 19, 2022
1 parent d842911 commit 00446f1
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 28 deletions.
@@ -1,5 +1,5 @@
define(["require"], function (_require) {
"use strict";

var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
var modP = Promise.resolve().then(() => new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)));
});
@@ -1,3 +1,3 @@
define(["require"], function (_require) {
var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(imported), _reject));
var modP = Promise.resolve().then(() => new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(imported), _reject)));
});
@@ -1,3 +1,3 @@
define(["require"], function (_require) {
var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
var modP = Promise.resolve().then(() => new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)));
});
@@ -0,0 +1 @@
import(2);
@@ -0,0 +1,3 @@
define(["require"], function (_require) {
new Promise(r => r(`${2}`)).then(s => new Promise((_resolve, _reject) => _require([s], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)));
});
Expand Up @@ -8,5 +8,5 @@ define(["require", "exports", "foo"], function (_require, _exports, _foo) {
_foo = babelHelpers.interopRequireDefault(_foo);
var mod;
_exports.mod = mod;
new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)).then(m => _exports.mod = mod = m);
Promise.resolve().then(() => new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject))).then(m => _exports.mod = mod = m);
});
@@ -0,0 +1 @@
module.exports = 1;
@@ -0,0 +1,7 @@
let i = 0;
const promise = expect(import({ toString: () => `./${++i}.js` }))
.resolves.toHaveProperty("default", 1);
expect(i).toBe(1);
++i;
expect(i).toBe(2);
return promise;
@@ -0,0 +1,5 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -0,0 +1,2 @@
return expect(import({ toString: () => { throw "toString failed"; } }))
.rejects.toBe("toString failed");
@@ -0,0 +1,5 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -1 +1 @@
Promise.resolve(`${2}`).then(s => babelHelpers.interopRequireWildcard(require(s)));
new Promise(r => r(`${2}`)).then(s => babelHelpers.interopRequireWildcard(require(s)));
@@ -0,0 +1 @@
import(2);
@@ -0,0 +1,10 @@
System.register([], function (_export, _context) {
"use strict";

return {
setters: [],
execute: function () {
new Promise(r => r(`${2}`)).then(s => _context.import(s));
}
};
});
32 changes: 24 additions & 8 deletions packages/babel-plugin-transform-modules-amd/src/index.ts
Expand Up @@ -98,15 +98,31 @@ export default declare<State>((api, options: Options) => {
let result: t.Node = t.identifier("imported");
if (!noInterop) result = wrapInterop(path, result, "namespace");

const source = getDynamicImportSource(path.node);

path.replaceWith(
template.expression.ast`
new Promise((${resolveId}, ${rejectId}) =>
${requireId}(
[${getDynamicImportSource(path.node)}],
imported => ${t.cloneNode(resolveId)}(${result}),
${t.cloneNode(rejectId)}
)
)`,
t.isStringLiteral(source) ||
(t.isTemplateLiteral(source) && source.quasis.length === 0)
? template.expression.ast`
Promise.resolve()
.then(() => new Promise((${resolveId}, ${rejectId}) =>
${requireId}(
[${source}],
imported => ${t.cloneNode(resolveId)}(${result}),
${t.cloneNode(rejectId)}
)
))
`
: template.expression.ast`
new Promise(r => r(${source}))
.then(s => new Promise((${resolveId}, ${rejectId}) =>
${requireId}(
[s],
imported => ${t.cloneNode(resolveId)}(${result}),
${t.cloneNode(rejectId)}
)
))
`,
);
},

Expand Down
Expand Up @@ -29,9 +29,8 @@ export function transformDynamicImport(
Promise.resolve().then(() => ${buildRequire(source, file)})
`
: template.expression.ast`
Promise.resolve(${source}).then(
s => ${buildRequire(t.identifier("s"), file)}
)
new Promise(r => r(${source}))
.then(s => ${buildRequire(t.identifier("s"), file)})
`;

path.replaceWith(replacement);
Expand Down
23 changes: 16 additions & 7 deletions packages/babel-plugin-transform-modules-systemjs/src/index.ts
Expand Up @@ -273,14 +273,23 @@ export default declare<PluginState>((api, options: Options) => {
}
}

const source = getDynamicImportSource(path.node);
const context = t.identifier(state.contextIdent);

path.replaceWith(
t.callExpression(
t.memberExpression(
t.identifier(state.contextIdent),
t.identifier("import"),
),
[getDynamicImportSource(path.node)],
),
t.isStringLiteral(source) ||
(t.isTemplateLiteral(source) && source.quasis.length === 0)
? t.callExpression(
t.memberExpression(
t.identifier(state.contextIdent),
t.identifier("import"),
),
[source],
)
: template.expression.ast`
new Promise(r => r(${source}))
.then(s => ${context}.import(s))
`,
);
}
},
Expand Down
@@ -1,7 +1,9 @@
define(["require"], function (_require) {
new Promise(function (_resolve, _reject) {
return _require(["foo"], function (imported) {
return _resolve(babelHelpers.interopRequireWildcard(imported));
}, _reject);
Promise.resolve().then(function () {
return new Promise(function (_resolve, _reject) {
return _require(["foo"], function (imported) {
return _resolve(babelHelpers.interopRequireWildcard(imported));
}, _reject);
});
});
});
@@ -1,3 +1,3 @@
define(["require"], function (_require) {
new Promise((_resolve, _reject) => _require(["foo"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject));
Promise.resolve().then(() => new Promise((_resolve, _reject) => _require(["foo"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)));
});

0 comments on commit 00446f1

Please sign in to comment.