Skip to main content

@babel/preset-flow

如果你使用 Flow(JavaScript 代码的静态类型检查器),建议使用此预设。它包括以下插件:

¥This preset is recommended if you use Flow, a static type checker for JavaScript code. It includes the following plugins:

示例

¥Example

输入

¥In

JavaScript
function foo(one: any, two: number, three?): string {}

输出

¥Out

JavaScript
function foo(one, two, three) {}

安装

¥Installation

npm install --save-dev @babel/preset-flow

用法

¥Usage

¥With a configuration file (Recommended)

babel.config.json
{
"presets": ["@babel/preset-flow"]
}

通过 CLI

¥Via CLI

Shell
babel --presets @babel/preset-flow script.js

通过 Node API

¥Via Node API

JavaScript
require("@babel/core").transformSync("code", {
presets: ["@babel/preset-flow"],
});

选项

¥Options

all

boolean,默认为 false

¥boolean, defaults to false.

如果文件顶部存在 @flow pragma,或者 all 选项 设置在 .flowconfig 中,Flow 只会解析 Flow 特定的功能。

¥Flow will only parse Flow-specific features if a @flow pragma is present atop the file, or the all option is set inside the .flowconfig.

如果你在 Flow 配置中使用 all 选项,请务必将此选项设置为 true 以获得匹配的行为。

¥If you are using the all option in your Flow config, be sure to set this option to true to get matching behavior.

例如,如果没有上述任何一种设置,则以下调用表达式带有类型参数:

¥For example, without either of the above set, the following call expression with a type argument:

f<T>(e)

将被解析为嵌套的二进制表达式:

¥Would get parsed as a nested binary expression:

JavaScript
f < T > e;

allowDeclareFields

boolean,默认为 false

¥boolean, defaults to false

添加于:v7.9.0

¥Added in: v7.9.0

注意

这将在 Babel 8 中默认启用

¥This will be enabled by default in Babel 8

启用后,仅类型的类字段仅在以 declare 修饰符为前缀时才会被删除:

¥When enabled, type-only class fields are only removed if they are prefixed with the declare modifier:

JavaScript
class A {
declare foo: string; // Removed
bar: string; // Initialized to undefined
}

ignoreExtensions

boolean,默认为 true

¥boolean, defaults to true

添加于:v7.24.0

¥Added in: v7.24.0

当它设置为 true 时,Babel 会将流变换应用于所有扩展。当设置为 false 时,Babel 将避免对 *.tsx 文件进行流转换。

¥When it is set to true, Babel will apply the flow transform to all extensions. When it is set to false, Babel will avoid the flow transform for *.tsx files.

experimental_useHermesParser

boolean,默认为 false

¥boolean, defaults to false

添加于:v7.24.0

¥Added in: v7.24.0

Hermes 团队正在维护 Babel 的替代 Flow 解析器,它可以更好地与最新的 Flow 语法功能保持同步。你可以通过将此选项设置为 true 来启用它。

¥The Hermes team is maintaining an alternative Flow parser for Babel, which is better kept up-to-date with the latest Flow syntax features. You can enable it by setting this option to true.

警告

Hermes 解析器当前不向 AST 附加注释。这可能会导致依赖于特定注释的存在的转换问题。

¥The Hermes parser does not currently attach comments to the AST. This can cause problems with transforms that depend on the presence of specific comments.

你可以阅读有关配置预设选项 此处 的更多信息

¥You can read more about configuring preset options here