forked from browserslist/browserslist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.js
410 lines (369 loc) · 11.1 KB
/
node.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
var feature = require('caniuse-lite/dist/unpacker/feature').default
var region = require('caniuse-lite/dist/unpacker/region').default
var path = require('path')
var fs = require('fs')
var BrowserslistError = require('./error')
var IS_SECTION = /^\s*\[(.+)]\s*$/
var CONFIG_PATTERN = /^browserslist-config-/
var SCOPED_CONFIG__PATTERN = /@[^/]+(?:\/[^/]+)?\/browserslist-config(?:-|$|\/)/
var TIME_TO_UPDATE_CANIUSE = 6 * 30 * 24 * 60 * 60 * 1000
var FORMAT =
'Browserslist config should be a string or an array ' +
'of strings with browser queries'
var dataTimeChecked = false
var filenessCache = {}
var configCache = {}
function checkExtend(name) {
var use = ' Use `dangerousExtend` option to disable.'
if (!CONFIG_PATTERN.test(name) && !SCOPED_CONFIG__PATTERN.test(name)) {
throw new BrowserslistError(
'Browserslist config needs `browserslist-config-` prefix. ' + use
)
}
if (name.replace(/^@[^/]+\//, '').indexOf('.') !== -1) {
throw new BrowserslistError(
'`.` not allowed in Browserslist config name. ' + use
)
}
if (name.indexOf('node_modules') !== -1) {
throw new BrowserslistError(
'`node_modules` not allowed in Browserslist config.' + use
)
}
}
function isFile(file) {
if (file in filenessCache) {
return filenessCache[file]
}
var result = fs.existsSync(file) && fs.statSync(file).isFile()
if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
filenessCache[file] = result
}
return result
}
function eachParent(file, callback) {
var dir = isFile(file) ? path.dirname(file) : file
var loc = path.resolve(dir)
do {
var result = callback(loc)
if (typeof result !== 'undefined') return result
} while (loc !== (loc = path.dirname(loc)))
return undefined
}
function check(section) {
if (Array.isArray(section)) {
for (var i = 0; i < section.length; i++) {
if (typeof section[i] !== 'string') {
throw new BrowserslistError(FORMAT)
}
}
} else if (typeof section !== 'string') {
throw new BrowserslistError(FORMAT)
}
}
function pickEnv(config, opts) {
if (typeof config !== 'object') return config
var name
if (typeof opts.env === 'string') {
name = opts.env
} else if (process.env.BROWSERSLIST_ENV) {
name = process.env.BROWSERSLIST_ENV
} else if (process.env.NODE_ENV) {
name = process.env.NODE_ENV
} else {
name = 'production'
}
if (opts.throwOnMissing) {
if (name && name !== 'defaults' && !config[name]) {
throw new BrowserslistError(
'Missing config for Browserslist environment `' + name + '`'
)
}
}
return config[name] || config.defaults
}
function parsePackage(file) {
var config = JSON.parse(
fs
.readFileSync(file)
.toString()
.replace(/^\uFEFF/m, '')
)
if (config.browserlist && !config.browserslist) {
throw new BrowserslistError(
'`browserlist` key instead of `browserslist` in ' + file
)
}
var list = config.browserslist
if (Array.isArray(list) || typeof list === 'string') {
list = { defaults: list }
}
for (var i in list) {
check(list[i])
}
return list
}
function latestReleaseTime(agents) {
var latest = 0
for (var name in agents) {
var dates = agents[name].releaseDate || {}
for (var key in dates) {
if (latest < dates[key]) {
latest = dates[key]
}
}
}
return latest * 1000
}
function normalizeStats(data, stats) {
if (!data) {
data = {}
}
if (stats && 'dataByBrowser' in stats) {
stats = stats.dataByBrowser
}
if (typeof stats !== 'object') return undefined
var normalized = {}
for (var i in stats) {
var versions = Object.keys(stats[i])
if (versions.length === 1 && data[i] && data[i].versions.length === 1) {
var normal = data[i].versions[0]
normalized[i] = {}
normalized[i][normal] = stats[i][versions[0]]
} else {
normalized[i] = stats[i]
}
}
return normalized
}
function normalizeUsageData(usageData, data) {
for (var browser in usageData) {
var browserUsage = usageData[browser]
// eslint-disable-next-line max-len
// https://github.com/browserslist/browserslist/issues/431#issuecomment-565230615
// caniuse-db returns { 0: "percentage" } for `and_*` regional stats
if ('0' in browserUsage) {
var versions = data[browser].versions
browserUsage[versions[versions.length - 1]] = browserUsage[0]
delete browserUsage[0]
}
}
}
module.exports = {
loadQueries: function loadQueries(ctx, name) {
if (!ctx.dangerousExtend && !process.env.BROWSERSLIST_DANGEROUS_EXTEND) {
checkExtend(name)
}
var queries = require(require.resolve(name, { paths: ['.', ctx.path] }))
if (queries) {
if (Array.isArray(queries)) {
return queries
} else if (typeof queries === 'object') {
if (!queries.defaults) queries.defaults = []
return pickEnv(queries, ctx, name)
}
}
throw new BrowserslistError(
'`' +
name +
'` config exports not an array of queries' +
' or an object of envs'
)
},
loadStat: function loadStat(ctx, name, data) {
if (!ctx.dangerousExtend && !process.env.BROWSERSLIST_DANGEROUS_EXTEND) {
checkExtend(name)
}
var stats = require(require.resolve(
path.join(name, 'browserslist-stats.json'),
{ paths: ['.'] }
))
return normalizeStats(data, stats)
},
getStat: function getStat(opts, data) {
var stats
if (opts.stats) {
stats = opts.stats
} else if (process.env.BROWSERSLIST_STATS) {
stats = process.env.BROWSERSLIST_STATS
} else if (opts.path && path.resolve && fs.existsSync) {
stats = eachParent(opts.path, function (dir) {
var file = path.join(dir, 'browserslist-stats.json')
return isFile(file) ? file : undefined
})
}
if (typeof stats === 'string') {
try {
stats = JSON.parse(fs.readFileSync(stats))
} catch (e) {
throw new BrowserslistError("Can't read " + stats)
}
}
return normalizeStats(data, stats)
},
loadConfig: function loadConfig(opts) {
if (process.env.BROWSERSLIST) {
return process.env.BROWSERSLIST
} else if (opts.config || process.env.BROWSERSLIST_CONFIG) {
var file = opts.config || process.env.BROWSERSLIST_CONFIG
if (path.basename(file) === 'package.json') {
return pickEnv(parsePackage(file), opts)
} else {
return pickEnv(module.exports.readConfig(file), opts)
}
} else if (opts.path) {
return pickEnv(module.exports.findConfig(opts.path), opts)
} else {
return undefined
}
},
loadCountry: function loadCountry(usage, country, data) {
var code = country.replace(/[^\w-]/g, '')
if (!usage[code]) {
var compressed
try {
compressed = require('caniuse-lite/data/regions/' + code + '.js')
} catch (e) {
throw new BrowserslistError('Unknown region name `' + code + '`.')
}
var usageData = region(compressed)
normalizeUsageData(usageData, data)
usage[country] = {}
for (var i in usageData) {
for (var j in usageData[i]) {
usage[country][i + ' ' + j] = usageData[i][j]
}
}
}
},
loadFeature: function loadFeature(features, name) {
name = name.replace(/[^\w-]/g, '')
if (features[name]) return
var compressed
try {
compressed = require('caniuse-lite/data/features/' + name + '.js')
} catch (e) {
throw new BrowserslistError('Unknown feature name `' + name + '`.')
}
var stats = feature(compressed).stats
features[name] = {}
for (var i in stats) {
features[name][i] = {}
for (var j in stats[i]) {
features[name][i][j] = stats[i][j]
}
}
},
parseConfig: function parseConfig(string) {
var result = { defaults: [] }
var sections = ['defaults']
string
.toString()
.replace(/#[^\n]*/g, '')
.split(/\n|,/)
.map(function (line) {
return line.trim()
})
.filter(function (line) {
return line !== ''
})
.forEach(function (line) {
if (IS_SECTION.test(line)) {
sections = line.match(IS_SECTION)[1].trim().split(' ')
sections.forEach(function (section) {
if (result[section]) {
throw new BrowserslistError(
'Duplicate section ' + section + ' in Browserslist config'
)
}
result[section] = []
})
} else {
sections.forEach(function (section) {
result[section].push(line)
})
}
})
return result
},
readConfig: function readConfig(file) {
if (!isFile(file)) {
throw new BrowserslistError("Can't read " + file + ' config')
}
return module.exports.parseConfig(fs.readFileSync(file))
},
findConfig: function findConfig(from) {
from = path.resolve(from)
var passed = []
var resolved = eachParent(from, function (dir) {
if (dir in configCache) {
return configCache[dir]
}
passed.push(dir)
var config = path.join(dir, 'browserslist')
var pkg = path.join(dir, 'package.json')
var rc = path.join(dir, '.browserslistrc')
var pkgBrowserslist
if (isFile(pkg)) {
try {
pkgBrowserslist = parsePackage(pkg)
} catch (e) {
if (e.name === 'BrowserslistError') throw e
console.warn(
'[Browserslist] Could not parse ' + pkg + '. Ignoring it.'
)
}
}
if (isFile(config) && pkgBrowserslist) {
throw new BrowserslistError(
dir + ' contains both browserslist and package.json with browsers'
)
} else if (isFile(rc) && pkgBrowserslist) {
throw new BrowserslistError(
dir + ' contains both .browserslistrc and package.json with browsers'
)
} else if (isFile(config) && isFile(rc)) {
throw new BrowserslistError(
dir + ' contains both .browserslistrc and browserslist'
)
} else if (isFile(config)) {
return module.exports.readConfig(config)
} else if (isFile(rc)) {
return module.exports.readConfig(rc)
} else {
return pkgBrowserslist
}
})
if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
passed.forEach(function (dir) {
configCache[dir] = resolved
})
}
return resolved
},
clearCaches: function clearCaches() {
dataTimeChecked = false
filenessCache = {}
configCache = {}
this.cache = {}
},
oldDataWarning: function oldDataWarning(agentsObj) {
if (dataTimeChecked) return
dataTimeChecked = true
if (process.env.BROWSERSLIST_IGNORE_OLD_DATA) return
var latest = latestReleaseTime(agentsObj)
var halfYearAgo = Date.now() - TIME_TO_UPDATE_CANIUSE
if (latest !== 0 && latest < halfYearAgo) {
console.warn(
'Browserslist: caniuse-lite is outdated. Please run:\n' +
' npx update-browserslist-db@latest\n' +
' Why you should do it regularly: ' +
'https://github.com/browserslist/update-db#readme'
)
}
},
currentNode: function currentNode() {
return 'node ' + process.versions.node
},
env: process.env
}