@babel/runtime

@babel/runtimeopen in new window是一个工具库,包含了 Babel 模块化的运行时辅助工具函数和regenerator-runtime

安装

npm install --save @babel/runtime
1

另可参考@babel/runtime-corejs2open in new window

使用

这意味着,该插件是作为@babel/plugin-transform-runtime插件的运行时依赖使用的。

Why

有些 Babel 可能会往编译输出里注入一些代码,而这些代码可能是重复的,因此存在复用这些代码的可能。

比如,class的转换(非loose模式):

class Circle {}
1

转换为:

function _classCallCheck(instance, Constructor) {
  //...
}

var Circle = function Circle() {
  _classCallCheck(this, Circle);
};
1
2
3
4
5
6
7

这意味着每个包含class的文件都会存在一个_classCallCheck函数。若是使用@babel/plugin-transform-runtime,将会把对_classCallCheck的引用替换为对@babel/runtime模块的引用。

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

var Circle = function Circle() {
  _classCallCheck(this, Circle);
};
1
2
3
4
5

@babel/runtime仅是以模块化方式包含了这些函数的实现的包。