From c05b2c8b6177f6b32dd2cbccd06a4b6edd9abaca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 26 Oct 2019 11:39:49 +0200 Subject: [PATCH 1/3] Add docs for top level await parsing --- docs/parser.md | 8 +++-- docs/plugin-syntax-top-level-await.md | 49 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 docs/plugin-syntax-top-level-await.md diff --git a/docs/parser.md b/docs/parser.md index 888bfb0592..803817a4c9 100644 --- a/docs/parser.md +++ b/docs/parser.md @@ -34,9 +34,10 @@ mind. When in doubt, use `.parse()`. declarations can only appear at a program's top level. Setting this option to `true` allows them anywhere where a statement is allowed. -- **allowAwaitOutsideFunction**: By default, `await` use is not allowed - outside of an async function. Set this to `true` to accept such - code. +- **allowAwaitOutsideFunction**: By default, `await` use is only allowed + inside of an async function or, when the `topLevelAwait` plugin is enabled, + in the top-level scope of modules. Set this to `true` to also acept it in the + top-level scope of scripts. - **allowReturnOutsideFunction**: By default, a return statement at the top level raises an error. Set this to `true` to accept such @@ -174,6 +175,7 @@ require("@babel/parser").parse("code", { | `partialApplication` ([proposal](https://github.com/babel/proposals/issues/32)) | `f(?, a)` | | `pipelineOperator` ([proposal](https://github.com/babel/proposals/issues/29)) | a |> b | | `throwExpressions` ([proposal](https://github.com/babel/proposals/issues/23)) | `() => throw new Error("")` | +| `topLevelAwait` ([proposal])(https://github.com/tc39/proposal-top-level-await/) | `await promise` in modules | #### Plugins options diff --git a/docs/plugin-syntax-top-level-await.md b/docs/plugin-syntax-top-level-await.md new file mode 100644 index 0000000000..01a170310c --- /dev/null +++ b/docs/plugin-syntax-top-level-await.md @@ -0,0 +1,49 @@ +--- +id: babel-plugin-syntax-top-level-await +title: @babel/plugin-syntax-top-level-await +sidebar_label: syntax-top-level-await +--- + +> #### Syntax only +> +> This plugin only enables parsing of this feature. Babel doesn't support transforming +> top-level await, but you can use Rollup's `experimentalTopLevelAwait` or webpack@5's +> `experiments.topLevelAwait` options. + +```js +const val = await promise; + +export { val }; +``` + +## Installation + +```sh +npm install --save-dev @babel/plugin-syntax-top-level-await +``` + +## Usage + +### Via `.babelrc` (Recommended) + +**.babelrc** + +```json +{ + "plugins": ["@babel/plugin-syntax-top-level-await"] +} +``` + +### Via CLI + +```sh +babel --plugins @babel/plugin-syntax-top-level-await script.js +``` + +### Via Node API + +```javascript +require("@babel/core").transform("code", { + plugins: ["@babel/plugin-syntax-top-level-await"], +}); +``` From 232c5ae2d0e307c44d12da235f3cf9a18e7ea141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 29 Oct 2019 22:25:47 +0100 Subject: [PATCH 2/3] Typo Co-Authored-By: Brian Ng --- docs/parser.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/parser.md b/docs/parser.md index 803817a4c9..a40b7ebc4f 100644 --- a/docs/parser.md +++ b/docs/parser.md @@ -36,7 +36,7 @@ mind. When in doubt, use `.parse()`. - **allowAwaitOutsideFunction**: By default, `await` use is only allowed inside of an async function or, when the `topLevelAwait` plugin is enabled, - in the top-level scope of modules. Set this to `true` to also acept it in the + in the top-level scope of modules. Set this to `true` to also accept it in the top-level scope of scripts. - **allowReturnOutsideFunction**: By default, a return statement at From 1280caff5b1158f5715f2210fdbd2ed562edaaea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 5 Nov 2019 12:22:47 +0100 Subject: [PATCH 3/3] Update docs/plugin-syntax-top-level-await.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Huáng Jùnliàng --- docs/plugin-syntax-top-level-await.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/plugin-syntax-top-level-await.md b/docs/plugin-syntax-top-level-await.md index 01a170310c..d7ba1e4d9d 100644 --- a/docs/plugin-syntax-top-level-await.md +++ b/docs/plugin-syntax-top-level-await.md @@ -43,7 +43,7 @@ babel --plugins @babel/plugin-syntax-top-level-await script.js ### Via Node API ```javascript -require("@babel/core").transform("code", { +require("@babel/core").transform(code, { plugins: ["@babel/plugin-syntax-top-level-await"], }); ```