Skip to main content

@babel/plugin-syntax-import-attributes

info

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

note

仅语法​

🌐 Syntax only

这个插件只是让 Babel 能解析和生成这种语法。Babel 并不支持转换这种语法。

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

这个插件使 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。

如果启用,将支持使用已弃用的 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.