@babel/plugin-syntax-import-attributes
仅语法
¥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/generator
的 importAttributesKeyword
选项:
¥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:
import foo from "./foo.json" with { type: "json" };
安装
¥Installation
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-syntax-import-attributes
yarn add --dev @babel/plugin-syntax-import-attributes
pnpm add --save-dev @babel/plugin-syntax-import-attributes
用法
¥Usage
使用配置文件(推荐)
¥With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-syntax-import-attributes"]
}
通过 CLI
¥Via CLI
babel --plugins @babel/plugin-syntax-import-attributes script.js
通过 Node API
¥Via Node API
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:
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.