Polyfilling Promises Using Bluebird in Webpack 2

Last updated: March 8, 2017

Surprisingly, whilst determining the best way to polyfill promises in a Webpack build and using Bluebird I came across such mixed results.

A lot of the posts out there don’t mention Bluebird at all, opting for es6-promise when it has been proven Bluebird has such great performance, it’s even faster than native promises in some browsers.

A lot of the results I found were from 2015 and a couple in 2016. As you know, front-end development moves rapidly and 2015 is 100 years in front-end land.

I wanted to specifically know the best way to polyfill promises using Bluebird in Webpack 2.

The solution I am proposing might sound familiar, using imports-loader and exports-loader in combination with the Provide plugin to override native API’s.

Another solution is to conditionally load polyfills depending on what the browser supports, but that is for another time.

The approach we are taking won’t pollute the global namespace, whenever a reference to a module is found Webpack will automatically rewrite its reference to be the supplied module.

As you can see, we are polyfilling Promise, which means if you open up your application in a browser without native Promise support, typing Promise into the console will result in undefined. However, any code in your app referencing Promise will get the polyfill version.

Install the dependencies

npm install --save-dev exports-loader imports-loader
npm install --save bluebird

Configuring your Webpack

Once you have your required dependencies, inside of the plugins section of your webpack.config.js file:

plugins: [
    new webpack.ProvidePlugin({
        Promise: "imports-loader?this=>global!exports-loader?global.Promise!bluebird"

Now, whenever your application requests to use a Promise, you’ll get Bluebird instead. Swell.

Bonus: jQuery and whatwg-fetch polyfill

If you are using jQuery in your application, then using the ProvidePlugin you can also shim certain variables to modules.

plugins: [
    new webpack.ProvidePlugin({
        Promise: "imports-loader?this=>global!exports-loader?global.Promise!bluebird",
        fetch: "imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch",
        $: "jquery",
        jQuery: "jquery"

Purchase Aurelia for Real World Applications over at Leanpub now




One thought on “Polyfilling Promises Using Bluebird in Webpack 2

Leave a Reply

Your email address will not be published. Required fields are marked *