Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel的polyfill解决方案 #23

Open
thecatshidog opened this issue Jul 25, 2017 · 0 comments
Open

babel的polyfill解决方案 #23

thecatshidog opened this issue Jul 25, 2017 · 0 comments

Comments

@thecatshidog
Copy link
Owner

Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,比如Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise等全局对象,以及一些定义在全局对象上的方法(比如Object.assign)都不会转码。

1、使用babel-runtime和babel-plugin-transform-runtime

babel-runtime提供helper函数,将我们需要的api注入到我们代码中

const Promise = require('babel-runtime/core-js/promise')

babel-plugin-transform-runtime将每个模块的helper函数提取出来变成一个公共模块,这样就不用重复的引用代码。

问题:
1、不能转码实例代码,下面这样的代码就不能被转换

[1, 2, 3].include(1)

注意:
1、transform-runtime依赖于babel-runtime,.babelrc虽然不需要写babel-runtime,但是需要在package.json中加上。
2、有些不能通过runtime方式来解决的问题,可以使用再加上plugin去解决问题。

2、使用babel-polyfill+babel-preset-env方式

在runtime方式中不能解决实例方法的转化,那么我们可以使用babel-polyfill在webpack的entry中引入,然后在babel-preset-env的配置中添加一个useBuiltIns为true,babel就会将引入的babel-polyfill自动转化成项目所需的polyfill。

参考文章:
1、Babel 全家桶
2、无处不在的Babel
3、21分钟精通polyfill方案

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant