Skip to main content

@babel/plugin-proposal-import-attributes-to-assertions

caution

这个插件会生成与当前 ECMAScript 规范或任何正在提议的扩展不兼容的代码。只有在你发布原生 ES 模块,并且只需要兼容那些不支持 Import Attributes 语法(import pkg from "./package.json" with { type: "json" })但支持旧的 Import Assertions 语法(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 install --save-dev @babel/plugin-proposal-import-attributes-to-assertions

用法​

🌐 Usage

🌐 With a configuration file (Recommended)

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

通过 CLI​

🌐 Via CLI

Shell
babel --plugins @babel/plugin-proposal-import-attributes-to-assertions script.js

通过 Node API​

🌐 Via Node API

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