1 | var feature = require('caniuse-lite/dist/unpacker/feature').default
|
---|
2 | var region = require('caniuse-lite/dist/unpacker/region').default
|
---|
3 | var fs = require('fs')
|
---|
4 | var path = require('path')
|
---|
5 |
|
---|
6 | var BrowserslistError = require('./error')
|
---|
7 |
|
---|
8 | var IS_SECTION = /^\s*\[(.+)]\s*$/
|
---|
9 | var CONFIG_PATTERN = /^browserslist-config-/
|
---|
10 | var SCOPED_CONFIG__PATTERN = /@[^/]+(?:\/[^/]+)?\/browserslist-config(?:-|$|\/)/
|
---|
11 | var FORMAT =
|
---|
12 | 'Browserslist config should be a string or an array ' +
|
---|
13 | 'of strings with browser queries'
|
---|
14 |
|
---|
15 | var dataTimeChecked = false
|
---|
16 | var filenessCache = {}
|
---|
17 | var configCache = {}
|
---|
18 | function checkExtend(name) {
|
---|
19 | var use = ' Use `dangerousExtend` option to disable.'
|
---|
20 | if (!CONFIG_PATTERN.test(name) && !SCOPED_CONFIG__PATTERN.test(name)) {
|
---|
21 | throw new BrowserslistError(
|
---|
22 | 'Browserslist config needs `browserslist-config-` prefix. ' + use
|
---|
23 | )
|
---|
24 | }
|
---|
25 | if (name.replace(/^@[^/]+\//, '').indexOf('.') !== -1) {
|
---|
26 | throw new BrowserslistError(
|
---|
27 | '`.` not allowed in Browserslist config name. ' + use
|
---|
28 | )
|
---|
29 | }
|
---|
30 | if (name.indexOf('node_modules') !== -1) {
|
---|
31 | throw new BrowserslistError(
|
---|
32 | '`node_modules` not allowed in Browserslist config.' + use
|
---|
33 | )
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | function isFile(file) {
|
---|
38 | if (file in filenessCache) {
|
---|
39 | return filenessCache[file]
|
---|
40 | }
|
---|
41 | var result = fs.existsSync(file) && fs.statSync(file).isFile()
|
---|
42 | if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
|
---|
43 | filenessCache[file] = result
|
---|
44 | }
|
---|
45 | return result
|
---|
46 | }
|
---|
47 |
|
---|
48 | function eachParent(file, callback) {
|
---|
49 | var dir = isFile(file) ? path.dirname(file) : file
|
---|
50 | var loc = path.resolve(dir)
|
---|
51 | do {
|
---|
52 | if (!pathInRoot(loc)) break
|
---|
53 | var result = callback(loc)
|
---|
54 | if (typeof result !== 'undefined') return result
|
---|
55 | } while (loc !== (loc = path.dirname(loc)))
|
---|
56 | return undefined
|
---|
57 | }
|
---|
58 |
|
---|
59 | function pathInRoot(p) {
|
---|
60 | if (!process.env.BROWSERSLIST_ROOT_PATH) return true
|
---|
61 | var rootPath = path.resolve(process.env.BROWSERSLIST_ROOT_PATH)
|
---|
62 | if (path.relative(rootPath, p).substring(0, 2) === '..') {
|
---|
63 | return false
|
---|
64 | }
|
---|
65 | return true
|
---|
66 | }
|
---|
67 |
|
---|
68 | function check(section) {
|
---|
69 | if (Array.isArray(section)) {
|
---|
70 | for (var i = 0; i < section.length; i++) {
|
---|
71 | if (typeof section[i] !== 'string') {
|
---|
72 | throw new BrowserslistError(FORMAT)
|
---|
73 | }
|
---|
74 | }
|
---|
75 | } else if (typeof section !== 'string') {
|
---|
76 | throw new BrowserslistError(FORMAT)
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | function pickEnv(config, opts) {
|
---|
81 | if (typeof config !== 'object') return config
|
---|
82 |
|
---|
83 | var name
|
---|
84 | if (typeof opts.env === 'string') {
|
---|
85 | name = opts.env
|
---|
86 | } else if (process.env.BROWSERSLIST_ENV) {
|
---|
87 | name = process.env.BROWSERSLIST_ENV
|
---|
88 | } else if (process.env.NODE_ENV) {
|
---|
89 | name = process.env.NODE_ENV
|
---|
90 | } else {
|
---|
91 | name = 'production'
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (opts.throwOnMissing) {
|
---|
95 | if (name && name !== 'defaults' && !config[name]) {
|
---|
96 | throw new BrowserslistError(
|
---|
97 | 'Missing config for Browserslist environment `' + name + '`'
|
---|
98 | )
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return config[name] || config.defaults
|
---|
103 | }
|
---|
104 |
|
---|
105 | function parsePackage(file) {
|
---|
106 | var config = JSON.parse(
|
---|
107 | fs
|
---|
108 | .readFileSync(file)
|
---|
109 | .toString()
|
---|
110 | .replace(/^\uFEFF/m, '')
|
---|
111 | )
|
---|
112 | if (config.browserlist && !config.browserslist) {
|
---|
113 | throw new BrowserslistError(
|
---|
114 | '`browserlist` key instead of `browserslist` in ' + file
|
---|
115 | )
|
---|
116 | }
|
---|
117 | var list = config.browserslist
|
---|
118 | if (Array.isArray(list) || typeof list === 'string') {
|
---|
119 | list = { defaults: list }
|
---|
120 | }
|
---|
121 | for (var i in list) {
|
---|
122 | check(list[i])
|
---|
123 | }
|
---|
124 |
|
---|
125 | return list
|
---|
126 | }
|
---|
127 |
|
---|
128 | function parsePackageOrReadConfig(file) {
|
---|
129 | if (path.basename(file) === 'package.json') {
|
---|
130 | return parsePackage(file)
|
---|
131 | } else {
|
---|
132 | return module.exports.readConfig(file)
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | function latestReleaseTime(agents) {
|
---|
137 | var latest = 0
|
---|
138 | for (var name in agents) {
|
---|
139 | var dates = agents[name].releaseDate || {}
|
---|
140 | for (var key in dates) {
|
---|
141 | if (latest < dates[key]) {
|
---|
142 | latest = dates[key]
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | return latest * 1000
|
---|
147 | }
|
---|
148 |
|
---|
149 | function getMonthsPassed(date) {
|
---|
150 | var now = new Date()
|
---|
151 | var past = new Date(date)
|
---|
152 |
|
---|
153 | var years = now.getFullYear() - past.getFullYear()
|
---|
154 | var months = now.getMonth() - past.getMonth()
|
---|
155 |
|
---|
156 | return years * 12 + months
|
---|
157 | }
|
---|
158 |
|
---|
159 | function normalizeStats(data, stats) {
|
---|
160 | if (!data) {
|
---|
161 | data = {}
|
---|
162 | }
|
---|
163 | if (stats && 'dataByBrowser' in stats) {
|
---|
164 | stats = stats.dataByBrowser
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (typeof stats !== 'object') return undefined
|
---|
168 |
|
---|
169 | var normalized = {}
|
---|
170 | for (var i in stats) {
|
---|
171 | var versions = Object.keys(stats[i])
|
---|
172 | if (versions.length === 1 && data[i] && data[i].versions.length === 1) {
|
---|
173 | var normal = data[i].versions[0]
|
---|
174 | normalized[i] = {}
|
---|
175 | normalized[i][normal] = stats[i][versions[0]]
|
---|
176 | } else {
|
---|
177 | normalized[i] = stats[i]
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | return normalized
|
---|
182 | }
|
---|
183 |
|
---|
184 | function normalizeUsageData(usageData, data) {
|
---|
185 | for (var browser in usageData) {
|
---|
186 | var browserUsage = usageData[browser]
|
---|
187 | // https://github.com/browserslist/browserslist/issues/431#issuecomment-565230615
|
---|
188 | // caniuse-db returns { 0: "percentage" } for `and_*` regional stats
|
---|
189 | if ('0' in browserUsage) {
|
---|
190 | var versions = data[browser].versions
|
---|
191 | browserUsage[versions[versions.length - 1]] = browserUsage[0]
|
---|
192 | delete browserUsage[0]
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | module.exports = {
|
---|
198 | loadQueries: function loadQueries(ctx, name) {
|
---|
199 | if (!ctx.dangerousExtend && !process.env.BROWSERSLIST_DANGEROUS_EXTEND) {
|
---|
200 | checkExtend(name)
|
---|
201 | }
|
---|
202 | var queries = require(require.resolve(name, { paths: ['.', ctx.path] }))
|
---|
203 | if (queries) {
|
---|
204 | if (Array.isArray(queries)) {
|
---|
205 | return queries
|
---|
206 | } else if (typeof queries === 'object') {
|
---|
207 | if (!queries.defaults) queries.defaults = []
|
---|
208 | return pickEnv(queries, ctx, name)
|
---|
209 | }
|
---|
210 | }
|
---|
211 | throw new BrowserslistError(
|
---|
212 | '`' +
|
---|
213 | name +
|
---|
214 | '` config exports not an array of queries' +
|
---|
215 | ' or an object of envs'
|
---|
216 | )
|
---|
217 | },
|
---|
218 |
|
---|
219 | loadStat: function loadStat(ctx, name, data) {
|
---|
220 | if (!ctx.dangerousExtend && !process.env.BROWSERSLIST_DANGEROUS_EXTEND) {
|
---|
221 | checkExtend(name)
|
---|
222 | }
|
---|
223 | var stats = require(require.resolve(
|
---|
224 | path.join(name, 'browserslist-stats.json'),
|
---|
225 | { paths: ['.'] }
|
---|
226 | ))
|
---|
227 | return normalizeStats(data, stats)
|
---|
228 | },
|
---|
229 |
|
---|
230 | getStat: function getStat(opts, data) {
|
---|
231 | var stats
|
---|
232 | if (opts.stats) {
|
---|
233 | stats = opts.stats
|
---|
234 | } else if (process.env.BROWSERSLIST_STATS) {
|
---|
235 | stats = process.env.BROWSERSLIST_STATS
|
---|
236 | } else if (opts.path && path.resolve && fs.existsSync) {
|
---|
237 | stats = eachParent(opts.path, function (dir) {
|
---|
238 | var file = path.join(dir, 'browserslist-stats.json')
|
---|
239 | return isFile(file) ? file : undefined
|
---|
240 | })
|
---|
241 | }
|
---|
242 | if (typeof stats === 'string') {
|
---|
243 | try {
|
---|
244 | stats = JSON.parse(fs.readFileSync(stats))
|
---|
245 | } catch (e) {
|
---|
246 | throw new BrowserslistError("Can't read " + stats)
|
---|
247 | }
|
---|
248 | }
|
---|
249 | return normalizeStats(data, stats)
|
---|
250 | },
|
---|
251 |
|
---|
252 | loadConfig: function loadConfig(opts) {
|
---|
253 | if (process.env.BROWSERSLIST) {
|
---|
254 | return process.env.BROWSERSLIST
|
---|
255 | } else if (opts.config || process.env.BROWSERSLIST_CONFIG) {
|
---|
256 | var file = opts.config || process.env.BROWSERSLIST_CONFIG
|
---|
257 | return pickEnv(parsePackageOrReadConfig(file), opts)
|
---|
258 | } else if (opts.path) {
|
---|
259 | return pickEnv(module.exports.findConfig(opts.path), opts)
|
---|
260 | } else {
|
---|
261 | return undefined
|
---|
262 | }
|
---|
263 | },
|
---|
264 |
|
---|
265 | loadCountry: function loadCountry(usage, country, data) {
|
---|
266 | var code = country.replace(/[^\w-]/g, '')
|
---|
267 | if (!usage[code]) {
|
---|
268 | var compressed
|
---|
269 | try {
|
---|
270 | compressed = require('caniuse-lite/data/regions/' + code + '.js')
|
---|
271 | } catch (e) {
|
---|
272 | throw new BrowserslistError('Unknown region name `' + code + '`.')
|
---|
273 | }
|
---|
274 | var usageData = region(compressed)
|
---|
275 | normalizeUsageData(usageData, data)
|
---|
276 | usage[country] = {}
|
---|
277 | for (var i in usageData) {
|
---|
278 | for (var j in usageData[i]) {
|
---|
279 | usage[country][i + ' ' + j] = usageData[i][j]
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | },
|
---|
284 |
|
---|
285 | loadFeature: function loadFeature(features, name) {
|
---|
286 | name = name.replace(/[^\w-]/g, '')
|
---|
287 | if (features[name]) return
|
---|
288 | var compressed
|
---|
289 | try {
|
---|
290 | compressed = require('caniuse-lite/data/features/' + name + '.js')
|
---|
291 | } catch (e) {
|
---|
292 | throw new BrowserslistError('Unknown feature name `' + name + '`.')
|
---|
293 | }
|
---|
294 | var stats = feature(compressed).stats
|
---|
295 | features[name] = {}
|
---|
296 | for (var i in stats) {
|
---|
297 | features[name][i] = {}
|
---|
298 | for (var j in stats[i]) {
|
---|
299 | features[name][i][j] = stats[i][j]
|
---|
300 | }
|
---|
301 | }
|
---|
302 | },
|
---|
303 |
|
---|
304 | parseConfig: function parseConfig(string) {
|
---|
305 | var result = { defaults: [] }
|
---|
306 | var sections = ['defaults']
|
---|
307 |
|
---|
308 | string
|
---|
309 | .toString()
|
---|
310 | .replace(/#[^\n]*/g, '')
|
---|
311 | .split(/\n|,/)
|
---|
312 | .map(function (line) {
|
---|
313 | return line.trim()
|
---|
314 | })
|
---|
315 | .filter(function (line) {
|
---|
316 | return line !== ''
|
---|
317 | })
|
---|
318 | .forEach(function (line) {
|
---|
319 | if (IS_SECTION.test(line)) {
|
---|
320 | sections = line.match(IS_SECTION)[1].trim().split(' ')
|
---|
321 | sections.forEach(function (section) {
|
---|
322 | if (result[section]) {
|
---|
323 | throw new BrowserslistError(
|
---|
324 | 'Duplicate section ' + section + ' in Browserslist config'
|
---|
325 | )
|
---|
326 | }
|
---|
327 | result[section] = []
|
---|
328 | })
|
---|
329 | } else {
|
---|
330 | sections.forEach(function (section) {
|
---|
331 | result[section].push(line)
|
---|
332 | })
|
---|
333 | }
|
---|
334 | })
|
---|
335 |
|
---|
336 | return result
|
---|
337 | },
|
---|
338 |
|
---|
339 | readConfig: function readConfig(file) {
|
---|
340 | if (!isFile(file)) {
|
---|
341 | throw new BrowserslistError("Can't read " + file + ' config')
|
---|
342 | }
|
---|
343 | return module.exports.parseConfig(fs.readFileSync(file))
|
---|
344 | },
|
---|
345 |
|
---|
346 | findConfigFile: function findConfigFile(from) {
|
---|
347 | var resolved = eachParent(from, function (dir) {
|
---|
348 | var config = path.join(dir, 'browserslist')
|
---|
349 | var pkg = path.join(dir, 'package.json')
|
---|
350 | var rc = path.join(dir, '.browserslistrc')
|
---|
351 |
|
---|
352 | var pkgBrowserslist
|
---|
353 | if (isFile(pkg)) {
|
---|
354 | try {
|
---|
355 | pkgBrowserslist = parsePackage(pkg)
|
---|
356 | } catch (e) {
|
---|
357 | if (e.name === 'BrowserslistError') throw e
|
---|
358 | console.warn(
|
---|
359 | '[Browserslist] Could not parse ' + pkg + '. Ignoring it.'
|
---|
360 | )
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | if (isFile(config) && pkgBrowserslist) {
|
---|
365 | throw new BrowserslistError(
|
---|
366 | dir + ' contains both browserslist and package.json with browsers'
|
---|
367 | )
|
---|
368 | } else if (isFile(rc) && pkgBrowserslist) {
|
---|
369 | throw new BrowserslistError(
|
---|
370 | dir + ' contains both .browserslistrc and package.json with browsers'
|
---|
371 | )
|
---|
372 | } else if (isFile(config) && isFile(rc)) {
|
---|
373 | throw new BrowserslistError(
|
---|
374 | dir + ' contains both .browserslistrc and browserslist'
|
---|
375 | )
|
---|
376 | } else if (isFile(config)) {
|
---|
377 | return config
|
---|
378 | } else if (isFile(rc)) {
|
---|
379 | return rc
|
---|
380 | } else if (pkgBrowserslist) {
|
---|
381 | return pkg
|
---|
382 | }
|
---|
383 | })
|
---|
384 |
|
---|
385 | return resolved
|
---|
386 | },
|
---|
387 |
|
---|
388 | findConfig: function findConfig(from) {
|
---|
389 | from = path.resolve(from)
|
---|
390 |
|
---|
391 | var fromDir = isFile(from) ? path.dirname(from) : from
|
---|
392 | if (fromDir in configCache) {
|
---|
393 | return configCache[fromDir]
|
---|
394 | }
|
---|
395 |
|
---|
396 | var resolved
|
---|
397 | var configFile = this.findConfigFile(from)
|
---|
398 | if (configFile) {
|
---|
399 | resolved = parsePackageOrReadConfig(configFile)
|
---|
400 | }
|
---|
401 |
|
---|
402 | if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
|
---|
403 | var configDir = configFile && path.dirname(configFile)
|
---|
404 | eachParent(from, function (dir) {
|
---|
405 | configCache[dir] = resolved
|
---|
406 | if (dir === configDir) {
|
---|
407 | return null
|
---|
408 | }
|
---|
409 | })
|
---|
410 | }
|
---|
411 | return resolved
|
---|
412 | },
|
---|
413 |
|
---|
414 | clearCaches: function clearCaches() {
|
---|
415 | dataTimeChecked = false
|
---|
416 | filenessCache = {}
|
---|
417 | configCache = {}
|
---|
418 |
|
---|
419 | this.cache = {}
|
---|
420 | },
|
---|
421 |
|
---|
422 | oldDataWarning: function oldDataWarning(agentsObj) {
|
---|
423 | if (dataTimeChecked) return
|
---|
424 | dataTimeChecked = true
|
---|
425 | if (process.env.BROWSERSLIST_IGNORE_OLD_DATA) return
|
---|
426 |
|
---|
427 | var latest = latestReleaseTime(agentsObj)
|
---|
428 | var monthsPassed = getMonthsPassed(latest)
|
---|
429 |
|
---|
430 | if (latest !== 0 && monthsPassed >= 6) {
|
---|
431 | var months = monthsPassed + ' ' + (monthsPassed > 1 ? 'months' : 'month')
|
---|
432 | console.warn(
|
---|
433 | 'Browserslist: browsers data (caniuse-lite) is ' +
|
---|
434 | months +
|
---|
435 | ' old. Please run:\n' +
|
---|
436 | ' npx update-browserslist-db@latest\n' +
|
---|
437 | ' Why you should do it regularly: ' +
|
---|
438 | 'https://github.com/browserslist/update-db#readme'
|
---|
439 | )
|
---|
440 | }
|
---|
441 | },
|
---|
442 |
|
---|
443 | currentNode: function currentNode() {
|
---|
444 | return 'node ' + process.versions.node
|
---|
445 | },
|
---|
446 |
|
---|
447 | env: process.env
|
---|
448 | }
|
---|