Skip to main content

@babel/plugin-syntax-import-attributes

信息

此插件包含在 @babel/preset-env 中,在 ES2025

¥This plugin is included in @babel/preset-env, in ES2025

注意

仅语法

¥Syntax only

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

¥This plugin only enables Babel to parse and generate this syntax. Babel does not support transforming this syntax.

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

¥While Babel supports parsing import attributes by default since v7.25.0, this plugin is still needed to let Babel choose the correct syntax when emitting code. As an alternative to this plugin, you can use @babel/generator's importAttributesKeyword option:

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

:::

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

¥This plugin enables Babel to parse import attributes:

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

安装

¥Installation

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

用法

¥Usage

¥With a configuration file (Recommended)

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

通过 CLI

¥Via CLI

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

通过 Node API

¥Via Node API

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

选项

¥Options

deprecatedAssertSyntax

boolean,默认为 false

¥boolean, defaults to false.

如果启用,支持使用 已弃用 assert 关键字解析导入属性:

¥If enabled, support parsing import attributes using the deprecated assert keyword:

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

此语法仅在基于 V8 的引擎中受支持,并且正在研究从 Web 中删除它。

¥This syntax is only supported in V8-based engines, and its removal from the web is being investigated.