Skip to main content

@babel/runtime

@babel/runtime 是一个包含 Babel 模块化运行时辅助工具的库。

安装​

🌐 Installation

npm install --save @babel/runtime

另见:@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):

JavaScript
class Circle {}

变成:

🌐 turns into:

JavaScript
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.

JavaScript
var _classCallCheck = require("@babel/runtime/helpers/classCallCheck");

var Circle = function Circle() {
_classCallCheck(this, Circle);
};

@babel/runtime只是一个以模块化方式包含函数实现的包。