1. ProvidePlugin()
Looks like a commonly used approach. There is a gist about it, showcasing how to include whatwg-fetch polyfill into a Webpack build. Lots of answers on StackOverflow use it here and here.
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery',
'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
})
Pros
- It works. (please update this list if I am missing something)
Cons
- Need to keep track of global libraries in the Webpack config.
2. entry: [...]
I was a little surprised by this approach when I discovered it in this gist but it works just as well.
entry: [
'babel-polyfill',
'whatwg-fetch',
'jquery',
'webpack-hot-middleware/client',
path.join(process.cwd(), 'app/app.js')
],
Pros
- It works.
- I can drop
ProvidePlugin()
entirely.
Cons
- Need to keep track of global libraries in the Webpack config.
3. Top-level import
This one is very straightforward, see this app.js example. This file is an entry-point to a React application.
/**
* app.js
*/
import 'whatwg-fetch';
import 'babel-polyfill';
import 'jquery';
Pros
- It works just as well.
- Easy add/remove. No need to touch the Webpack config.
Cons
- Doesn't look like this approach alone will work with jQuery plugins, e.g. bootstrap.js.
Observation: Between all three approaches, I haven't noticed any changes in the bundle size.
Is there one recommended way of handling global libraries with Webpack (and React)? Would any of these solutions cause a problem down the road with server-side rendering?
Thanks!