Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to load polyfills and shims with Browserify

I'm building a web app and I'm getting to know and love Browserify. One thing has bugged me though.

I'm using some ES6 features that need to be shimmed/polyfilled in older browsers such as es6-promise and object-assign (packages on npm).

Currently I'm just loading them into each module that needs them:

var assign  = require('object-assign');
var Promise = require('es6-promise');

I know this is definitely not the way to go. It is hard to maintain and I would like to use the ES6 features transparently instead of having to "depend" on them via requires.

What's the definitive way to load shims like these? I've seen several examples around the internet but they're all different. I could:

  • load them externally:

    var bundle = browserify(); 
    bundle.require('s6-promise');
    // or should I use it bundle.add to make sure the code is runned???
    

    The problem I have here is that I don't know which order the modules will be loaded in in the browser. So the polyfilling might not have happened yet at call sites that need the polyfilled functionality.

    This has the additional downside that backend code cannot benefit from these polyfills (unless I'm missing something).

  • use browserify-shim or something similar. I don't really see how this would work for ES6 features.

  • manually set up the polyfilling:

    Object.assign = require('object-assign');
    
like image 578
romeovs Avatar asked Jan 09 '15 23:01

romeovs


People also ask

Where do you put Polyfills?

After you eject your React/Redux app (created using create-react-app ), you should find the polyfills. js at /config folder.

How do you apply Polyfills?

If you want to include a polyfill, you need to: - add a fallback 'resolve. fallback: { "path": require. resolve("path-browserify") }' - install 'path-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve. fallback: { "path": false } @ ./src/config.

What are shims and Polyfills?

A shim is a piece of code used to correct the behavior of code that already exists, usually by adding new API that works around the problem. This differs from a polyfill, which implements a new API that is not supported by the stock browser as shipped.

What does Browserify?

Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node. js-style modules that compile for use in the browser.


1 Answers

Don't require polyfills in your modules, that's an anti-pattern. Your modules should assume that the runtime is patched (when needed), and that should be part of the contract. A good example of this is ReactJS, where they explicitly define the minimum requirement for the runtime so that the library can work: http://facebook.github.io/react/docs/working-with-the-browser.html#browser-support-and-polyfills

You could use a polyfill service (e.g.: https://cdn.polyfill.io/) to include an optimized script tag at the top of your page to guarantee that the runtime is patched correctly with the pieces you need, while modern browsers will not get penalized.

like image 92
caridy Avatar answered Nov 15 '22 18:11

caridy