source: trip-planner-front/node_modules/browserslist/node.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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