diff --git a/packages/babel-plugin-transform-modules-systemjs/src/index.js b/packages/babel-plugin-transform-modules-systemjs/src/index.js index f30daf04b5..b76a6d4648 100644 --- a/packages/babel-plugin-transform-modules-systemjs/src/index.js +++ b/packages/babel-plugin-transform-modules-systemjs/src/index.js @@ -11,9 +11,7 @@ const buildTemplate = template(` BEFORE_BODY; return { setters: SETTERS, - execute: function () { - BODY; - } + execute: EXECUTE, }; }); `); @@ -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( @@ -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), }), diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/input.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/input.mjs new file mode 100644 index 0000000000..5987e69429 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/input.mjs @@ -0,0 +1 @@ +async () => await test; diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/output.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/output.mjs new file mode 100644 index 0000000000..1da3c2b626 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/output.mjs @@ -0,0 +1,10 @@ +System.register([], function (_export, _context) { + "use strict"; + + return { + setters: [], + execute: function () { + async () => await test; + } + }; +}); diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/options.json b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/options.json new file mode 100644 index 0000000000..39c5d8d3f5 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", "transform-modules-systemjs", "syntax-top-level-await"] +} diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/input.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/input.mjs new file mode 100644 index 0000000000..87669a0caa --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/input.mjs @@ -0,0 +1,3 @@ +if (maybe) { + await test; +} diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/output.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/output.mjs new file mode 100644 index 0000000000..0a912481b6 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/output.mjs @@ -0,0 +1,12 @@ +System.register([], function (_export, _context) { + "use strict"; + + return { + setters: [], + execute: async function () { + if (maybe) { + await test; + } + } + }; +}); diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/input.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/input.mjs new file mode 100644 index 0000000000..c2a4f87ba7 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/input.mjs @@ -0,0 +1,2 @@ +await test; + diff --git a/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/output.mjs b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/output.mjs new file mode 100644 index 0000000000..5af85cee63 --- /dev/null +++ b/packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/output.mjs @@ -0,0 +1,10 @@ +System.register([], function (_export, _context) { + "use strict"; + + return { + setters: [], + execute: async function () { + await test; + } + }; +});