npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autopolyfiller

v1.6.1

Published

Precise polyfills. Like Autoprefixer, but for JavaScript polyfills

Downloads

189

Readme

Autopolyfiller — Precise polyfills

NPM Version Build Status Coverage Status Code Climate Dependency Status

This is like Autoprefixer, but for JavaScript polyfills. It scans your code and applies only required polyfills. Live example.

Assume you code is Object.keys(window). Object.keys polyfill is required to run it in any browser (include IE7). On the other hand this code can be executed on iOS 7 Safari without any polyfills. AutoPolyfiller knows about ES5 and ES6 features and their support in browsers. It can help you to write cutting-edge JavaScript without thinking about ES shims and shivs.

How it works. Step by step:

  1. Using AST matchers, it scans your code and finds all polyfills
  2. If target browsers are specified, then it reduces the list of polyfills according to the "feature database"
  3. It generates polyfills code, using polyfills database, which precisely fixes only required features

Limitations:

  • Right now it supports only safe and cross-browser polyfills from ES5, but you can add your own (see examples).
  • It can have a false-positives for some cases. For instance, autopolyfiller thinks that $('div').map() is call of Array.prototype.map. But you can exclude false-positives (see examples).

It will not work if:

  • You are evaling code with polyfills. Eg eval('Object.keys(this)')
  • You are doing something odd. Eg Object['k' + 'eys']()

Installation

autopolyfiller can be installed using npm:

npm install autopolyfiller

CLI Example

$ autopolyfiller lib/**/*.js -b "Explorer 7, Chrome >= 10"
$ cat lib/*.js | autopolyfiller

Grunt, Gulp & Enb tasks

Example

// Polyfills + Code
require('autopolyfiller')().add(code) + code;

List of polyfills without browsers filtering

var autopolyfiller = require('autopolyfiller');

autopolyfiller()
.add('"".trim();')
.polyfills;
// ['String.prototype.trim']

Filtering using Autoprefixer-style browser matchers

var autopolyfiller = require('autopolyfiller');

autopolyfiller('IE 11', 'Chrome >= 31')
.add('"".trim();Object.create();new Promise()')
.polyfills;
// ['Promise']

Default autoprefixer browsers

var autopolyfiller = require('autopolyfiller'),
    autoprefixer = require('autopolyfiller');

autopolyfiller(autoprefixer.default)
.add('new Promise();')
.polyfills;
// ['Promise']

Excluding/including polyfills

var autopolyfiller = require('autopolyfiller');

autopolyfiller()
.exclude(['Promise'])
.include(['String.prototype.trim'])
// All Array polyfills
.include(['Array.*'])
.add('new My.Promise();')
.polyfills;
// ['String.prototype.trim']

Using custom parser

var autopolyfiller = require('autopolyfiller');

autopolyfiller()
.withParser('[email protected]', {ecmaVersion: 6})
.add('array.map(x => x * x)')
.polyfills;
// ['Array.prototype.map']

Adding your own polyfills

var query = require('grasp-equery').query;
var autopolyfiller = require('autopolyfiller');

autopolyfiller.use({
    // AST tree pattern matching
    // It may "grep" multiply polyfills
    test: function (ast) {
        return query('Object.newFeature(_$)', ast).length > 0 ? ['Object.newFeature'] : [];
    },

    // Your polyfills code
    polyfill: {
        'Object.newFeature': 'Object.newFeature = function () {};'
    },

    // This list means "apply this feature to the <list of browsers>"
    // For more examples see https://github.com/jonathantneal/polyfill/blob/master/agent.js.json
    support: {
        // For chrome 29 only apply Object.newFeature polyfill
        'Chrome': [{
            only: '29',
            fill: 'Object.newFeature'
        }]
    },

    // This is optional. By default autopolyfiller will use
    // polyfill's name to generate condition's code:
    wrapper: {
        'Object.newFeature': {
            'before': 'if (!("newFeature" in Object)) {',
            'after': '}'
        }
    }
});

autopolyfiller()
.add('Object.create();Object.newFeature();')
.polyfills;
// ['Object.create', 'Object.newFeature']

autopolyfiller()
.add('Object.newFeature();')
.toString();
// if (!("newFeature" in Object)) {
// Object.newFeature = function () {};
// }

autopolyfiller('Chrome >= 20')
.add('Object.create();Object.newFeature();')
.polyfills;
// []

Handling polyfills issues

Right now Autopolyfiller aggreagates existing sources of polyfills. If you have any issues related to a polyfill code itself, please, add an issue or a pull request to the jonathantneal/polyfill.

Here is how to temporary workaround, while your issue being resolved:

var autopolyfiller = require('autopolyfiller');

autopolyfiller.use({
    polyfill: {
        'Function.prototype.bind': 'fixed code (!Function.prototype.bind)'
    }
});