Polyfill Bundler Service

1.0.126 · active · verified Tue Apr 21

Polyfill Bundler is a JavaScript utility package that enables the creation and hosting of a dynamic polyfill service. This service intelligently delivers custom polyfill bundles to clients based on their user-agent string and explicitly requested features, ensuring that browsers only receive the JavaScript code necessary to support modern web standards. This approach contrasts with static bundling, which often includes all polyfills regardless of need, leading to unnecessary payload size. The package is currently at version 1.0.126, suggesting ongoing development with frequent updates to its minor and patch versions. It is primarily designed for developers who wish to host their own polyfill service to optimize client-side performance and ensure broad browser compatibility without over-bundling. The core differentiator is its on-demand, user-agent-aware polyfill generation, reducing load times for modern browsers while providing full functionality for older ones. [17, 21]

Common errors

Warnings

Install

Quickstart

Demonstrates the client-side consumption of the `polyfill-bundler` service via a script tag, along with a conceptual Node.js server illustrating how such a service would dynamically generate polyfill bundles based on requested features. This quickstart highlights the primary use case of `polyfill-bundler` as a self-hostable service for optimizing polyfill delivery. [17]

<!-- Include this in your HTML to use the self-hosted polyfill service -->
<script src="https://polyfill.your.domain/polyfill.js?features=AbortController,Array.from,Promise.prototype.finally"></script>

// Example of how to start a very basic (not production-ready) Node.js server
// that could theoretically serve polyfills, assuming polyfill-bundler provides
// an API to generate them. (Note: The exact API for 'polyfill-bundler' as a library
// to generate bundles programmatically is not directly exposed in the README snippet;
// this is a conceptual server illustrating the client-side usage.)

const http = require('http');
const url = require('url');

// In a real application, 'polyfill-bundler' would be used here to generate the bundle.
// For this example, we'll simulate a response.
function generatePolyfillBundle(features) {
  console.log(`Generating polyfill for features: ${features.join(', ')}`);
  // In a real scenario, this would use the polyfill-bundler logic
  // to detect user-agent and generate a minified, targeted polyfill string.
  // For demonstration, a simple placeholder.
  let bundle = '';
  if (features.includes('AbortController')) bundle += '/* AbortController polyfill */\nself.AbortController = self.AbortController || function AbortController() { this.signal = { aborted: false }; };\n';
  if (features.includes('Array.from')) bundle += '/* Array.from polyfill */\nif (!Array.from) { Array.from = (iterable) => [...iterable]; }\n';
  if (features.includes('Promise.prototype.finally')) bundle += '/* Promise.finally polyfill */\nif (!Promise.prototype.finally) { Promise.prototype.finally = function(cb) { const P = this.constructor || Promise; return this.then(val => P.resolve(cb()).then(() => val), err => P.resolve(cb()).then(() => { throw err; })); }; }\n';
  return bundle;
}

const server = http.createServer((req, res) => {
  const parsedUrl = url.parse(req.url, true);
  if (parsedUrl.pathname === '/polyfill.js') {
    const featuresParam = parsedUrl.query.features;
    const features = featuresParam ? featuresParam.split(',') : [];

    const polyfillCode = generatePolyfillBundle(features);

    res.writeHead(200, { 'Content-Type': 'application/javascript' });
    res.end(polyfillCode);
  } else {
    res.writeHead(404, { 'Content-Type': 'text/plain' });
    res.end('Not Found');
  }
});

const PORT = process.env.PORT ?? 3000;
server.listen(PORT, () => {
  console.log(`Polyfill service running at http://localhost:${PORT}`);
  console.log('Access via: http://localhost:3000/polyfill.js?features=AbortController,Array.from');
});

view raw JSON →