Skip to content

Commit

Permalink
SystemJS top-level await support (#12163)
Browse files Browse the repository at this point in the history
Co-authored-by: Hu谩ng J霉nli脿ng <jlhwung@gmail.com>
  • Loading branch information
guybedford and JLHwung committed Oct 14, 2020
1 parent d0d1fdb commit 9c7d9c0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/babel-plugin-transform-modules-systemjs/src/index.js
Expand Up @@ -11,9 +11,7 @@ const buildTemplate = template(`
BEFORE_BODY;
return {
setters: SETTERS,
execute: function () {
BODY;
}
execute: EXECUTE,
};
});
`);
Expand Down Expand Up @@ -601,6 +599,18 @@ export default declare((api, options) => {
path.remove();
}

let hasTLA = false;
path.traverse({
AwaitExpression(path) {
hasTLA = true;
path.stop();
},
Function(path) {
path.skip();
},
noScope: true,
});

path.node.body = [
buildTemplate({
SYSTEM_REGISTER: t.memberExpression(
Expand All @@ -610,8 +620,14 @@ export default declare((api, options) => {
BEFORE_BODY: beforeBody,
MODULE_NAME: moduleName,
SETTERS: t.arrayExpression(setters),
EXECUTE: t.functionExpression(
null,
[],
t.blockStatement(path.node.body),
false,
hasTLA,
),
SOURCES: t.arrayExpression(sources),
BODY: path.node.body,
EXPORT_IDENTIFIER: t.identifier(exportIdent),
CONTEXT_IDENTIFIER: t.identifier(contextIdent),
}),
Expand Down
@@ -0,0 +1 @@
async () => await test;
@@ -0,0 +1,10 @@
System.register([], function (_export, _context) {
"use strict";

return {
setters: [],
execute: function () {
async () => await test;
}
};
});
@@ -0,0 +1,3 @@
{
"plugins": ["external-helpers", "transform-modules-systemjs", "syntax-top-level-await"]
}
@@ -0,0 +1,3 @@
if (maybe) {
await test;
}
@@ -0,0 +1,12 @@
System.register([], function (_export, _context) {
"use strict";

return {
setters: [],
execute: async function () {
if (maybe) {
await test;
}
}
};
});
@@ -0,0 +1,2 @@
await test;

@@ -0,0 +1,10 @@
System.register([], function (_export, _context) {
"use strict";

return {
setters: [],
execute: async function () {
await test;
}
};
});

0 comments on commit 9c7d9c0

Please sign in to comment.