@babel/runtime
@babel/runtime 是一个包含 Babel 模块化运行时助手的库。
¥@babel/runtime is a library that contains Babel modular runtime helpers.
安装
¥Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save @babel/runtime
yarn add @babel/runtime
pnpm add @babel/runtime
bun add @babel/runtime
也可以看看:
@babel/runtime-corejs2。¥See also:
@babel/runtime-corejs2.
用法
¥Usage
这意味着与 Babel 插件 @babel/plugin-transform-runtime 一起用作运行时 dependency。请查看该软件包中的文档以了解使用情况。
¥This is meant to be used as a runtime dependency along with the Babel plugin @babel/plugin-transform-runtime. Please check out the documentation in that package for usage.
为什么
¥Why
有时 Babel 可能会在输出中注入一些跨文件相同的代码,因此可能会被重用。
¥Sometimes Babel may inject some code in the output that is the same across files, and thus can be potentially re-used.
例如,使用类变换(没有松散模式):
¥For example, with the class transform (without loose mode):
class Circle {}
变成:
¥turns into:
function _classCallCheck(instance, Constructor) {
//...
}
var Circle = function Circle() {
_classCallCheck(this, Circle);
};
这意味着每个包含类的文件每次都会重复 _classCallCheck 函数。
¥this means every file that contains a class would have the _classCallCheck function repeated each time.
对于 @babel/plugin-transform-runtime,它将替换对 @babel/runtime 版本的函数的引用。
¥With @babel/plugin-transform-runtime, it would replace the reference to the function to the @babel/runtime version.
var _classCallCheck = require("@babel/runtime/helpers/classCallCheck");
var Circle = function Circle() {
_classCallCheck(this, Circle);
};
@babel/runtime 只是以模块化方式包含功能实现的包。
¥@babel/runtime is just the package that contains the implementations of the functions in a modular way.