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

Create @babel/plugin-syntax-top-level-await #10573

Merged
merged 1 commit into from Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/babel-plugin-syntax-top-level-await/.npmignore
@@ -0,0 +1,3 @@
node_modules
*.log
src
Copy link
Contributor

@JLHwung JLHwung Oct 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should also ignore test, to be future-proof.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copy-pasted this file from somewhere else in the repo; we should fix many of them.

19 changes: 19 additions & 0 deletions packages/babel-plugin-syntax-top-level-await/README.md
@@ -0,0 +1,19 @@
# @babel/plugin-syntax-top-level-await

> Allow parsing of top-level await in modules

See our website [@babel/plugin-syntax-top-level-await](https://babeljs.io/docs/en/next/babel-plugin-syntax-top-level-await.html) for more information.

## Install

Using npm:

```sh
npm install --save-dev @babel/plugin-syntax-top-level-await
```

or using yarn:

```sh
yarn add @babel/plugin-syntax-top-level-await --dev
```
23 changes: 23 additions & 0 deletions packages/babel-plugin-syntax-top-level-await/package.json
@@ -0,0 +1,23 @@
{
"name": "@babel/plugin-syntax-top-level-await",
"version": "7.6.4",
"description": "Allow parsing of top-level await in modules",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-top-level-await",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"devDependencies": {
"@babel/core": "^7.4.4"
}
}
13 changes: 13 additions & 0 deletions packages/babel-plugin-syntax-top-level-await/src/index.js
@@ -0,0 +1,13 @@
import { declare } from "@babel/helper-plugin-utils";

export default declare(api => {
api.assertVersion(7);

return {
name: "syntax-top-level-await",

manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("topLevelAwait");
},
};
});
1 change: 1 addition & 0 deletions packages/babel-preset-env/package.json
Expand Up @@ -27,6 +27,7 @@
"@babel/plugin-syntax-json-strings": "^7.2.0",
"@babel/plugin-syntax-object-rest-spread": "^7.2.0",
"@babel/plugin-syntax-optional-catch-binding": "^7.2.0",
"@babel/plugin-syntax-top-level-await": "^7.6.4",
"@babel/plugin-transform-arrow-functions": "^7.2.0",
"@babel/plugin-transform-async-to-generator": "^7.5.0",
"@babel/plugin-transform-block-scoped-functions": "^7.2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-env/src/available-plugins.js
Expand Up @@ -6,6 +6,7 @@ export default {
"syntax-json-strings": require("@babel/plugin-syntax-json-strings"),
"syntax-object-rest-spread": require("@babel/plugin-syntax-object-rest-spread"),
"syntax-optional-catch-binding": require("@babel/plugin-syntax-optional-catch-binding"),
"syntax-top-level-await": require("@babel/plugin-syntax-top-level-await"),
"transform-async-to-generator": require("@babel/plugin-transform-async-to-generator"),
"proposal-async-generator-functions": require("@babel/plugin-proposal-async-generator-functions"),
"proposal-dynamic-import": require("@babel/plugin-proposal-dynamic-import"),
Expand Down
12 changes: 12 additions & 0 deletions packages/babel-preset-env/src/index.js
Expand Up @@ -65,11 +65,13 @@ export const getModulesPluginNames = ({
transformations,
shouldTransformESM,
shouldTransformDynamicImport,
shouldParseTopLevelAwait,
}: {
modules: ModuleOption,
transformations: ModuleTransformationsType,
shouldTransformESM: boolean,
shouldTransformDynamicImport: boolean,
shouldParseTopLevelAwait: boolean,
}) => {
const modulesPluginNames = [];
if (modules !== false && transformations[modules]) {
Expand All @@ -95,6 +97,11 @@ export const getModulesPluginNames = ({
} else {
modulesPluginNames.push("syntax-dynamic-import");
}

if (shouldParseTopLevelAwait) {
modulesPluginNames.push("syntax-top-level-await");
}

return modulesPluginNames;
};

Expand Down Expand Up @@ -165,6 +172,10 @@ function supportsDynamicImport(caller) {
return !!(caller && caller.supportsDynamicImport);
}

function supportsTopLevelAwait(caller) {
return !!(caller && caller.supportsTopLevelAwait);
}

export default declare((api, opts) => {
api.assertVersion(7);

Expand Down Expand Up @@ -225,6 +236,7 @@ export default declare((api, opts) => {
modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
shouldTransformDynamicImport:
modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
shouldParseTopLevelAwait: api.caller(supportsTopLevelAwait),
});

const pluginNames = filterItems(
Expand Down
@@ -0,0 +1 @@
await 0;
@@ -0,0 +1,8 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
"supportsTopLevelAwait": true
},
"presets": ["env"]
}
@@ -0,0 +1 @@
await 0;
@@ -0,0 +1 @@
await 0;
@@ -0,0 +1,9 @@
{
"caller": {
"name": "test-fixture",
"supportsStaticESM": true,
"supportsTopLevelAwait": false
},
"presets": ["env"],
"throws": "Can not use keyword 'await' outside an async function (1:0)"
}