-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
245 lines (210 loc) · 6.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var acorn = require('acorn');
var assert = require('assert');
var scan = require('./lib/polyfill-scan');
var reduce = require('./lib/polyfill-reduce');
var wrap = require('./lib/polyfill-wrap');
var minimatch = require('minimatch');
var code = require('./lib/polyfill-code');
var stablePolyfills = require('autopolyfiller-stable');
var polyfillNames = Object.keys(stablePolyfills.polyfill);
/**
*
* @param {Object} options
* @param {String[]} [options.browsers] Autoprefixer style list of browsers
* @constructor
*
* @example
*
* new AutoPolyFiller({
* browsers: ['IE 11', 'Chrome >= 31']
* })
* .withParser(require('esprima-fb'))
* .exclude(['Object.create'])
* .include(['Array.prototype.map'])
* .add('"".trim();Object.create();new Promise();')
* .polyfills;
* // ['Promise', 'Array.prototype.map']
*/
function AutoPolyFiller(options) {
this.browsers = options.browsers;
this.polyfills = [];
this.excluedPolyfills = [];
this.parserOptions = void 0;
this.parser = acorn;
}
AutoPolyFiller.prototype = {
/**
* Scans `code` for polyfills
*
* @param {String} code
* @returns {String[]}
* @private
*/
_scan: function (code) {
var polyfills = scan(code, this.parser, this.parserOptions);
// Do not reduce if no browsers
if (this.browsers && this.browsers.length === 0) {
return polyfills;
}
return reduce(polyfills, this.browsers);
},
/**
* Scans for polyfills in code of each polyfills
*
* @param {String[]} polyfills list of polyfills names
* @returns {String[]} list contains non unique polyfills
* @private
*/
_scanForPolyfillsOfPolyfills: function (polyfills) {
var hasIterated = {};
var iteratePolyfills = function (polyfills, polyfillName) {
// Already scanned this polyfill
if (hasIterated.hasOwnProperty(polyfillName)) {
return polyfills;
}
hasIterated[polyfillName] = true;
polyfills = polyfills.concat(this._scan(code(polyfillName)));
return polyfills.concat(polyfills.reduce(iteratePolyfills, []));
}.bind(this);
return polyfills.reduce(iteratePolyfills, []);
},
/**
* Inspects given `code` for polyfills
* @param {String} code javascipt code
* @returns {AutoPolyFiller}
*/
add: function (code) {
var polyfills = this._scan(code);
var polyfillsOfPolyfills = this._scanForPolyfillsOfPolyfills(polyfills);
this.include(polyfills.concat(polyfillsOfPolyfills));
return this;
},
/**
*
* @returns {string} code that polyfills all listed functions
*/
toString: function () {
return this.polyfills.map(function (polyfillName) {
var polyfillCode = code(polyfillName);
return wrap(polyfillCode, polyfillName);
}).join('');
},
/**
* Checks if `polyfill` is not in a `excluedPolyfills` list
*
* @param {String} polyfill
* @returns {Boolean}
* @private
*/
_isPolyfillIncluded: function (polyfill) {
return this.excluedPolyfills.indexOf(polyfill) === -1;
},
/**
* Adds `polyfills` to the list of required polyfills
*
* @param {String[]} polyfills
* @returns {AutoPolyFiller}
*/
include: function (polyfills) {
this.polyfills = this.polyfills
.concat(polyfills)
// If any of the patterns contain '*', add all of the matching
// polyfills
.reduce(function (polyfills, polyfill) {
if (polyfill.indexOf('*') > -1) {
var matches = polyfillNames.filter(function (name) {
return minimatch(name, polyfill);
});
return polyfills.concat(matches);
}
polyfills.push(polyfill);
return polyfills;
}, [])
// Filter ignored polyfills
.filter(this._isPolyfillIncluded.bind(this))
// Make unique polyfills
.reduce(function (polyfills, polyfill) {
if (polyfills.indexOf(polyfill) === -1) {
polyfills.push(polyfill);
}
return polyfills;
}, []);
return this;
},
/**
* Ignores `polyfills`, excluded their code from result
*
* @param {String[]} polyfills
* @returns {AutoPolyFiller}
*/
exclude: function (polyfills) {
this.excluedPolyfills.push.apply(this.excluedPolyfills, polyfills);
// Filter ignored polyfills
this.polyfills = this.polyfills
.filter(this._isPolyfillIncluded.bind(this));
return this;
},
/**
* Overrides default parser
*
* @param {Object} parser
* @param {Object} parser.parse
* @param {Object} [parserOptions]
* @returns {AutoPolyFiller}
*/
withParser: function (parser, parserOptions) {
this.parserOptions = parserOptions;
if (parser) {
assert(typeof parser.parse === 'function', 'parser should have a `parse` method');
this.parser = parser;
}
return this;
}
};
/**
* Polyfill interface
*
* @example
*
* polyfiller('IE 11', 'Chrome >= 31')
* .add('"".trim();Object.create();new Promise()')
* .polyfills;
* // ['Promise']
*/
function create() {
var browsers = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
if (browsers.length === 1 && browsers[0] instanceof Array) {
browsers = browsers[0];
}
return new AutoPolyFiller({
browsers: browsers
});
}
/**
* Customizes polyfills
*
* @param {Object} options
* @param {Function} [options.test]
* @param {Object} [options.support]
* @param {Object} [options.polyfill]
* @param {Object} [options.wrapper]
*/
function use(options) {
if (options.test) {
scan.use({
test: options.test
});
}
if (options.support) {
reduce.support(options.support);
}
if (options.polyfill) {
code.addSource(options.polyfill);
}
if (options.wrapper) {
wrap.addWrapper(options.wrapper);
}
}
use(stablePolyfills);
module.exports = create;
module.exports.use = use;