前端工具-ES6相关工具

处理ES6语法

使用Babel

Babel是一个用于将ECMAScript 2015+ 代码转换为新旧浏览器或其他环境支持的JavaScript向下兼容版本代码的工具链。

Babel主要可以做:

  • 语法翻译
  • 拓展(Polyfill)您的目标环境不支持的特性
  • 源码转换

处理ES6的module

  1. 使用Babel的插件babel-plugin-transform-es2015-modules-commonjs · Babel将module转化为commonjs
  2. 使用用Rollup或者webpack打包

支持ES6新的API(Promise等)

使用babel-polyfill

这个插件是要写入到最后生成的JS文件中的,在浏览器执行JS时先运行polyfill将特性加到全局环境(生成器函数倒是需要翻译源代码)。

使用babel-plugin-transform-runtime可以复用Babel注入的帮助代码,减小代码体积。

相关资料:

This means you can use new built-ins like Promise or WeakMap, static methods like Array.from or Object.assign, instance methods like Array.prototype.includes, and generator functions (provided you use the regenerator plugin). The polyfill adds to the global scope as well as native prototypes like String in order to do this.

http://www.ruanyifeng.com/blog/2016/01/babel.html
Babel 默认只转换新的 JavaScript 句法(syntax),而不转换新的 API ,比如 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象,以及一些定义在全局对象上的方法(比如 Object.assign)都不会转码。Babel 默认不转码的 API 非常多,详细清单可以查看 definitions.js 文件

http://babeljs.io/docs/en#polyfill
Since Babel only transforms syntax (like arrow functions), you can use babel-polyfill in order to support new globals such as Promise or new native methods like String.padStart (left-pad). It uses core-js and regenerator. Check out our babel-polyfill docs for more info.

支持ES6的fetch

babel-polyfill不支持fetch,得用github/fetch: A window.fetch JavaScript polyfill.

相关资料:

作者说fetch不是跨平台特性,在一些环境中它没有意义就没加进来

window.fetch is not a cross-platform feature, in some environments it makes no sense. For this reason, I don't think it should be in core-js. Looking at a large number of requests it may be added in the future. Now you can use, for example, this polyfill.

转载于:https://www.cnblogs.com/jffun-blog/p/10212007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值