gitmyhub

polyfill-library

JavaScript ★ 674 updated 2y ago

NodeJS module to create polyfill bundles tailored to individual user-agents.

Polyfill-library is a Node.js module (a reusable package for JavaScript server-side code) that generates custom polyfill bundles — a polyfill being a piece of code that adds a modern browser feature to older browsers that don't support it natively. Instead of serving the same set of polyfills to every visitor, this library inspects the browser's "user agent" string (a piece of text browsers send identifying themselves) and generates only the polyfills that particular browser actually needs.

This approach saves bandwidth and processing time: a modern browser gets little or nothing extra, while an older browser gets exactly what it's missing. You specify which features you want supported, pass in the user agent string, and the library returns a JavaScript bundle tailored to that browser.

The core function is getPolyfillString, which accepts options like which features to polyfill, whether to minify (compress) the output, and what to do with unknown or unrecognized browsers. It returns the polyfill code as either a text string or a stream for efficiency with large bundles.

You would use this when building a web server that serves JavaScript to many different visitors with varying browsers and wants to support modern JavaScript features without sending unnecessary code. A typical use case is middleware that intercepts each page request, generates the right polyfill bundle for that visitor's browser, and serves it. The module also includes deployment notes for running in serverless environments like AWS Lambda, where extra setup is needed to ensure the polyfill files are packaged correctly. It is written in JavaScript and available via npm.