7.28.0 发布:支持 `babel.config.ts`、显式资源管理及弃用绑定提案
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
Babel 7.28.0 正式发布!
本次更新包含对 babel.config.ts 和 babel.config.mts 的支持、ES2026 显式资源管理特性、弃用绑定提案,以及 sourceType: "commonjs" 选项。
您可以在 GitHub 查看完整更新日志。若您正在使用 Babel 8 beta,所有 7.28.0 的新特性已包含在 v8.0.0-beta.1 中。
如果您或您的公司希望支持 Babel 和 JavaScript 的发展但不知如何行动,可以通过我们的 Open Collective 进行捐赠,更可直接参与 新 ECMAScript 提案 的实现!作为志愿者驱动的项目,我们依赖社区支持来为广泛的 JavaScript 用户提供持续服务。欢迎通过 team@babeljs.io 联系我们详谈!
重点更新
原生支持 babel.config.ts 和 babel.config.mts (#17392)
Babel 现已原生支持 babel.config.ts 和 babel.config.mts。建议使用 Node.js 24 以获得最佳效果。
若您正在使用 Babel 8.0.0-beta.1,可直接从 @babel/core 导入类型并在 Babel 配置中使用:
// Requires Babel 8.0.0-beta.1 or above
import type { ConfigAPI, InputOptions } from "@babel/core";
export default function babelConfig(api: ConfigAPI): InputOptions {
return {
assumptions: {
noDocumentAll: true
},
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
"@babel/preset-typescript",
],
};
}
默认启用显式资源管理 (#17346, #17355)
{
using handlerSync = openSync();
await using handlerAsync = await openAsync();
// handlerSync and handlerAsync will be disposed when the block exits
}
显式资源管理 已在 2025 年 5 月 TC39 会议上有条件进入 Stage 4,现已在 @babel/preset-env 和 @babel/parser 中默认启用。
若您正在使用 @babel/plugin-proposal-explicit-resource-management 插件和 @babel/preset-env,现在可从配置中移除该插件。
若您使用了 explicitResourceManagement 解析器插件,可将其从配置中移除。
弃用绑定 (#17276)
Babel 现已支持转换 弃用绑定 提案。您可使用 void 绑定标记未使用的变量或参数:
{
using void = new AcquireLock(mutex);
// The mutex lock will be automatically disposed when the block exits
}
// A customized JSON serializer with bigint support
// The first parameter of the replacer is unused
JSON.stringify(input, (void, value) => {
if (typeof value === "bigint") {
return value.toString();
} else {
return value.toJSON();
}
});
// Get a clone of the input object without property `z`
const { z: void, ...obj } = { x: 1, y: 2, z: 3 };
obj; // { x: 1, y: 2 }
您可通过在 Babel 配置中添加 @babel/plugin-proposal-discard-binding 插件来启用此功能。
export default {
plugins: [
["@babel/plugin-proposal-discard-binding", { syntaxType: "void" }]
]
}
请注意该提案可能仍会改用不同语法:因此必须配置 syntaxType 选项,且当前唯一有效值为 "void"。
sourceType: "commonjs" (#17390)
"commonjs" 模式表示代码应在 CommonJS 环境(如 Node.js)中运行。该模式类似 "script" 模式,但允许在顶层使用 return、new.target 以及 using/await using 声明。
export default {
// Specify commonjs sourceType for all `*.cjs` sources
overrides: [
{
test: "*.cjs",
sourceType: "commonjs"
}
]
}
当使用特定于 CommonJS 的语法时,您应当使用 sourceType: "commonjs";而当编写非模块的 <script> 标签时,则应使用 sourceType: "script"。