跳至主内容

@babel/插件语法-导入属性

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

信息

此插件已包含在 @babel/preset-env 中,属于 ES2025 标准

备注

仅语法支持

此插件仅允许 Babel 解析和生成此语法。Babel 不支持转换此语法。

虽然自 v7.25.0 起 Babel 默认支持解析导入属性,但在生成代码时仍需要此插件来选择正确的语法。作为此插件的替代方案,你可以使用 @babel/generatorimportAttributesKeyword 选项:

{
"generatorOpts": {
"importAttributesKeyword": "with"
},
}

:::

此插件使 Babel 能够解析导入属性:

JavaScript
import foo from "./foo.json" with { type: "json" };

安装

npm install --save-dev @babel/plugin-syntax-import-attributes

用法

通过配置文件(推荐)

babel.config.json
{
"plugins": ["@babel/plugin-syntax-import-attributes"]
}

通过命令行

Shell
babel --plugins @babel/plugin-syntax-import-attributes script.js

通过 Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-syntax-import-attributes"]
});

配置选项

deprecatedAssertSyntax

boolean,默认值 false

若启用此选项,则支持使用已弃用的 assert关键字解析导入属性:

JavaScript
import foo from "./foo.json" assert { type: "json" };

此语法仅在基于 V8 的引擎中受支持,并且正在研究将其从 web 中移除。