我如何用webpack填充Promise?

Wil*_*hes 40 javascript polyfills internet-explorer-11 webpack

我正在使用webpack捆绑我的JavaScript.我依赖于使用任何承诺的冰棒等模块.

这是我的代码:

var popsicle = require('popsicle');
popsicle.get('/').then(function() {
  console.log('loaded URL');
});
Run Code Online (Sandbox Code Playgroud)

这在Promise可用的浏览器中工作正常,但IE 11不提供Promise.所以我想使用es6-promise作为polyfill.

我尝试添加一个明确的ProvidePlugin给我webpack.config.js:

plugins: [
  new webpack.ProvidePlugin({
    'Promise': 'exports?global.Promise!es6-promise'
  })
]
Run Code Online (Sandbox Code Playgroud)

但我仍然在IE 11中得到错误:any-promise browser requires a polyfill or explicit registration e.g: require('any-promise/register/bluebird').

我尝试明确附加全局:

global.Promise = global.Promise || require('es6-promise');
Run Code Online (Sandbox Code Playgroud)

但IE 11给出了一个不同的错误:Object doesn't support this action.

我也尝试过明确注册es6-promise:

require('any-promise/register/es6-promise');
var popsicle = require('popsicle');
Run Code Online (Sandbox Code Playgroud)

这有效,但我必须在每个加载的文件中执行此操作popsicle.我想Promise加入window.

如何window.Promise使用webpack 确保始终定义?

这里有完整的回购演示.

Jer*_*ims 66

对于将babel与webpack结合使用的人:您可以使用babel-polyfill

只需这样做npm install --save "babel-polyfill",然后将其添加为webpack.config.js中的入口点:

module.exports = {
   entry: ['babel-polyfill', './app/js']
};
Run Code Online (Sandbox Code Playgroud)

或者,您可以安装core-js和引用您需要的模块,而不是使用整个babel-polyfill .

module.exports = {
   entry: ['core-js/fn/promise', './app/js']
};
Run Code Online (Sandbox Code Playgroud)

  • Webpack v3.11,TS v2.7.x和node> v9,支持动态模块导入,问题是你不能导入Promise polyfill,因为动态导入还依赖于Promises.因此我手动完成:`if(!(窗口中的"Promise")){var head = document.getElementsByTagName("head")[0]; var script = document.createElement("script"); script.src ="https://../YourPromisePolyfill.js"; head.appendChild(脚本为HTMLScriptElement); 你当然可以使用require/import来加载包,但是它会得到Bundeled并且即使在不需要时也会被下载 (3认同)

asi*_*niy 20

对我有用的选择.es6-promise-promise旨在直接包含在webpack构建中.所以:

plugins: [
  new webpack.ProvidePlugin({
    Promise: 'es6-promise-promise'
  })
]
Run Code Online (Sandbox Code Playgroud)

  • 在使用`ProvidePlugin`然后尝试做一些`require.ensure`时,仍然在IE 11中获得`ReferenceError` (2认同)

sta*_*afl 9

使用最近添加@ asiniy的答案的更新和简洁版本property的功能ProvidePlugin,而不需要参考es6-promise-promise:

    new webpack.ProvidePlugin({
        Promise: ['es6-promise', 'Promise']
    })
Run Code Online (Sandbox Code Playgroud)

为此,请不要忘记添加es6-promise为要填充的项目的依赖项Promise.


Ben*_*Ben 6

babel polyfill usually works, but this time for a vuejs(webpack1) project, somehow not working.

Fortunately, core-js works as a charm.

npm install core-js --save

module.exports = {
   entry: ['core-js/fn/promise', './app/js']
};
Run Code Online (Sandbox Code Playgroud)


Ale*_*arc 5

更好地使用蓝鸟

plugins: [
  new webpack.ProvidePlugin({
    Promise: 'bluebird'
  }),

  new webpack.NormalModuleReplacementPlugin(/es6-promise$/, 'bluebird'),
]
Run Code Online (Sandbox Code Playgroud)


Gab*_*rit 1

你几乎明白了 - 在你的webpack.config.js

plugins: [
  new webpack.ProvidePlugin({
    Promise: 'imports?this=>global!exports?global.Promise!es6-promise'
  })
]
Run Code Online (Sandbox Code Playgroud)

请注意,您需要安装imports-loader并且exports-loader

npm install --save imports-loader exports-loader