From 9c7d9c0fe517a9fa38aa470586c40b933b2b7927 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 14 Oct 2020 11:14:52 -0700 Subject: [PATCH] SystemJS top-level await support (#12163) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Huáng Jùnliàng --- .../src/index.js | 24 +++++++++++++++---- .../test/fixtures/tla/not-tla/input.mjs | 1 + .../test/fixtures/tla/not-tla/output.mjs | 10 ++++++++ .../test/fixtures/tla/options.json | 3 +++ .../test/fixtures/tla/tla-block/input.mjs | 3 +++ .../test/fixtures/tla/tla-block/output.mjs | 12 ++++++++++ .../test/fixtures/tla/tla/input.mjs | 2 ++ .../test/fixtures/tla/tla/output.mjs | 10 ++++++++ 8 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/input.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/not-tla/output.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/options.json create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/input.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla-block/output.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/input.mjs create mode 100644 packages/babel-plugin-transform-modules-systemjs/test/fixtures/tla/tla/output.mjs 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; + } + }; +});