You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`webpack` as a module bundler can understand modules written as ES2015 modules, CommonJS or AMD. But many times, while using third party libraries, we see that they expect dependencies which are global, AKA `$` for `jquery`. They might also be creating global variables which need to be exported. Here we will see different ways to help webpack understand these __broken modules__.
@@ -158,3 +164,82 @@ module.exports = {
158
164
## Node Built-Ins
159
165
160
166
Node built-ins, like `process`, can be polyfilled right directly from your configuration file without the use of any special loaders or plugins. See the [node configuration page](/configuration/node) for more information and examples.
167
+
168
+
169
+
## Loading polyfills on demand
170
+
171
+
It's common in web projects to include polyfills in the main bundle. This is not recommended because we are penalizing modern browsers users by making them download a bigger file with unneeded scripts.
172
+
173
+
The simplest way to mitigate this is by adding a separate entry point in your webpack config file including the polyfills your project needs.
174
+
175
+
```javascript
176
+
// webpack.config.js
177
+
module.exports= {
178
+
entry: {
179
+
polyfills: [
180
+
'babel-polyfill',
181
+
'whatwg-fetch'
182
+
],
183
+
main:'./src/index.js'
184
+
}
185
+
// ... rest of your webpack config
186
+
};
187
+
```
188
+
189
+
An alternative is to create a new entry file and manually import these packages.
190
+
191
+
```javascript
192
+
// src/polyfills.js
193
+
import'babel-polyfill';
194
+
import'whatwg-fetch';
195
+
```
196
+
197
+
```javascript
198
+
// webpack.config.js
199
+
module.exports= {
200
+
entry: {
201
+
polyfills:'./src/polyfills.js',
202
+
main:'./src/index.js'
203
+
}
204
+
// rest of your webpack config
205
+
};
206
+
```
207
+
208
+
In your html file you need to conditionally load the `polyfills.js` file before your bundle. How you make this decision depends on the technologies and browsers you need to support.
209
+
210
+
```html
211
+
<script>
212
+
var modernBrowser = (
213
+
'fetch'inwindow&&
214
+
'assign'inObject
215
+
);
216
+
217
+
var scripts = [ '/main.js' ];
218
+
219
+
if (!modernBrowser) {
220
+
scripts.unshift('/polyfills.js');
221
+
}
222
+
223
+
scripts.map(function(src) {
224
+
var scriptElement =document.createElement('script');
225
+
scriptElement.async=false;
226
+
scriptElement.src= src;
227
+
document.head.appendChild(scriptElement);
228
+
});
229
+
</script>
230
+
```
231
+
232
+
T> Any script added dynamically like in the example above will run as soon as it's parsed, but we need our polyfill to run before our bundle. This is why we are setting `async` to `false` for each script.
233
+
234
+
235
+
### Smaller babel polyfill
236
+
237
+
`babel-preset-env` uses [browserslist](https://github.com/ai/browserslist) to transpile only what is not supported in your browsers matrix. This preset comes with the `useBuiltIns` option _(false by default)_ which converts your global `babel-polyfill` import to a more granular feature by feature import pattern like:
0 commit comments