Core-js: Recomended way to use with Webpack 3

Created on 3 Nov 2017  路  2Comments  路  Source: zloirock/core-js

What is the recomended way to use with core-js with WebPack 3? I was thinking of using the Webpack ProvidePlugin plugin like so:

new webpack.ProvidePlugin({
    Promise: 'core-js/fn/promise'
}),

However, I'm not sure how to then also include all of the other polyfills like String.prototype.includes. Is there a list of them available that I can add to the ProvidePlugin or am I going about this in the wrong way? Ideally you'd only want to import something if you use it, the alternative is to just import 'core-js'.

question

Most helpful comment

@RehanSaeed

We create a polyfills.js like the following:

import 'core-js/fn/promise'
import 'core-js/fn/string/includes'

and add it our webpack entry point:

entry: ['./polyfills.js', './index.js']

You could also do:

entry: ['core-js/fn/promise', 'core-js/fn/string/includes', './app/js']

the core-js/fn/* modules patch the global namespace (for example core-js/fn/promise changes the global Promise), so there is no need to use the ProvidePlugin plugin.

All 2 comments

@RehanSaeed

We create a polyfills.js like the following:

import 'core-js/fn/promise'
import 'core-js/fn/string/includes'

and add it our webpack entry point:

entry: ['./polyfills.js', './index.js']

You could also do:

entry: ['core-js/fn/promise', 'core-js/fn/string/includes', './app/js']

the core-js/fn/* modules patch the global namespace (for example core-js/fn/promise changes the global Promise), so there is no need to use the ProvidePlugin plugin.

@RehanSaeed I think that solution like ProvidePlugin are not useful because that can determinate only little part of features. I prefer way like proposes @michael-wolfenden. Also, you can try babel-preset-env with useBuiltIns: 'usage' option which injects required part of core-js.

Was this page helpful?
0 / 5 - 0 ratings