Giter Club home page Giter Club logo

polyfill-io-loader's Introduction

Polyfill.io loader for Webpack

Install

npm i polyfill-io-loader --save-dev

Features

  1. Lazy load - evergreen browsers that support all the given features will not contact polyfill.io at all - it will just initialise the main app. This avoids an unnecessary http round trip.

  2. Retrieve minimal polyfills - it will only pull the missing functionality your browser needs and not all the polyfills you list.

  3. Load from custom self hosted url/cdn - if your self hosting your own polyfill.io then you can point it towards your CDN url.

Usage

import loadPolyfills from 'polyfill-io-loader!?Promise,NodeList.prototype.forEach,Object.assign';

loadPolyfills(function() {
    // All polyfills have been loaded.
    // Initialise your app here.
    //
    // For example:
    // ReactDOM.render(<ClientRoot />, document.getElementById('root'));
});

Load from custom CDN/url:

import loadPolyfills from 'polyfill-io-loader!?Promise,url=http://my-custom-cdn.com';

How does it work?

When the loader runs, it pulls the minimal js detections code for each of the polyfills you list in the import. At runtime of your app, it will check all the detections then make a request to polyfill.io with all the missing functionality:

So for example:

import loadPolyfills from 'polyfill-io-loader!?Promise,fetch,Object.assign';

If the browser supports Promise but not fetch or Object.assign, the following request will be made to polyfill.io:

https://polyfill.io/v3/polyfill.min.js?features=fetch,Object.assign&flags=always

How to know which polyfills to use?

  1. create your production client bundle (something like: npm run build)
  2. run npx create-polyfill-service-url analyse --file dist/main.5cac0bed.js <- replace the filename with the one provided by the bundler in the previous step. This will analyze your production bundle and create a list of polyfills according to your browserslist settings.
  3. if the list of polyfill seems long, check the log of the create-polyfill-service-url. If you see something like 'we do not know if and_qq 12.12.0 supports fetch', update your browserlist to exclude those browsers.

Contributing

This library aims to be lightweight and minimal, but if you feel a new option or functionality is required, please create an issue for feedback.

polyfill-io-loader's People

Contributors

garygreen avatar maapteh avatar zsolt-dev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.