Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
autopolyfiller
Advanced tools
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:
Limitations:
$('div').map()
is call of Array.prototype.map
. But you can exclude false-positives (see examples).It will not work if:
eval
ing code with polyfills. Eg eval('Object.keys(this)')
Object['k' + 'eys']()
autopolyfiller
can be installed using npm
:
npm install autopolyfiller
$ autopolyfiller lib/**/*.js -b "Explorer 7, Chrome >= 10"
$ cat lib/*.js | autopolyfiller
// 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'])
.add('new My.Promise();')
.polyfills;
// ['String.prototype.trim']
Using custom parser
var autopolyfiller = require('autopolyfiller');
autopolyfiller()
.withParser('acorn@0.11.0', {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;
// []
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)'
}
});
FAQs
Precise polyfills. Like Autoprefixer, but for JavaScript polyfills
The npm package autopolyfiller receives a total of 34 weekly downloads. As such, autopolyfiller popularity was classified as not popular.
We found that autopolyfiller demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.