Polyfills
Create polyfill builds based on the client's browser and serve only what's needed.
This allows you to write modern JavaScript without worrying too much
(you should still do due diligence) about browser support as well as
not penalizing modern browsers with unnecessary polyfills.
See https://polyfills.github.io for more details.
Description
This library is merely the "logic" and does not handle any HTTP serving.
It essentially does the following:
- Parses user agent strings for
<family> <major>.<minor>.<version>
and creates polyfill bundles based on these variables. - Caches builds locally.
- Creates minified and gzipped builds.
- Returns metadata for the
Content-Encoding
, Content-Length
and ETag
headers. - Allows you to choose between which build you'd like and whether to read it or stream it.
It also stores nothing in memory, making it suitable for production usage within existing node apps.
Installation
npm install polyfills
Usage
var polyfill = Polyfills([options])
var polyfill = require('polyfills')()
Return a new instance of polyfill
based on options
.
include
- which polyfills to include.
This is an inclusive list.
The names are included in polyfills/db.exclude
- conversely, you can exclude specific polyfills.
polyfill.clean()
Clean all the bundles from the cache.
polyfill(useragent).then( data => )
Build and cache the bundle. Returns data with:
name
- the name of the buildhash
- a sha256
sha sum of the JS file for ETag
headerspolyfills[]
- an array of all the polyfills' names usedlength[extension]
- the byte size of each build for Content-Length
headers
The possible extensions are:
.json
- where the metadata is stored.js
.js.gz
.min.js
.min.js.gz
polyfill.read(name, ext, [encoding]).then( buf => )
Read a bundle.
var stream = polyfill.stream(name, ext)
Stream a bundle.
var filename = polyfill.pathOf(name, ext)
Get the filename of a bundle.
Adding polyfills
Checkout polyfills/db.