Create @babel/plugin-syntax-top-level-await (#10573)
This commit is contained in:
parent
3d2f365074
commit
4e6a4b08bb
3
packages/babel-plugin-syntax-top-level-await/.npmignore
Normal file
3
packages/babel-plugin-syntax-top-level-await/.npmignore
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
node_modules
|
||||||
|
*.log
|
||||||
|
src
|
||||||
19
packages/babel-plugin-syntax-top-level-await/README.md
Normal file
19
packages/babel-plugin-syntax-top-level-await/README.md
Normal file
@ -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
packages/babel-plugin-syntax-top-level-await/package.json
Normal file
23
packages/babel-plugin-syntax-top-level-await/package.json
Normal file
@ -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
packages/babel-plugin-syntax-top-level-await/src/index.js
Normal file
13
packages/babel-plugin-syntax-top-level-await/src/index.js
Normal file
@ -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");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
@ -27,6 +27,7 @@
|
|||||||
"@babel/plugin-syntax-json-strings": "^7.2.0",
|
"@babel/plugin-syntax-json-strings": "^7.2.0",
|
||||||
"@babel/plugin-syntax-object-rest-spread": "^7.2.0",
|
"@babel/plugin-syntax-object-rest-spread": "^7.2.0",
|
||||||
"@babel/plugin-syntax-optional-catch-binding": "^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-arrow-functions": "^7.2.0",
|
||||||
"@babel/plugin-transform-async-to-generator": "^7.5.0",
|
"@babel/plugin-transform-async-to-generator": "^7.5.0",
|
||||||
"@babel/plugin-transform-block-scoped-functions": "^7.2.0",
|
"@babel/plugin-transform-block-scoped-functions": "^7.2.0",
|
||||||
|
|||||||
@ -6,6 +6,7 @@ export default {
|
|||||||
"syntax-json-strings": require("@babel/plugin-syntax-json-strings"),
|
"syntax-json-strings": require("@babel/plugin-syntax-json-strings"),
|
||||||
"syntax-object-rest-spread": require("@babel/plugin-syntax-object-rest-spread"),
|
"syntax-object-rest-spread": require("@babel/plugin-syntax-object-rest-spread"),
|
||||||
"syntax-optional-catch-binding": require("@babel/plugin-syntax-optional-catch-binding"),
|
"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"),
|
"transform-async-to-generator": require("@babel/plugin-transform-async-to-generator"),
|
||||||
"proposal-async-generator-functions": require("@babel/plugin-proposal-async-generator-functions"),
|
"proposal-async-generator-functions": require("@babel/plugin-proposal-async-generator-functions"),
|
||||||
"proposal-dynamic-import": require("@babel/plugin-proposal-dynamic-import"),
|
"proposal-dynamic-import": require("@babel/plugin-proposal-dynamic-import"),
|
||||||
|
|||||||
@ -65,11 +65,13 @@ export const getModulesPluginNames = ({
|
|||||||
transformations,
|
transformations,
|
||||||
shouldTransformESM,
|
shouldTransformESM,
|
||||||
shouldTransformDynamicImport,
|
shouldTransformDynamicImport,
|
||||||
|
shouldParseTopLevelAwait,
|
||||||
}: {
|
}: {
|
||||||
modules: ModuleOption,
|
modules: ModuleOption,
|
||||||
transformations: ModuleTransformationsType,
|
transformations: ModuleTransformationsType,
|
||||||
shouldTransformESM: boolean,
|
shouldTransformESM: boolean,
|
||||||
shouldTransformDynamicImport: boolean,
|
shouldTransformDynamicImport: boolean,
|
||||||
|
shouldParseTopLevelAwait: boolean,
|
||||||
}) => {
|
}) => {
|
||||||
const modulesPluginNames = [];
|
const modulesPluginNames = [];
|
||||||
if (modules !== false && transformations[modules]) {
|
if (modules !== false && transformations[modules]) {
|
||||||
@ -95,6 +97,11 @@ export const getModulesPluginNames = ({
|
|||||||
} else {
|
} else {
|
||||||
modulesPluginNames.push("syntax-dynamic-import");
|
modulesPluginNames.push("syntax-dynamic-import");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldParseTopLevelAwait) {
|
||||||
|
modulesPluginNames.push("syntax-top-level-await");
|
||||||
|
}
|
||||||
|
|
||||||
return modulesPluginNames;
|
return modulesPluginNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -165,6 +172,10 @@ function supportsDynamicImport(caller) {
|
|||||||
return !!(caller && caller.supportsDynamicImport);
|
return !!(caller && caller.supportsDynamicImport);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function supportsTopLevelAwait(caller) {
|
||||||
|
return !!(caller && caller.supportsTopLevelAwait);
|
||||||
|
}
|
||||||
|
|
||||||
export default declare((api, opts) => {
|
export default declare((api, opts) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
@ -225,6 +236,7 @@ export default declare((api, opts) => {
|
|||||||
modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
|
modules !== "auto" || !api.caller || !api.caller(supportsStaticESM),
|
||||||
shouldTransformDynamicImport:
|
shouldTransformDynamicImport:
|
||||||
modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
|
modules !== "auto" || !api.caller || !api.caller(supportsDynamicImport),
|
||||||
|
shouldParseTopLevelAwait: api.caller(supportsTopLevelAwait),
|
||||||
});
|
});
|
||||||
|
|
||||||
const pluginNames = filterItems(
|
const pluginNames = filterItems(
|
||||||
|
|||||||
1
packages/babel-preset-env/test/fixtures/top-level-await/supported/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/top-level-await/supported/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
await 0;
|
||||||
8
packages/babel-preset-env/test/fixtures/top-level-await/supported/options.json
vendored
Normal file
8
packages/babel-preset-env/test/fixtures/top-level-await/supported/options.json
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"caller": {
|
||||||
|
"name": "test-fixture",
|
||||||
|
"supportsStaticESM": true,
|
||||||
|
"supportsTopLevelAwait": true
|
||||||
|
},
|
||||||
|
"presets": ["env"]
|
||||||
|
}
|
||||||
1
packages/babel-preset-env/test/fixtures/top-level-await/supported/output.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/top-level-await/supported/output.mjs
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
await 0;
|
||||||
1
packages/babel-preset-env/test/fixtures/top-level-await/unsupported/input.mjs
vendored
Normal file
1
packages/babel-preset-env/test/fixtures/top-level-await/unsupported/input.mjs
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
await 0;
|
||||||
9
packages/babel-preset-env/test/fixtures/top-level-await/unsupported/options.json
vendored
Normal file
9
packages/babel-preset-env/test/fixtures/top-level-await/unsupported/options.json
vendored
Normal file
@ -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)"
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user