程序员

前端工程搭建入门(六)支持es6新特性

2018-08-05  本文已影响0人  文者字清

背景

Polyfill

综上,polyfill 的原则就是,对于全局变量 Map Set Promise 等,以及 es6 新增的方法全部重写。async 是通过 Promise 实现的,并且每个出现引用的模块都会定义一次。

transform-runtime

The runtime transformer plugin does three things:

  • Automatically requires babel-runtime/regenerator when you use generators/async functions.
  • Automatically requires babel-runtime/core-js and maps ES6 static methods and built-ins.
  • Removes the inline Babel helpers and uses the module babel-runtime/helpers instead.
    What does this actually mean though? Basically, you can use built-ins such as Promise, Set, Symbol, etc., as well use all the Babel features that require a polyfill seamlessly, without global pollution, making it extremely suitable for libraries.

runtime 编译器插件做了以下三件事:

  • 当你使用 generators/async 函数时,自动引入 babel-runtime/regenerator 。
  • 自动引入 babel-runtime/core-js 并映射 ES6 静态方法和内置插件。
  • 移除内联的 Babel helper 并使用模块 babel-runtime/helpers 代替。
    这意味着什么?基本上,你可以使用诸如 Promise,Set,Symbol 等内置函数,以及所有需要 polyfill 来完成且不带来全局污染的 Babel 功能,因此非常适合作为库使用。

综上,transform-runtime 不会污染全局环境,不会污染原型,至于文档说的 沙盒环境 ,暂时没感受到,希望以后的开发过程中能够体会到其真正用意。

这些就是我一步一步去仔细体会 polyfill 和 transform-runtime 的一些区别,当然只是通过一个简单的列子,不能展现所有的实现原理,只是感受二者的基本差异。可能有错误的理解,希望大家多多指正,共同学习,共同进步 。

上一篇下一篇

猜你喜欢

热点阅读