处理 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.

最新文章

  1. 在 CentOS7 上安装 MongoDB
  2. charles使用
  3. hdu 2010 - 水仙花数
  4. topcoder SRM 622 DIV2 FibonacciDiv2
  5. CC3000 主机驱动API介绍
  6. ASP.NET的运行原理与运行机制 如何:为 IIS 7.0 配置 <system.webServer> 节
  7. CentOS 安裝 VMware Workstation / VMware Player
  8. java 抽象类和接口
  9. Part 6 Group by in sql server
  10. flask前后台交互数据的几个思路
  11. 小白日记16:kali渗透测试之弱点扫描-openvas、nessus
  12. ajax实现下载功能
  13. Unreal Engine 4(虚幻UE4)GameplayAbilities 插件入门教程(四)技能屏蔽和简单的Buff等
  14. SQL基础----DCL
  15. idea常用操作大全
  16. 结合OPENSIFT源码详解SIFT算法
  17. 属性动画总结(Property Animation)
  18. 如何查看linux程序被何种版本的编译器编译的?
  19. kbmMW 5.08.01压力测试报告
  20. Java Message Service学习(一)

热门文章

  1. 20190906 On Java8 第十八章 字符串
  2. [转]JavaScript构造函数及原型对象
  3. 028 (H5*) 商城实战
  4. “希希敬敬对”队软件工程第九次作业-beta冲刺第一次随笔
  5. JAVA总结--java数据类型
  6. 字典树(Trie树)实现与应用(转)
  7. VirtualBox虚拟机与主机互通,并且虚拟机又能上网配置
  8. stylus快速上手
  9. iOS开发-retain/assign/strong/weak/copy/mutablecopy/autorelease区别
  10. Jupyter配置工作路径