Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for top level await parsing #2115

Merged
merged 3 commits into from Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/parser.md
Expand Up @@ -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 accept 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
Expand Down Expand Up @@ -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)) | <code>a &#124;> b</code> |
| `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

Expand Down
49 changes: 49 additions & 0 deletions 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", {
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
plugins: ["@babel/plugin-syntax-top-level-await"],
});
```