I\'m building a site with ES6 and Babel.
In a script file, I need to make an ajax call to a service on server. For that I\'m doing like this:
fetch(\
You can also use Webpack's ProvidePlugin with the imports-loader and exports-loader packages as described in this answer, which removes the need to import anything in your code:
config.plugins = [
new webpack.ProvidePlugin({
'fetch': 'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch'
})
];
At the time of writing there's a compatibility issue with the 3.0.0 version of whatwg-fetch
. The workaround is using the following:
new webpack.ProvidePlugin({
fetch: 'exports-loader?self.fetch!whatwg-fetch/dist/fetch.umd'
})