"corejs": 2 是相对于@babel/runtime-corejs2与@babel/core与core-js中谁配置的
时间: 2023-06-26 17:07:15 浏览: 60
"@babel/runtime-corejs2" 和 "@babel/core" 都是 Babel 的依赖项,而 "core-js" 则是一个 JavaScript 标准库,提供了许多 ECMAScript 扩展功能的 polyfill 实现。在 Babel 的配置中, "corejs": 2 表示使用 core-js@2 版本的 polyfill。这个值的具体含义是告诉 Babel 使用 "@babel/runtime-corejs2" 这个包来提供 polyfill,而不是使用 "@babel/runtime"。因此,"corejs": 2 的设置与 "@babel/runtime-corejs2" 中的配置有关,与 "@babel/core" 无关。
相关问题
@babel/runtime-corejs2与@babel/core与core-js怎么在.babelrc下配置
在`.babelrc`文件中,可以使用以下方式配置`@babel/runtime-corejs2`、`@babel/core`和`core-js`:
```json
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": 2
}
]
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
```
- `"presets": ["@babel/preset-env"]`:这表示使用`@babel/preset-env`预设来转换代码,它可以根据目标环境自动选择需要的插件和polyfills。
- `"useBuiltIns": "usage"`:这个选项告诉`@babel/preset-env`根据代码中使用的特性,自动添加所需的polyfills。默认值为`false`,可以设置为`entry`或`usage`。`entry`会在打包时将所有polyfills导入,无论是否使用,而`usage`则只会导入代码中实际使用的polyfills。
- `"corejs": 2`:这个选项告诉`@babel/preset-env`使用`core-js@2`来提供polyfills。
因为`@babel/plugin-transform-runtime`依赖于`@babel/runtime-corejs2`,所以在安装了这两个包之后,将`"@babel/plugin-transform-runtime"`添加到`"plugins"`中即可。
@babel/runtime-corejs2与@babel/core与core-js有什么区别
`@babel/runtime-corejs2` 是一个 Babel 插件,用于将 ES6+ 代码转换为向后兼容的代码,以便在旧版本的浏览器和环境中运行。它使用了 `core-js` 库来提供这些向后兼容的特性。
`@babel/core` 是 Babel 的核心库,它负责解析、转换和生成 JavaScript 代码。它允许你在项目中配置不同的插件和预设,以满足不同的需求。
`core-js` 则是一个包含 ECMAScript 标准库的库,它提供了许多 ES6+ 的新特性,例如 Promise、Map、Set 等等。它可以在任何 JavaScript 环境下使用,包括浏览器和 Node.js。
简而言之,`@babel/runtime-corejs2` 是 Babel 插件,依赖于 `core-js` 库来提供向后兼容的特性,而 `@babel/core` 则是 Babel 的核心库,用于解析、转换和生成 JavaScript 代码。`core-js` 则是一个包含 ECMAScript 标准库的库,它提供了许多 ES6+ 的新特性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)