@babel/plugin-proposal-import-attributes-to-assertions
此插件将生成与当前 ECMAScript 规范或任何当前提议的添加不兼容的代码。仅当你交付原生 ES 模块并且需要与不支持导入属性语法(import pkg from "./package.json" with { type: "json" }
)但支持旧导入断言语法(import pkg from "./package.json" assert { type: "json" }
)的工具兼容时才使用它,例如 Chrome 91 - 122
和 Node.js ^16.14 || ^18 || ^20 < 20.10
。
¥This plugin will generate code that is not compatible with the current ECMAScript specification or with any currently proposed addition to it. Only use it when you are shipping native ES modules and you need compatibility exclusively with tools that don't support the Import Attributes syntax (import pkg from "./package.json" with { type: "json" }
) but support the old Import Assertions syntax (import pkg from "./package.json" assert { type: "json" }
), such as Chrome 91 - 122
and Node.js ^16.14 || ^18 || ^20 < 20.10
.
安装
¥Installation
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-proposal-import-attributes-to-assertions
yarn add --dev @babel/plugin-proposal-import-attributes-to-assertions
pnpm add --save-dev @babel/plugin-proposal-import-attributes-to-assertions
用法
¥Usage
使用配置文件(推荐)
¥With a configuration file (Recommended)
{
"plugins": ["@babel/plugin-proposal-import-attributes-to-assertions"]
}
通过 CLI
¥Via CLI
babel --plugins @babel/plugin-proposal-import-attributes-to-assertions script.js
通过 Node API
¥Via Node API
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-import-attributes-to-assertions"],
});