升级到 Babel 8 (API)
本文档列出了 Babel 8.0.0 中引入的重大更改,以及从 Babel 7.0.0 升级时如何调整 Babel 的使用以适应这些更改。本文档面向直接使用 Babel API 的开发者,例如 Babel 插件作者或使用 Babel 作为库的项目作者。
¥This document lists the breaking changes introduced in Babel 8.0.0, and how to adapt your usage of Babel to them when upgrading from Babel 7.0.0. This document is intended for developers using Babel's API directly, such as Babel plugin authors, or authors of projects that use Babel as a library.
请先查看 Babel 8 迁移指南,了解面向用户的重大变更。
¥Check out the Babel 8 migration guide first to learn about user-facing breaking changes.
AST 变更
¥AST Changes
JavaScript 节点
¥JavaScript nodes
-
将动态
import()表示为ImportExpression节点(#15682、#16114)。¥Represent dynamic
import()with anImportExpressionnode (#15682, #16114).// Example input
import("foo", options);
// AST in Babel 7
{
type: "CallExpression",
callee: { type: "Import" },
arguments: [
StringLiteral("foo"),
Identifier("options")
]
}
// AST in Babel 8
{
type: "ImportExpression",
source: StringLiteral("foo"),
options: Identifier("options")
}迁移:我们鼓励你使用新的 AST(从 v7.23.0 开始)测试你的 Babel 插件,并在 Babel 配置中指定
{ parserOpts: { createImportExpressions: true } }。对于使用依赖于旧版import()AST 的 Babel 插件的终端用户,可以将createImportExpressions设置为false。请注意,Babel 7import()AST 现已弃用,它不支持新的 ES 功能,例如 源短语导入。我们将在 Babel 9 中移除createImportExpressions解析器选项。¥Migration: You are encouraged to test your Babel plugins with the new AST, starting from v7.23.0, specifying
{ parserOpts: { createImportExpressions: true } }in the Babel config. For end users utilizing Babel plugins that rely on the legacyimport()AST, it is possible to setcreateImportExpressionstofalse. Note that the Babel 7import()AST is now considered deprecated, it does not support new ES features such as Source Phrase Imports. We will remove thecreateImportExpressionsparser option in Babel 9.
-
使用
bigint代替BigIntLiteral.value,而不是字符串(#17378)¥ Use
bigintforBigIntLiteral.value, rather than a string(#17378)// Example input
0xff01_0000_0000_0000_0000_0000_0000_0001n
// AST in Babel 7
{
type: "BigIntLiteral",
value: "0xff010000000000000000000000000001",
extra: {
rawValue: "0xff010000000000000000000000000001",
raw: "0xff01_0000_0000_0000_0000_0000_0000_0001n",
}
}
// AST in Babel 8
{
type: "BigIntLiteral",
value: 338958331222012082418099330867817086977n,
extra: {
rawValue: 338958331222012082418099330867817086977n,
raw: "0xff01_0000_0000_0000_0000_0000_0000_0001n"
}
}迁移:此项变更与我们处理
NumericLiteral的方式一致。如果你想在 Babel 7 中访问字符串表示,请调用toString()的 bigint 实例方法。请注意,内置 JavaScript 方法JSON.stringify无法序列化bigint。如果你想将 Babel 8 AST 存储为 JSON,可以使用 提供你自己的bigint序列化器。¥Migration: This change aligns with how we handle
NumericLiteral. If you want to access string representation in Babel 7, call thetoString()bigint instance method. Note that the builtin JavaScript methodJSON.stringifycan not serializebigint. If you want to store the Babel 8 AST as JSON, you can provide your ownbigintserializer.
TypeScript 节点
¥TypeScript nodes
我们对 TypeScript 特定的 AST 节点所做的大部分更改都是为了减少与 @typescript-eslint 项目 AST 形状的差异。这将使编写 ESLint 规则变得更容易,当不依赖类型信息时,这些规则可以同时与 @typescript-eslint/parser 和 @babel/eslint-parser 兼容。
¥Most of the changes to our TypeScript-specific AST nodes are to reduce the differences with the AST shape of the @typescript-eslint project. This will make it easier to write ESLint rules that, when not depending on type information, can work both with @typescript-eslint/parser and @babel/eslint-parser.
-
使用标识符代替
TSTypeParameter.name,而不是纯字符串(#12829)¥ Use an identifier for
TSTypeParameter.name, rather than a plain string (#12829)input.ts// T is a TSTypeParameter
function process<T>(input: T): T {}
// AST in Babel 7
{
type: "TSTypeParameter",
name: "T",
}
// AST in Babel 8
{
type: "TSTypeParameter",
name: { type: "Identifier", name: "T" },
} -
允许将
ThisExpression用作TSTypeQuery.exprName,而不是this标识符 (#17059)¥Allow
ThisExpressionasTSTypeQuery.exprName, rather than athisidentifier (#17059)input.tsfunction fn() {
// `typeof this` is a TSTypeQuery
var self: typeof this
}
// AST in Babel 7
{
type: "TSTypeQuery",
exprName: { type: "Identifier", name: "this" }
}
// AST in Babel 8
{
type: "TSTypeQuery",
exprName: { type: "ThisExpression" }
} -
将
TSImportEqualsDeclaration导出为ExportNamedDeclaration进行解析(#17073)¥Parse exporting
TSImportEqualsDeclarationas anExportNamedDeclaration(#17073)请注意,
isExport字段也已被移除。对于任何TSImportEqualsDeclarationNodePathp,使用p.parentPath.isExportNamedDeclaration()检测它是否位于export关键字之后。¥Note that the
isExportfield is also removed. For anyTSImportEqualsDeclarationNodePathp, usep.parentPath.isExportNamedDeclaration()to detect whether it is following anexportkeyword.input.tsexport import foo = require("foo")
// AST in Babel 7
{
type: "TSImportEqualsDeclaration",
importKind: "value"
isExport: true,
id: Identifier("foo"),
moduleReference: {
type: "TSExternalModuleReference",
expression: StringLiteral("foo")
}
}
// AST in Babel 8
{
type: "ExportNamedDeclaration",
declaration: {
type: "TSImportEqualsDeclaration",
importKind: "value"
id: Identifier("foo"),
moduleReference: {
type: "TSExternalModuleReference",
expression: StringLiteral("foo")
}
},
specifiers: []
} -
将
TSCallSignatureDeclaration、TSConstructSignatureDeclaration、TSFunctionType、TSConstructorType和TSMethodSignature(#9231、#13709)中的parameters重命名为params,将typeAnnotation重命名为returnType¥Rename
parameterstoparamsandtypeAnnotationtoreturnTypeinTSCallSignatureDeclaration,TSConstructSignatureDeclaration,TSFunctionType,TSConstructorTypeandTSMethodSignature(#9231, #13709)TSCallSignatureDeclaration
input.tsinterface Foo {
(x: number): string;
}
// AST in Babel 7
{
type: "TSCallSignatureDeclaration",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSCallSignatureDeclaration",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSConstructSignatureDeclaration
input.tsinterface Foo {
new (x: number): string;
}
// AST in Babel 7
{
type: "TSConstructSignatureDeclaration",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSConstructSignatureDeclaration",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSMethodSignature
input.tsinterface Foo {
foo(x: number): string;
}
// AST in Babel 7
{
type: "TSMethodSignature",
key: Identifier("foo"),
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSMethodSignature",
key: Identifier("foo"),
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSFunctionType
input.tstype Bar = (x: number) => string;
// AST in Babel 7
{
type: "TSFunctionType",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSFunctionType",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}TSConstructorType
input.tstype Bar = (x: number) => string;
// AST in Babel 7
{
type: "TSConstructorType",
parameters: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
typeAnnotation: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
}
// AST in Babel 8
{
type: "TSConstructorType",
params: [
{ type: "Identifier", name: "x", typeAnnotation: { type: "TSNumberKeyword" } }
],
retutnType: {
type: "TSTypeAnnotation",
typeAnnotation: { type: "TSStringKeyword" }
}
} -
将
CallExpression、JSXOpeningElement、NewExpression、OptionalCallExpression、TSImportType、TSInstantiationExpression、TSTypeQuery和TSTypeReference(#16679、#17008、#17012、#17020、#17042)中的typeParameters重命名为typeArguments¥ Rename
typeParameterstotypeArgumentsinCallExpression,JSXOpeningElement,NewExpression,OptionalCallExpression,TSImportType,TSInstantiationExpression,TSTypeQueryandTSTypeReference(#16679, #17008, #17012, #17020, #17042)CallExpression
input.tsfn<string>()
// AST in Babel 7
{
type: "CallExpression",
callee: Identifier("fn"),
arguments: [],
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "CallExpression",
callee: Identifier("fn"),
arguments: [],
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}JSXOpeningElement
input.ts<Component<string>/>
// AST in Babel 7
{
type: "JSXOpeningElement",
name: JSXIdentifier("Component"),
attributes: [],
selfClosing: true,
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "JSXOpeningElement",
name: JSXIdentifier("Component"),
attributes: [],
selfClosing: true,
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}NewExpression
input.tsnew Component<string>()
// AST in Babel 7
{
type: "NewExpression",
callee: Identifier("Component"),
arguments: [],
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "NewExpression",
callee: Identifier("Component"),
arguments: [],
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}OptionalCallExpression
input.tsfn?.<string>()
// AST in Babel 7
{
type: "OptionalCallExpression",
callee: Identifier("fn"),
arguments: [],
optional: true,
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "OptionalCallExpression",
callee: Identifier("fn"),
arguments: [],
optional: true,
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSImportType
input.tsvar arr: import("./Array")<string>
// AST in Babel 7
{
type: "TSImportType",
argument: StringLiteral("./Array"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSImportType",
argument: {
type: "TSLiteralType",
literal: StringLiteral("./Array")
},
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSInstantiationExpression
input.tsfn<string>
// AST in Babel 7
{
type: "TSInstantiationExpression",
expression: Identifier("fn"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSInstantiationExpression",
expression: Identifier("fn"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSTypeQuery
input.tsvar arr: typeof Array<string>;
// AST in Babel 7
{
type: "TSTypeQuery",
exprName: Identifier("Array"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSTypeQuery",
exprName: Identifier("Array"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}TSTypeReference
input.tsvar arr: Array<string>;
// AST in Babel 7
{
type: "TSTypeReference",
typeName: Identifier("Array"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "TSTypeReference",
typeName: Identifier("Array"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
} -
将
ClassDeclaration和ClassExpression(#16679、#16997)中的superTypeParameters重命名为superTypeArguments¥Rename
superTypeParameterstosuperTypeArgumentsinClassDeclarationandClassExpression(#16679, #16997)input.tsclass X extends Y<string> {}
// AST in Babel 7
{
type: "ClassDeclaration",
id: Identifier("X"),
superClass: Identifier("Y"),
superTypeParameters: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
}
// AST in Babel 8
{
type: "ClassDeclaration",
id: Identifier("X"),
superClass: Identifier("Y"),
superTypeArguments: {
type: "TSTypeParameterInstantiation",
params: [{
type: "TSStringKeyword"
}]
}
} -
将
typeParameter从TSMappedType(#16733)中拆分出来。¥ Split
typeParameterofTSMappedType(#16733).在
TSMappedType节点中,typeParameter属性被展平为TSMappedType本身的key和constraint属性。¥In
TSMappedTypenodes, thetypeParameterproperty is flattened askeyandconstraintproperties ofTSMappedTypeitself.input.tslet map1: { [P in string]: number; };
// AST in Babel 7
{
type: "TSMappedType",
typeParameter: {
type: "TypeParameter",
name: Identifier("P"),
constraint: TSStringKeyword()
},
typeAnnotation: TSNumberKeyword(),
}
// AST in Babel 8
{
type: "TSMappedType",
key: Identifier("P"),
constraint: TSStringKeyword()
typeAnnotation: TSNumberKeyword(),
} -
将
TSExpressionWithTypeArguments拆分为TSClassImplements和TSInterfaceHeritage(#16731)。¥Split
TSExpressionWithTypeArgumentsintoTSClassImplementsandTSInterfaceHeritage(#16731).新节点也使用
typeArguments而不是typeParameters(#17017)。如果expression是 TS 限定名称(例如a.b),它将被解析为MemberExpression(#17139)。¥The new nodes also use
typeArgumentsinstead oftypeParameters(#17017). If theexpressionis a TS qualified name (e.g.a.b), it will be parsed as aMemberExpression(#17139).TSClassImplements
input.tsclass C implements X<T> {}
// AST in Babel 7
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSExpressionWithTypeArguments",
expression: Identifier("X"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: ClassBody([]),
}
// AST in Babel 8
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSClassImplements",
expression: Identifier("X"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: ClassBody([]),
}TSInterfaceHeritage
input.tsinterface I extends X<T> {}
// AST in Babel 7
{
type: "TSInterfaceDeclaration",
id: Identifier("I"),
extends: [
{
type: "TSExpressionWithTypeArguments",
expression: Identifier("X"),
typeParameters: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: TSInterfaceBody([]),
}
// AST in Babel 8
{
type: "TSInterfaceDeclaration",
id: Identifier("I"),
extends: [
{
type: "TSInterfaceHeritage",
expression: Identifier("X"),
typeArguments: {
type: "TSTypeParameterInstantiation",
params: [TSTypeReference(Identifier("T"))]
}
}
],
body: TSInterfaceBody([]),
}Qualified name
input.tsclass C implements X.Y {}
// AST in Babel 7
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSExpressionWithTypeArguments",
expression: {
type: "TSQualifiedName",
left: Identifier("X"),
right: Identifier("Y")
}
}
],
body: ClassBody([]),
}
// AST in Babel 8
{
type: "ClassDeclaration",
id: Identifier("C"),
implements: [
{
type: "TSClassImplements",
expression: {
type: "MemberExpression",
object: Identifier("X"),
property: Identifier("Y"),
computed: false
}
}
],
body: ClassBody([]),
} -
将
TSImportType的argument封装在TSLiteralType节点中(#17046)¥ Wrap the
argumentofTSImportTypewithin aTSLiteralType(#17046)TSImportType还使用typeArguments而不是typeParameters(#17042)。有关示例,请参阅 此处。¥The
TSImportTypealso usestypeArgumentsinstead oftypeParameters(#17042). See here for an example.input.tsvar arr: import("./Array")
// AST in Babel 7
{
type: "TSImportType",
argument: StringLiteral("./Array")
}
// AST in Babel 8
{
type: "TSImportType",
argument: {
type: "TSLiteralType",
literal: StringLiteral("./Array")
}
} -
将
TSEnumDeclaration的members封装在TSEnumBody节点中(#16979)¥ Wrap the
membersofTSEnumDeclarationwithin aTSEnumBodynode (#16979)input.ts// Example input
enum ColorType {
Red,
Green,
Blue,
}
// AST in Babel 7
{
type: "TSEnumDeclaration",
id: Identifier("ColorType")
members: [
EnumMember("Red"),
EnumMember("Green"),
EnumMember("Blue")
]
}
// AST in Babel 8
{
type: "TSEnumDeclaration",
id: Identifier("ColorType")
body: {
type: "TSEnumBody",
members: [
EnumMember("Red"),
EnumMember("Green"),
EnumMember("Blue")
]
}
} -
当至少有一个插值位置时,创建
TSTemplateLiteralType。¥Create
TSTemplateLiteralTypewhen there is at least one interpolated position请注意,当没有插值位置时,AST 不会更改,例如,作为模板字面量类型的
foo仍然会被解析为 TSLiteralType 中的 TemplateLiteral 节点。¥Note that the AST is not changed when there is no interpolated position, e.g.
`foo`as a template literal type is still parsed as a TemplateLiteral node within a TSLiteralType.input.tstype World = "world";
// `hello ${World}` is a template literal type
type Greeting = `hello ${World}`;
// AST in Babel 7
{
type: "TSLiteralType",
literal: {
type: "TemplateLiteral",
expressions: [{
type: "TSTypeReference",
typeName: Identifier("World")
}],
quasis: [
TemplateElement("hello "),
TemplateElement("")
]
}
}
// AST in Babel 8
{
type: "TSTemplateLiteralType",
types: [{
type: "TSTypeReference",
typeName: Identifier("World")
}],
quasis: [
TemplateElement("hello "),
TemplateElement("")
]
} -
当同时启用
estree和typescript解析器插件时,创建TSAbstractMethodDefinition和TSPropertyDefinition(#16679、#17014)。¥Create
TSAbstractMethodDefinitionandTSPropertyDefinitionwhen bothestreeandtypescriptparser plugins are enabled (#16679, #17014)迁移:此项重大变更是为了使库和 ESLint 插件能够同时与
typescript-eslint和@babel/eslint-parser兼容。对于大多数 Babel 插件开发者,你可以放心地忽略此更改,因为它不会影响 TypeScript 转换和代码修改。也就是说,如果你尝试使用@babel/eslint-parser开发自定义 ESLint 规则,此更改将使 Babel AST 与typescript-eslintAST 保持一致。¥Migration: This breaking change is part of the efforts to libraries and ESLint plugins that can work both with
typescript-eslintand@babel/eslint-parser. For most Babel plugin developers you can safely ignore this change as it does not affect the typescript transform and codemod. That said, if you are trying to develop a custom ESLint rule with@babel/eslint-parser, this change aligns the Babel AST to thetypescript-eslintAST. -
使用
TSQualifiedName作为namespace X.Y {}的名称 (#16982)¥Use
TSQualifiedNamefornamespace X.Y {}'s name (#16982)namespace X.Y {}不再表示为两个嵌套的TSModuleDeclaration节点,名称分别为X和Y,而是表示为单个TSModuleDeclaration,名称为TSQualifiedName(X, Y)。此项变更使 AST 与@typescript-eslint解析器保持一致。¥Rather than representing
namespace X.Y {}as two nestedTSModuleDeclarationnodes with namesXandY, it is now represented as a singleTSModuleDeclarationwhose name is aTSQualifiedName(X, Y). This change aligns the AST with the@typescript-eslintparser.// Example input
namespace X.Y {}
// AST in Babel 7
{
type: "TSModuleDeclaration",
id: Identifier("X"),
body: {
type: "TSModuleDeclaration",
id: Identifier("Y")
body: TSModuleBlock([]),
},
}
// AST in Babel 8
{
type: "TSModuleDeclaration",
id: {
type: "TSQualifiedName",
left: Identifier("X"),
right: Identifier("Y"),
},
body: TSModuleBlock([]),
}
-
除非启用
createParenthesizedExpression,否则不要生成TSParenthesizedType(#9546、#12608)。¥Don't generate
TSParenthesizedTypeunlesscreateParenthesizedExpressionis enabled (#9546, #12608)input.tstype T = ({});
// Babel 8 with createParenthesizedExpression: true, and Babel 7
TSParenthesizedType { typeAnnotation: TSTypeLiteral { members: [] } }
// Babel 8 with createParenthesizedExpression: false
TSTypeLiteral { members: [] }迁移:如果你需要有关括号的信息,请指定
createParenthesizedExpression解析器选项。¥Migration: If you need information about parentheses, specify the
createParenthesizedExpressionparser option.babel.config.json{ "parserOpts": { "createParenthesizedExpression": true } }当
createParenthesizedExpression为false时,你还可以使用node.extra.parenthesized来检测node是否包含在括号中。¥When
createParenthesizedExpressionisfalse, you can also usenode.extra.parenthesizedto detect whethernodeis wrapped in parentheses.
API 变更
¥API Changes
所有软件包
¥All packages
-
¥Disallow importing internal files (#14013, #14179).
迁移:仅使用导出的 API。如果你依赖 Babel 内部组件,请提交问题并告知我们。
¥Migration: Use the exported API only. If you are relying on Babel internals, please open an issue and let us know.
@babel/core
-
禁止同步使用
babel.transform、babel.transformFile、babel.transformFromAst、babel.parse、babel.loadOptions、babel.loadPartialConfig和babel.createConfigItem(#11110、#12695、#15869)。¥Disallow using
babel.transform,babel.transformFile,babel.transformFromAst,babel.parse,babel.loadOptions,babel.loadPartialConfigandbabel.createConfigItemsynchronously (#11110, #12695, #15869).迁移:上述 API 需要回调参数。如果你不提供回调,请使用其同步版本:
babel.transformSync、babel.transformFileSync、babel.transformFromAstSync、babel.parseSync、babel.loadOptionsSync、babel.loadPartialConfigSync和babel.createConfigItemSync。¥Migration: The APIs above require a callback argument. If you are not providing a callback, please use their sync versions:
babel.transformSync,babel.transformFileSync,babel.transformFromAstSync,babel.parseSync,babel.loadOptionsSync,babel.loadPartialConfigSyncandbabel.createConfigItemSync.
@babel/generator
-
移除
CodeGenerator类 (#16126)¥Remove
CodeGeneratorclass (#16126)迁移:在 Babel 8 中,未记录的
CodeGenerator类已被移除,请使用默认导出的generate函数。¥Migration: In Babel 8 the undocumented
CodeGeneratorclass has been removed, please use the default exportedgeneratefunction instead.- new CodeGenerator(ast).generate()
+ generate(ast)
@babel/types
-
拒绝
t.identifier构建器 (#10917) 中的无效标识符名称。¥Reject invalid identifier names in
t.identifierbuilder (#10917).babel-plugin.js// Empty string is an invalid identifier name
t.identifier("");迁移:使用有效名称调用
t.identifier。¥Migration: Call
t.identifierwith a valid name. -
拒绝
t.variableDeclaration构建器中的无效变量声明符(#10917、#17217)¥Reject invalid variable declarator in
t.variableDeclarationbuilder (#10917, #17217)babel-plugin.js// var [x] is invalid without an initializer
t.variableDeclaration("var", [
t.variableDeclarator(t.arrayPattern([t.identifier("x")]))
]); -
从
Expression别名中移除Super(#14750)。¥Remove
Superfrom theExpressionalias (#14750).Super节点在父类调用super()和父类属性super.foo中代表super。由于super不能是独立表达式,因此在 Babel 8 中,t.isExpression(t.super())将返回false。¥A
Supernode representssuperin super callsuper()and super propertysuper.foo. Assupercan not be a standalone expression,t.isExpression(t.super())will returnfalsein Babel 8.迁移:
t.isExpression和t.assertsExpression函数以及t.Expression类型别名的搜索用法:如果他们还需要接受Super节点,请相应地更新它们。¥Migration: Search usage of the
t.isExpressionandt.assertsExpressionfunctions, and of thet.Expressiontype alias: if they need to also acceptSupernodes, update them accordingly.my-babel-plugin.js// Add `.foo` to an expression
- if (t.isExpression(path.node)) {
+ if (t.isExpression(path.node) || t.isSuper(path.node)) {
path.replaceWith(
t.memberExpression(
path.node,
t.identifier("foo")
))
}如果不涉及
super()和super.foo,则无需更新用法:¥You don't have to update the usage if
super()andsuper.foois not involved:my-babel-plugin.js// define an expression as a computed key of `foo`
if (t.isExpression(path.node)) {
path.replaceWith(
t.memberExpression(
t.identifier("foo"),
// `super` can not be a computed key, so we don't update `isExpression`
path.node,
/* computed */ true
))
} -
从
LVal别名中移除AssignmentPattern和RestElement(#17387)¥Remove
AssignmentPatternandRestElementfrom theLValalias (#17387)LVal节点表示赋值表达式左侧 (LHS) 的有效节点。由于AssignmentPattern和RestElement不能是 LHS,因此在 Babel 8 中,t.isLVal(t.assignmentPattern(...))和t.isLVal(t.restElement(...))将返回false。¥An
LValnode represents valid nodes as the left hand side (LHS) of an assignment expression. AsAssignmentPatternandRestElementcan not be the LHS,t.isLVal(t.assignmentPattern(...))andt.isLVal(t.restElement(...))will returnfalsein Babel 8.迁移:
t.isLVal和t.assertsLVal函数以及t.LVal类型别名的搜索用法:如果他们还需要接受AssignmentPattern或RestElement节点,你可以使用t.PatternLike扩展t.LVal:¥Migration: Search usage of the
t.isLValandt.assertsLValfunctions, and of thet.LValtype alias: if they need to also acceptAssignmentPatternorRestElementnodes, you can broadent.LValwitht.PatternLike:my-babel-plugin.js// Iterate rest element within an object pattern
for (const propertyPath of path.get("properties")) {
- if (t.isLVal(propertyPath.node)) {
+ if (t.isLVal(propertyPath.node) || t.isPatternLike(propertyPath.node)) {
doSomethingWith(propertyPath);
}
} -
从
LVal别名中移除TSParameterProperty(#17391)¥Remove
TSParameterPropertyfrom theLValalias (#17391)LVal节点表示赋值表达式左侧 (LHS) 的有效节点。TSParameterProperty是一种特殊的函数参数类型,仅允许在类构造函数中使用。由于TSParameterProperty不能作为 LHS,因此在 Babel 8 中t.isLVal(t.tsParameterProperty(...))将返回false。¥An
LValnode represents valid nodes as the left hand side (LHS) of an assignment expression. ATSParameterPropertyis a special function parameter type allowed only in a class constructor. SinceTSParameterPropertycan not be the LHS,t.isLVal(t.tsParameterProperty(...))will returnfalsein Babel 8. -
要求
bigint作为t.bigIntLiteral(#17378)的唯一参数¥Require a
bigintas the only argument oft.bigIntLiteral(#17378)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
迁移:将
bigint原语传递给bigIntLiteral构建器。bigIntLiteral构建器从 Babel 7.28 开始支持bigint,你可以在升级到 Babel 8 之前开始迁移。¥Migration: Pass the
bigintprimitive to thebigIntLiteralbuilder. Support ofbigintin thebigIntLiteralbuilder starts from Babel 7.28, you can begin the migration before you upgrade to Babel 8my-babel-codemod.jst.bigIntLiteral( -
"0xff020000000000000000000000000002"
- 0xff02000000000000000000000000000002n )
- Require an `Identifier` node as the third argument of `t.tsTypeParameter` ([#12829](https://github.com/babel/babel/pull/12829))
This is due to the corresponding [AST shape change](#ast-TSTypeParameter).
__Migration__: Wrap the `name` string within the `identifier` builder
```diff title="my-babel-codemod.js"
t.tsTypeParameter(
/* constraint */ undefined,
/* default */ undefined,
+ t.identifier(
name
+ )
)
-
要求
TSLiteralType节点作为t.tsImportType(#17046)的第一个参数¥Require a
TSLiteralTypenode as the first argument oft.tsImportType(#17046)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
迁移:将
argument字符串字面量封装在tsLiteralType构建器中¥Migration: Wrap the
argumentstring literal within thetsLiteralTypebuildermy-babel-codemod.jst.tsImportType(
+ t.tsLiteralType(
t.stringLiteral("foo")
+ )
) -
要求
TSEnumBody节点作为t.tsEnumDeclaration(#16979)的第二个参数¥Require a
TSEnumBodynode as the second argument oft.tsEnumDeclaration(#16979)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
迁移:将
members数组封装在tsEnumBody构建器中¥Migration: Wrap the
membersarray within thetsEnumBodybuildermy-babel-codemod.js// Create `enum ColorType { Red, Green, Blue }`
t.tsEnumDeclaration(
t.identifier("ColorType"),
- [
+ t.tsEnumBody([
t.tsEnumMember(t.identifier("Red")),
t.tsEnumMember(t.identifier("Green")),
t.tsEnumMember(t.identifier("Blue"))
- ],
+ ]),
) -
更新
t.tsMappedType签名 (#16733)¥Update the
t.tsMappedTypesignature (#16733)这是由于相应的 AST 形状变更 造成的。
¥This is due to the corresponding AST shape change.
// Babel 7
declare function tsMappedType(
typeParameter: TSTypeParameter,
typeAnnotation?: TSType,
nameType?: TSType
): TSMappedType
// Babel 8
declare function tsMappedType(
key: Identifier,
constraint: TSType,
nameType?: TSType,
typeAnnotation?: TSType
): TSMappedType迁移:参见以下示例。
¥Migration: See the example below.
my-babel-codemod.ts// To create { [P in string as Q]: number }
// Babel 7
t.tsMappedType(
t.tsTypeParameter(t.tsStringKeyword(), undefined, "P"),
t.tsNumberKeyword(),
t.tsTypeReference(t.identifier("Q"))
)
// Babel 8
t.tsMappedType(
t.identifier("P"),
t.tsStringKeyword(),
t.tsTypeReference(t.identifier("Q")),
t.tsNumberKeyword()
) -
TSModuleDeclaration的第二个参数 (body) 不能再是TSModuleDeclaration(#16982)¥The second argument (
body) ofTSModuleDeclarationcannot be aTSModuleDeclarationanymore (#16982) -
t.tsModuleDeclaration的第二个参数 (body) 不能再是TSModuleDeclaration节点 (#16982)¥The second argument (
body) oft.tsModuleDeclarationcannot be aTSModuleDeclarationnode anymore (#16982)迁移:创建一个导出
TSModuleDeclaration的TSModuleBlock主体,或者创建一个以TSQualifiedName为名称的TSModuleDeclaration。¥Migration: Either create a
TSModuleBlockbody that exports theTSModuleDeclaration, or create a singleTSModuleDeclarationthat has aTSQualifiedNameas its name.my-babel-codemod.js// Option 1
// Create `namespace X.Y {}`
t.tsModuleDeclaration(
t.identifier("X"),
+ t.exportNamedDeclaration(
t.tsModuleDeclaration(
t.identifier("Y"),
t.tsModuleBlock([])
),
+ ),
)
```diff title="my-babel-codemod.js"
// Option 2
// Create `namespace X.Y {}`
t.tsModuleDeclaration(
+ t.tsQualifiedName(
t.identifier("X"),
- t.tsModuleDeclaration(
t.identifier("Y"),
+ ),
t.tsModuleBlock([])
- )
)
-
移除
t.jSX*和t.tS*构建器别名(#6989、#15527)¥Remove
t.jSX*andt.tS*builder aliases (#6989, #15527)迁移:改用
t.jsx*和t.ts*。例如,将t.jSXIdentifier("foo")替换为t.jsxIdentifier("foo")。¥Migration: Use
t.jsx*andt.ts*instead. For example, replacet.jSXIdentifier("foo")witht.jsxIdentifier("foo"). -
从
t.jsxElement中移除selfClosing参数(#14464)¥Remove
selfClosingargument fromt.jsxElement(#14464)- t.jsxElement(openingElement, closingElement, children, selfClosing?: boolean)
+ t.jsxElement(openingElement, closingElement, children)迁移:构建器中已不再使用
selfClosing参数。你可以安全地将其移除。¥Migration: The
selfClosingargument was already not used in the builder. You can safely remove it. -
从
t.memberExpression中移除optional参数(#13407)¥Remove
optionalargument fromt.memberExpression(#13407)- t.memberExpression(object, property, computed, optional?: boolean)
+ t.memberExpression(object, property, computed)迁移:构建器中已不再使用
optional参数。你可以安全地将其移除。¥Migration: The
optionalargument was already not used in the builder. You can safely remove it. -
删除
Noop节点类型 (#12361)¥Remove the
Noopnode type (#12361)迁移:没有通用的迁移路径:你应该将其替换为
Noop用作占位符的实际节点。如果你依赖Noop并且没有其他选择,请提交问题并解释你的用例。¥Migration: There is no generic migration path: you should replace it with the actual node that
Noopis being used as a placeholder for. If you are depending onNoopand have no alternative, please open an issue and explain your use case. -
在
t.objectTypeAnnotation(#14465)中,将节点ObjectTypeAnnotation中的indexers、callProperties和internalSlots初始化为空数组。¥Initialize
indexers,callPropertiesandinternalSlotsin the nodeObjectTypeAnnotationas an empty array int.objectTypeAnnotation(#14465)在 Babel 7 中,构建器
t.objectTypeAnnotation将它们初始化为null,这与@babel/parser解析 Flow 对象类型注释的方式不一致。在 Babel 8 中,新的默认值[]与解析器行为匹配。¥In Babel 7 the builder
t.objectTypeAnnotationinitializes them asnull, this is inconsistent with how@babel/parserwill parse the Flow object type annotations. In Babel 8 the new default value[]matches the parser behaviour. -
拒绝
t.numericLiteral中的负数和 NaN/无穷大数(#15802)¥Reject negative and NaN/infinite numbers from
t.numericLiteral(#15802)babel-plugin.js// NumericLiterals must be non-negative finite numbers.
t.numericLiteral(-1);迁移:要表示负数,请使用
t.unaryExpression("-", t.numericLiteral(1))。要表示 NaN 或无穷大,请使用t.identifier("NaN")。要将通用数值(也可以是负数、NaN 或无穷大)转换为正确的 AST 节点,请使用t.valueToNode(num)。¥Migration: To represent a negative number, use
t.unaryExpression("-", t.numericLiteral(1)). To represent NaN or Infinity, uset.identifier("NaN"). To convert a generic numeric value (which could also be negative, NaN, or an inifinity) to the proper AST node, uset.valueToNode(num).
@babel/parser
除以下列出的更改外,@babel/parser 受所有 AST 变更 的影响。
¥Other than the changes listed below, @babel/parser is affected by all the AST changes.
-
在 Flow 和 TypeScript 之间对齐 Babel 解析器错误代码 (#13294)
¥Align Babel parser error codes between Flow and TypeScript (#13294)
OptionalBindingPattern的error.code已重命名为PatternIsOptional。¥The
error.codeforOptionalBindingPatternis renamed asPatternIsOptional. -
从选项
tokens: true返回的tokens[].type中移除updateContext字段(#13768)¥Remove
updateContextfield fromtokens[].typereturned from optiontokens: true(#13768)babel-integration.jsimport { parse } from "@babel/parser";
const { tokens } = parse("a = 42", { tokens: true });
tokens[0].type;
// Babel 7
// { label: "name", updateContext: null, ...other properties }
// Babel 8
// { label: "name", ... other properties }请注意,
tokens[].type是一个不透明对象,用作 token 类型的标识,就像一个符号一样。其确切结构应作为内部实现细节。¥Note that
tokens[].typeis meant to be an opaque object used as an identity for token types, as if it was a symol. Its exact shape is meant to be an internal implementation detail. -
将私有名称 (
#priv) 标记为单个privateName标记 (#13256)¥Tokenize private names (
#priv) as a singleprivateNametoken (#13256)babel-integration.jsimport { parse } from "@babel/parser";
const { tokens } = parse("class C { #priv }", { tokens: true });
tokens.filter(t => t.start >= 10 && t.end <= 15) // get tokens for `#priv`
// Babel 7
// [
// Token (#) { value: "#", start: 10, end: 11 },
// Token (name) { value: "priv", start: 11, end: 15 }
// ]
// Babel 8
// [
// Token (privateName) { value: "priv", start: 10, end: 15 }
// ] -
将模板字面量标记为
templateNonTail和templateTail(#13919)¥Tokenize template literals as
templateNonTailandtemplateTail(#13919)babel-integration.jsimport { parse } from "@babel/parser";
const { tokens } = parse("`head${x}middle${y}tail`", { tokens: true });
console.log(tokens); // print tokens
// Babel 7
// [
// Token (`),
// Token (template) { value: "head" }, Token (${),
// Token (name) { value: "x" }, Token (}),
// Token (template) { value: "middle" }, Token (${),
// Token (name) { value: "y" }, Token (}),
// Token (template) { value: "tail" }
// Token (`)
// ]
// Babel 8
// [
// Token (templateNonTail) { value: "head" },
// Token (name) { value: "x" },
// Token (templateNonTail) { value: "middle" },
// Token (name) { value: "y" },
// Token (templateTail) { value: "tail" }
// ] -
从
ObjectProperty节点中移除extra.shorthand(#16521)¥Remove
extra.shorthandfromObjectPropertynodes (#16521)迁移:使用
node.shorthand而不是node.extra.shorthand。¥Migration: Use
node.shorthandrather thannode.extra.shorthand.
@babel/traverse
-
从
NodePath(#16655) 中移除is、isnt、has、equals方法¥Remove
is,isnt,has,equalsmethods fromNodePath(#16655)迁移:改为直接比较
path.node的属性。¥Migration: Directly compare properties of
path.nodeinstead.- functionExpressionPath.equals("id", idNode)
+ functionExpressionPath.node.id === idNode
- functionExpressionPath.is("id")
- functionExpressionPath.has("id")
+ functionExpressionPath.node.id
- functionExpressionPath.has("arguments")
+ !!functionExpressionPath.node.arguments.length
- functionExpressionPath.isnt("async")
+ !functionExpressionPath.node.async -
移除
Scope.prototype.traverse、Scope#parentBlock和Scope#hub(#16705)¥Remove
Scope.prototype.traverse,Scope#parentBlockandScope#hub(#16705)迁移:改用
scope.path的方法和属性:¥Migration: Use
scope.pathmethods and properties instead:- scope.traverse(scopeRootNode, visitor, state)
+ scope.path.traverse(visitor, state)
- scope.parentBlock
+ scope.path.parent
- scope.hub
+ scope.path.hub -
移除
Scope.prototype.getAllBindingsOfKind和Scope.prototype.toArray(#16705)¥Remove
Scope.prototype.getAllBindingsOfKindandScope.prototype.toArray(#16705)这些方法已被删除,因为它们在我们的代码库中不再使用。
¥These methods have been removed as they are not used anymore in our code base.
迁移:你可以将它们从 Babel 7 的源代码复制粘贴到你的插件中。
¥Migration: You can copy&paste them from Babel 7's source to your plugin.
-
从
NodePath(#16655) 中移除hoist、updateSiblingKeys、call、setScope、resync、popContext、pushContext、setup、setKey方法¥Remove
hoist,updateSiblingKeys,call,setScope,resync,popContext,pushContext,setup,setKeymethods fromNodePath(#16655)这些方法应为私有方法,因此没有真正的迁移方法。如果你的插件/构建因此更改而无法正常工作,请随时打开一个问题并告诉我们你如何使用这些方法,我们将在 Babel 8 发布后看看可以做些什么。
¥These methods are meant to be private so there is no real migration approach. If your plugin / build is broken by this change, feel free to open an issue and tell us how you use these methods and we can see what we can do after Babel 8 is released.
-
从
Scope#rename中移除block参数(#15288)¥Remove
blockargument fromScope#rename(#15288)- rename(oldName: string, newName?: string, block?: t.Pattern | t.Scopable)
+ rename(oldName: string, newName?: string)迁移:不要将其他块传递给
scope.rename(),而是直接在该块对应的作用域中调用.rename()。¥Migration: Instead of passing a different block to
scope.rename(), directly call.rename()on the scope corresponding to that block. -
将
Scope#uids和Scope#references替换为Scope#uidsSet和Scope#referencesSet(#16624)¥Replace
Scope#uidsandScope#referenceswithScope#uidsSetandScope#referencesSet(#16624)它们已从布尔值对象更改为
Set对象。此外,它们现在仅在程序作用域 (scope.getProgramParent()) 上定义,而不是在所有作用域对象上定义。¥They have been changed from objects with boolean values to
Setobjects. In addition to that, they are now only defined on the program scope (scope.getProgramParent()) rather than on all scope objects.- if (programScope.references["foo"])
+ if (programScope.referencesSet.has("foo")) -
允许将跳过的
NodePath重新排队 (#13291)¥Allow skipped
NodePaths to be requeued (#13291)NodePath#requeue()可以重新排队跳过的 NodePath。这实际上是一个错误修复,但它会导致 Babel 7 中@babel/plugin-transform-block-scoping的 暂时死区 实现出现无限循环:它也可能会破坏其他插件。¥
NodePath#requeue()can requeue a skipped NodePath. This is actually a bugfix, but it causes an infinite loop in the temporal dead zone implementation of@babel/plugin-transform-block-scopingin Babel 7: it may break other plugins as well.迁移:如果你想保留旧的行为,可以在调用
NodePath#requeue()之前使用NodePath#shouldSkip检查 NodePath 是否被跳过。¥Migration: If you want to preserve the old behavior, you can use
NodePath#shouldSkipto check whether a NodePath has been skipped before callingNodePath#requeue(). -
从
Scope和NodePath(#16504、#16705) 中移除以_开头的方法¥Remove methods starting with
_fromScopeandNodePath(#16504, #16705)// NodePath.prototype
_assertUnremoved
_call
_callRemovalHooks
_containerInsert
_containerInsertAfter
_containerInsertBefore
_getKey
_getPattern
_getQueueContexts
_getTypeAnnotation
_markRemoved
_remove
_removeFromScope
_replaceWith
_resolve
_resyncKey
_resyncList
_resyncParent
_resyncRemoved
_verifyNodeList
// Scope.prototype
_renameFromMap
_generateUid这些方法应为私有方法,因此没有真正的迁移方法。如果你的插件/构建因此更改而无法正常工作,请随时打开一个问题并告诉我们你如何使用这些方法,我们将在 Babel 8 发布后看看可以做些什么。
¥These methods are meant to be private so there is no real migration approach. If your plugin / build is broken by this change, feel free to open an issue and tell us how you use these methods and we can see what we can do after Babel 8 is released.
@babel/eslint-plugin
-
删除 exports 对象的
default属性 (#14180)¥Remove the
defaultproperty of the exports object (#14180)迁移:如果你在 ESLint 配置中使用该插件,则此项变更无效。但是,如果你要扩展
@babel/eslint-plugin,请确保从require("@babel/eslint-plugin")而不是require("@babel/eslint-plugin").default获取导出。¥Migration: This change has no effect if you are using the plugin in your ESLint config. However, if you are extending
@babel/eslint-plugin, ensure that you obtain exports fromrequire("@babel/eslint-plugin")rather thanrequire("@babel/eslint-plugin").default.my-eslint-plugin.cjs// Don't add `.default` after `require()`
const { rules, rulesConfig } = require("@babel/eslint-plugin")
@babel/compat-data
-
将
plugins.json(#14976)中的第 4 阶段插件条目从proposal-*重命名为transform-*¥Rename stage 4 plugin entries from
proposal-*totransform-*inplugins.json(#14976)此项变更还会影响
@babel/helper-compilation-targets的isRequired函数,该函数接收插件名称作为其第一个参数。¥This change also affects the
isRequiredfunction of@babel/helper-compilation-targets, which receives plugin names as its first parameter.迁移:例如,使用
transform-class-properties而不是proposal-class-properties。有关重命名插件的完整列表,请参阅 Babel 8 迁移的软件包重命名部分。¥Migration: For example, use
transform-class-propertiesrather thanproposal-class-properties. For a complete list of renamed plugin, see Packages Renames section of Babel 8 migration.my-babel-plugin.jsmodule.exports = api => {
const targets = api.targets();
// The targets have native optional chaining support
// if `transform-optional-chaining` is _not_ required
const optionalChainingSupported = !isRequired(
- "proposal-optional-chaining",
+ "transform-optional-chaining",
targets
);
}; -
直接将数据导出为 JSON 文件(#17267)。
¥Directly export data as JSON files (#17267)
软件包的入口点现在是 JSON 文件,而不是 JS 文件。
¥The entrypoints of the package are now JSON files, rather than JS files.
迁移:如果你从 ESM 导入此包,则需要更改导入,并在其中添加
with { type: "json" }。¥Migration: If you are importing this package from ESM, you will need to change your imports adding
with { type: "json" }to them.
-
从
data/native-modules.json中移除ios_saf(#15068)¥Remove
ios_saffromdata/native-modules.json(#15068)迁移:请改用
ios。¥Migration: Use
iosinstead.
@babel/helper-replace-supers
-
移除命名导出
environmentVisitor(#15550)¥Remove named export
environmentVisitor(#15550)迁移:从
@babel/helper-environment-visitor导入。¥Migration: Import it from
@babel/helper-environment-visitor.- import { environmentVisitor } from "@babel/helper-replace-supers";
+ import environmentVisitor from `@babel/helper-environment-visitor`; -
移除命名导出
skipAllButComputedKey(#15550)¥Remove named export
skipAllButComputedKey(#15550)迁移:请改用
requeueComputedKeyAndDecorators。查找并替换以下导入和用法。¥Migration: Use
requeueComputedKeyAndDecoratorsinstead. Find and replace the following import and usagemy-babel7-plugin.jsimport { skipAllButComputedKey } from "@babel/helper-replace-supers";
skipAllButComputedKey(path);to
my-babel8-plugin.jsimport { requeueComputedKeyAndDecorators } from `@babel/helper-environment-visitor`;
path.skip();
requeueComputedKeyAndDecorators(path);
@babel/helper-simple-access
-
从默认导出 (#15550) 中删除第三个参数
includeUpdateExpression¥Remove the the third parameter
includeUpdateExpressionfrom the default export (#15550)请注意,Babel 8 的行为与默认的 Babel 7 行为(即
includeUpdateExpression: true)相同。¥Note that the Babel 8 behavior is the same as the default Babel 7 behavior (i.e.
includeUpdateExpression: true).
@babel/highlight
-
删除
getChalk函数 (https://github.com/babel/babel/pull/15812)¥Remove the
getChalkfunction (https://github.com/babel/babel/pull/15812)如果你需要使用
chalk,请将其添加到你的依赖中。¥If you need to use
chalk, add it to your dependencies.
插件 API 变更
¥Plugin API changes
-
从插件传递中移除
getModuleName(#12724)¥Remove
getModuleNamefrom plugin pass (#12724)迁移:请改用
.file.getModuleName。¥Migration: Use
.file.getModuleNameinstead.my-babel-plugin.jsmodule.exports = {
name: "my-babel-plugin",
visitor: {
Identifier(path, pass) {
- const moduleName = pass.getModuleName();
+ const moduleName = pass.file.getModuleName();
}
}
}
-
从插件传递中移除
addImport(#15576)¥Remove
addImportfrom plugin pass (#15576)此更改可能不会影响你的插件,因为此方法在 Babel 7 中已经引发错误。
¥This change probably will not affect your plugin as this method is already throwing an error in Babel 7.
迁移:改用
@babel/helper-module-imports中的addNamed或addDefault。¥Migration: Use
addNamedoraddDefaultfrom@babel/helper-module-importsinstead. -
停止支持将插件字段作为命名导出 (#15576)
¥Stop supporting plugin fields as named exports (#15576)
例如,以下文件不再是有效的插件:
¥For example, the following file is not a valid plugin anymore:
legacy-babel-plugin.jsexport const name = "legacy-babel-plugin";
export const visitor = {
Identifier() {}
}迁移:找到此类模式并使用默认导出,例如导出插件对象或工厂函数。
¥Migration: Find such patterns and use a default export instead, either exporting a plugin object or a factory function.
my-babel-plugin.cjsexport default {
name: "babel-plugin",
visitor: {
Identifier() {}
}
}