如何安装babel-polyfill库? [英] How do I install the babel-polyfill library?
问题描述
我刚刚开始使用Babel将我的ES6 javascript代码编译成ES5。当我开始使用Promises时,看起来它不起作用。 Babel网站声明支持通过polyfill承诺。
I just started to use Babel to compile my ES6 javascript code into ES5. When I start to use Promises it looks like it's not working. The Babel website states support for promises via polyfills.
没有任何运气,我试图添加:
Without any luck, I tried to add:
require("babel/polyfill");
或
import * as p from "babel/polyfill";
这样我的应用程序引导就会出现以下错误:
With that I'll get the following error on my app bootstrapping:
无法找到模块'babel / polyfill'
Cannot find module 'babel/polyfill'
我搜索了模块,但似乎我在这里缺少一些基本的东西。我也尝试添加旧的和好的蓝鸟NPM,但看起来它不起作用。
I searched for the module but it seems I'm missing some fundamental thing here. I also tried to add the old and good bluebird NPM but it looks like it's not working.
如何使用Babel的polyfills?
How to use the polyfills from Babel?
推荐答案
这在babel v6中有所改变。
This changed a bit in babel v6.
来自文档:
polyfill将模拟完整的ES6环境。使用babel-node时会自动加载此polyfill。
The polyfill will emulate a full ES6 environment. This polyfill is automatically loaded when using babel-node.
安装:
$ npm install babel-polyfill
Node / Browserify / Webpack中的用法: < br>
要包含polyfill,您需要在应用程序入口点的顶部。
require(babel-polyfill);
在浏览器中使用
可从 dist / polyfill获取.js
babel-polyfill
npm版本中的文件。这需要包含在所有编译的Babel代码之前。您可以将其添加到已编译的代码中,也可以将其包含在< script>
之前。
Usage in Browser:
Available from the dist/polyfill.js
file within a babel-polyfill
npm release. This needs to be included before all your compiled Babel code. You can either prepend it to your compiled code or include it in a <script>
before it.
注意:不要要求
这通过browserify等,使用 babel-polyfill
。
NOTE: Do not require
this via browserify etc, use babel-polyfill
.
这篇关于如何安装babel-polyfill库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!