index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. var jsReleases = require('node-releases/data/processed/envs.json')
  2. var agents = require('caniuse-lite/dist/unpacker/agents').agents
  3. var e2c = require('electron-to-chromium/versions')
  4. var jsEOL = require('node-releases/data/release-schedule/release-schedule.json')
  5. var path = require('path')
  6. var BrowserslistError = require('./error')
  7. var env = require('./node')
  8. var parse = require('./parse') // Will load browser.js in webpack
  9. var YEAR = 365.259641 * 24 * 60 * 60 * 1000
  10. var ANDROID_EVERGREEN_FIRST = '37'
  11. var OP_MOB_BLINK_FIRST = 14
  12. // Helpers
  13. function isVersionsMatch(versionA, versionB) {
  14. return (versionA + '.').indexOf(versionB + '.') === 0
  15. }
  16. function isEolReleased(name) {
  17. var version = name.slice(1)
  18. return browserslist.nodeVersions.some(function (i) {
  19. return isVersionsMatch(i, version)
  20. })
  21. }
  22. function normalize(versions) {
  23. return versions.filter(function (version) {
  24. return typeof version === 'string'
  25. })
  26. }
  27. function normalizeElectron(version) {
  28. var versionToUse = version
  29. if (version.split('.').length === 3) {
  30. versionToUse = version.split('.').slice(0, -1).join('.')
  31. }
  32. return versionToUse
  33. }
  34. function nameMapper(name) {
  35. return function mapName(version) {
  36. return name + ' ' + version
  37. }
  38. }
  39. function getMajor(version) {
  40. return parseInt(version.split('.')[0])
  41. }
  42. function getMajorVersions(released, number) {
  43. if (released.length === 0) return []
  44. var majorVersions = uniq(released.map(getMajor))
  45. var minimum = majorVersions[majorVersions.length - number]
  46. if (!minimum) {
  47. return released
  48. }
  49. var selected = []
  50. for (var i = released.length - 1; i >= 0; i--) {
  51. if (minimum > getMajor(released[i])) break
  52. selected.unshift(released[i])
  53. }
  54. return selected
  55. }
  56. function uniq(array) {
  57. var filtered = []
  58. for (var i = 0; i < array.length; i++) {
  59. if (filtered.indexOf(array[i]) === -1) filtered.push(array[i])
  60. }
  61. return filtered
  62. }
  63. function fillUsage(result, name, data) {
  64. for (var i in data) {
  65. result[name + ' ' + i] = data[i]
  66. }
  67. }
  68. function generateFilter(sign, version) {
  69. version = parseFloat(version)
  70. if (sign === '>') {
  71. return function (v) {
  72. return parseLatestFloat(v) > version
  73. }
  74. } else if (sign === '>=') {
  75. return function (v) {
  76. return parseLatestFloat(v) >= version
  77. }
  78. } else if (sign === '<') {
  79. return function (v) {
  80. return parseFloat(v) < version
  81. }
  82. } else {
  83. return function (v) {
  84. return parseFloat(v) <= version
  85. }
  86. }
  87. function parseLatestFloat(v) {
  88. return parseFloat(v.split('-')[1] || v)
  89. }
  90. }
  91. function generateSemverFilter(sign, version) {
  92. version = version.split('.').map(parseSimpleInt)
  93. version[1] = version[1] || 0
  94. version[2] = version[2] || 0
  95. if (sign === '>') {
  96. return function (v) {
  97. v = v.split('.').map(parseSimpleInt)
  98. return compareSemver(v, version) > 0
  99. }
  100. } else if (sign === '>=') {
  101. return function (v) {
  102. v = v.split('.').map(parseSimpleInt)
  103. return compareSemver(v, version) >= 0
  104. }
  105. } else if (sign === '<') {
  106. return function (v) {
  107. v = v.split('.').map(parseSimpleInt)
  108. return compareSemver(version, v) > 0
  109. }
  110. } else {
  111. return function (v) {
  112. v = v.split('.').map(parseSimpleInt)
  113. return compareSemver(version, v) >= 0
  114. }
  115. }
  116. }
  117. function parseSimpleInt(x) {
  118. return parseInt(x)
  119. }
  120. function compare(a, b) {
  121. if (a < b) return -1
  122. if (a > b) return +1
  123. return 0
  124. }
  125. function compareSemver(a, b) {
  126. return (
  127. compare(parseInt(a[0]), parseInt(b[0])) ||
  128. compare(parseInt(a[1] || '0'), parseInt(b[1] || '0')) ||
  129. compare(parseInt(a[2] || '0'), parseInt(b[2] || '0'))
  130. )
  131. }
  132. // this follows the npm-like semver behavior
  133. function semverFilterLoose(operator, range) {
  134. range = range.split('.').map(parseSimpleInt)
  135. if (typeof range[1] === 'undefined') {
  136. range[1] = 'x'
  137. }
  138. // ignore any patch version because we only return minor versions
  139. // range[2] = 'x'
  140. switch (operator) {
  141. case '<=':
  142. return function (version) {
  143. version = version.split('.').map(parseSimpleInt)
  144. return compareSemverLoose(version, range) <= 0
  145. }
  146. case '>=':
  147. default:
  148. return function (version) {
  149. version = version.split('.').map(parseSimpleInt)
  150. return compareSemverLoose(version, range) >= 0
  151. }
  152. }
  153. }
  154. // this follows the npm-like semver behavior
  155. function compareSemverLoose(version, range) {
  156. if (version[0] !== range[0]) {
  157. return version[0] < range[0] ? -1 : +1
  158. }
  159. if (range[1] === 'x') {
  160. return 0
  161. }
  162. if (version[1] !== range[1]) {
  163. return version[1] < range[1] ? -1 : +1
  164. }
  165. return 0
  166. }
  167. function resolveVersion(data, version) {
  168. if (data.versions.indexOf(version) !== -1) {
  169. return version
  170. } else if (browserslist.versionAliases[data.name][version]) {
  171. return browserslist.versionAliases[data.name][version]
  172. } else {
  173. return false
  174. }
  175. }
  176. function normalizeVersion(data, version) {
  177. var resolved = resolveVersion(data, version)
  178. if (resolved) {
  179. return resolved
  180. } else if (data.versions.length === 1) {
  181. return data.versions[0]
  182. } else {
  183. return false
  184. }
  185. }
  186. function filterByYear(since, context) {
  187. since = since / 1000
  188. return Object.keys(agents).reduce(function (selected, name) {
  189. var data = byName(name, context)
  190. if (!data) return selected
  191. var versions = Object.keys(data.releaseDate).filter(function (v) {
  192. var date = data.releaseDate[v]
  193. return date !== null && date >= since
  194. })
  195. return selected.concat(versions.map(nameMapper(data.name)))
  196. }, [])
  197. }
  198. function cloneData(data) {
  199. return {
  200. name: data.name,
  201. versions: data.versions,
  202. released: data.released,
  203. releaseDate: data.releaseDate
  204. }
  205. }
  206. function byName(name, context) {
  207. name = name.toLowerCase()
  208. name = browserslist.aliases[name] || name
  209. if (context.mobileToDesktop && browserslist.desktopNames[name]) {
  210. var desktop = browserslist.data[browserslist.desktopNames[name]]
  211. if (name === 'android') {
  212. return normalizeAndroidData(cloneData(browserslist.data[name]), desktop)
  213. } else {
  214. var cloned = cloneData(desktop)
  215. cloned.name = name
  216. return cloned
  217. }
  218. }
  219. return browserslist.data[name]
  220. }
  221. function normalizeAndroidVersions(androidVersions, chromeVersions) {
  222. var iFirstEvergreen = chromeVersions.indexOf(ANDROID_EVERGREEN_FIRST)
  223. return androidVersions
  224. .filter(function (version) {
  225. return /^(?:[2-4]\.|[34]$)/.test(version)
  226. })
  227. .concat(chromeVersions.slice(iFirstEvergreen))
  228. }
  229. function copyObject(obj) {
  230. var copy = {}
  231. for (var key in obj) {
  232. copy[key] = obj[key]
  233. }
  234. return copy
  235. }
  236. function normalizeAndroidData(android, chrome) {
  237. android.released = normalizeAndroidVersions(android.released, chrome.released)
  238. android.versions = normalizeAndroidVersions(android.versions, chrome.versions)
  239. android.releaseDate = copyObject(android.releaseDate)
  240. android.released.forEach(function (v) {
  241. if (android.releaseDate[v] === undefined) {
  242. android.releaseDate[v] = chrome.releaseDate[v]
  243. }
  244. })
  245. return android
  246. }
  247. function checkName(name, context) {
  248. var data = byName(name, context)
  249. if (!data) throw new BrowserslistError('Unknown browser ' + name)
  250. return data
  251. }
  252. function unknownQuery(query) {
  253. return new BrowserslistError(
  254. 'Unknown browser query `' +
  255. query +
  256. '`. ' +
  257. 'Maybe you are using old Browserslist or made typo in query.'
  258. )
  259. }
  260. // Adjusts last X versions queries for some mobile browsers,
  261. // where caniuse data jumps from a legacy version to the latest
  262. function filterJumps(list, name, nVersions, context) {
  263. var jump = 1
  264. switch (name) {
  265. case 'android':
  266. if (context.mobileToDesktop) return list
  267. var released = browserslist.data.chrome.released
  268. jump = released.length - released.indexOf(ANDROID_EVERGREEN_FIRST)
  269. break
  270. case 'op_mob':
  271. var latest = browserslist.data.op_mob.released.slice(-1)[0]
  272. jump = getMajor(latest) - OP_MOB_BLINK_FIRST + 1
  273. break
  274. default:
  275. return list
  276. }
  277. if (nVersions <= jump) {
  278. return list.slice(-1)
  279. }
  280. return list.slice(jump - 1 - nVersions)
  281. }
  282. function isSupported(flags, withPartial) {
  283. return (
  284. typeof flags === 'string' &&
  285. (flags.indexOf('y') >= 0 || (withPartial && flags.indexOf('a') >= 0))
  286. )
  287. }
  288. function resolve(queries, context) {
  289. return parse(QUERIES, queries).reduce(function (result, node, index) {
  290. if (node.not && index === 0) {
  291. throw new BrowserslistError(
  292. 'Write any browsers query (for instance, `defaults`) ' +
  293. 'before `' +
  294. node.query +
  295. '`'
  296. )
  297. }
  298. var type = QUERIES[node.type]
  299. var array = type.select.call(browserslist, context, node).map(function (j) {
  300. var parts = j.split(' ')
  301. if (parts[1] === '0') {
  302. return parts[0] + ' ' + byName(parts[0], context).versions[0]
  303. } else {
  304. return j
  305. }
  306. })
  307. if (node.compose === 'and') {
  308. if (node.not) {
  309. return result.filter(function (j) {
  310. return array.indexOf(j) === -1
  311. })
  312. } else {
  313. return result.filter(function (j) {
  314. return array.indexOf(j) !== -1
  315. })
  316. }
  317. } else {
  318. if (node.not) {
  319. var filter = {}
  320. array.forEach(function (j) {
  321. filter[j] = true
  322. })
  323. return result.filter(function (j) {
  324. return !filter[j]
  325. })
  326. }
  327. return result.concat(array)
  328. }
  329. }, [])
  330. }
  331. function prepareOpts(opts) {
  332. if (typeof opts === 'undefined') opts = {}
  333. if (typeof opts.path === 'undefined') {
  334. opts.path = path.resolve ? path.resolve('.') : '.'
  335. }
  336. return opts
  337. }
  338. function prepareQueries(queries, opts) {
  339. if (typeof queries === 'undefined' || queries === null) {
  340. var config = browserslist.loadConfig(opts)
  341. if (config) {
  342. queries = config
  343. } else {
  344. queries = browserslist.defaults
  345. }
  346. }
  347. return queries
  348. }
  349. function checkQueries(queries) {
  350. if (!(typeof queries === 'string' || Array.isArray(queries))) {
  351. throw new BrowserslistError(
  352. 'Browser queries must be an array or string. Got ' + typeof queries + '.'
  353. )
  354. }
  355. }
  356. var cache = {}
  357. function browserslist(queries, opts) {
  358. opts = prepareOpts(opts)
  359. queries = prepareQueries(queries, opts)
  360. checkQueries(queries)
  361. var context = {
  362. ignoreUnknownVersions: opts.ignoreUnknownVersions,
  363. dangerousExtend: opts.dangerousExtend,
  364. mobileToDesktop: opts.mobileToDesktop,
  365. path: opts.path,
  366. env: opts.env
  367. }
  368. env.oldDataWarning(browserslist.data)
  369. var stats = env.getStat(opts, browserslist.data)
  370. if (stats) {
  371. context.customUsage = {}
  372. for (var browser in stats) {
  373. fillUsage(context.customUsage, browser, stats[browser])
  374. }
  375. }
  376. var cacheKey = JSON.stringify([queries, context])
  377. if (cache[cacheKey]) return cache[cacheKey]
  378. var result = uniq(resolve(queries, context)).sort(function (name1, name2) {
  379. name1 = name1.split(' ')
  380. name2 = name2.split(' ')
  381. if (name1[0] === name2[0]) {
  382. // assumptions on caniuse data
  383. // 1) version ranges never overlaps
  384. // 2) if version is not a range, it never contains `-`
  385. var version1 = name1[1].split('-')[0]
  386. var version2 = name2[1].split('-')[0]
  387. return compareSemver(version2.split('.'), version1.split('.'))
  388. } else {
  389. return compare(name1[0], name2[0])
  390. }
  391. })
  392. if (!env.env.BROWSERSLIST_DISABLE_CACHE) {
  393. cache[cacheKey] = result
  394. }
  395. return result
  396. }
  397. browserslist.parse = function (queries, opts) {
  398. opts = prepareOpts(opts)
  399. queries = prepareQueries(queries, opts)
  400. checkQueries(queries)
  401. return parse(QUERIES, queries)
  402. }
  403. // Will be filled by Can I Use data below
  404. browserslist.cache = {}
  405. browserslist.data = {}
  406. browserslist.usage = {
  407. global: {},
  408. custom: null
  409. }
  410. // Default browsers query
  411. browserslist.defaults = ['> 0.5%', 'last 2 versions', 'Firefox ESR', 'not dead']
  412. // Browser names aliases
  413. browserslist.aliases = {
  414. fx: 'firefox',
  415. ff: 'firefox',
  416. ios: 'ios_saf',
  417. explorer: 'ie',
  418. blackberry: 'bb',
  419. explorermobile: 'ie_mob',
  420. operamini: 'op_mini',
  421. operamobile: 'op_mob',
  422. chromeandroid: 'and_chr',
  423. firefoxandroid: 'and_ff',
  424. ucandroid: 'and_uc',
  425. qqandroid: 'and_qq'
  426. }
  427. // Can I Use only provides a few versions for some browsers (e.g. and_chr).
  428. // Fallback to a similar browser for unknown versions
  429. // Note op_mob is not included as its chromium versions are not in sync with Opera desktop
  430. browserslist.desktopNames = {
  431. and_chr: 'chrome',
  432. and_ff: 'firefox',
  433. ie_mob: 'ie',
  434. android: 'chrome' // has extra processing logic
  435. }
  436. // Aliases to work with joined versions like `ios_saf 7.0-7.1`
  437. browserslist.versionAliases = {}
  438. browserslist.clearCaches = env.clearCaches
  439. browserslist.parseConfig = env.parseConfig
  440. browserslist.readConfig = env.readConfig
  441. browserslist.findConfigFile = env.findConfigFile
  442. browserslist.findConfig = env.findConfig
  443. browserslist.loadConfig = env.loadConfig
  444. browserslist.coverage = function (browsers, stats) {
  445. var data
  446. if (typeof stats === 'undefined') {
  447. data = browserslist.usage.global
  448. } else if (stats === 'my stats') {
  449. var opts = {}
  450. opts.path = path.resolve ? path.resolve('.') : '.'
  451. var customStats = env.getStat(opts)
  452. if (!customStats) {
  453. throw new BrowserslistError('Custom usage statistics was not provided')
  454. }
  455. data = {}
  456. for (var browser in customStats) {
  457. fillUsage(data, browser, customStats[browser])
  458. }
  459. } else if (typeof stats === 'string') {
  460. if (stats.length > 2) {
  461. stats = stats.toLowerCase()
  462. } else {
  463. stats = stats.toUpperCase()
  464. }
  465. env.loadCountry(browserslist.usage, stats, browserslist.data)
  466. data = browserslist.usage[stats]
  467. } else {
  468. if ('dataByBrowser' in stats) {
  469. stats = stats.dataByBrowser
  470. }
  471. data = {}
  472. for (var name in stats) {
  473. for (var version in stats[name]) {
  474. data[name + ' ' + version] = stats[name][version]
  475. }
  476. }
  477. }
  478. return browsers.reduce(function (all, i) {
  479. var usage = data[i]
  480. if (usage === undefined) {
  481. usage = data[i.replace(/ \S+$/, ' 0')]
  482. }
  483. return all + (usage || 0)
  484. }, 0)
  485. }
  486. function nodeQuery(context, node) {
  487. var matched = browserslist.nodeVersions.filter(function (i) {
  488. return isVersionsMatch(i, node.version)
  489. })
  490. if (matched.length === 0) {
  491. if (context.ignoreUnknownVersions) {
  492. return []
  493. } else {
  494. throw new BrowserslistError(
  495. 'Unknown version ' + node.version + ' of Node.js'
  496. )
  497. }
  498. }
  499. return ['node ' + matched[matched.length - 1]]
  500. }
  501. function sinceQuery(context, node) {
  502. var year = parseInt(node.year)
  503. var month = parseInt(node.month || '01') - 1
  504. var day = parseInt(node.day || '01')
  505. return filterByYear(Date.UTC(year, month, day, 0, 0, 0), context)
  506. }
  507. function coverQuery(context, node) {
  508. var coverage = parseFloat(node.coverage)
  509. var usage = browserslist.usage.global
  510. if (node.place) {
  511. if (node.place.match(/^my\s+stats$/i)) {
  512. if (!context.customUsage) {
  513. throw new BrowserslistError('Custom usage statistics was not provided')
  514. }
  515. usage = context.customUsage
  516. } else {
  517. var place
  518. if (node.place.length === 2) {
  519. place = node.place.toUpperCase()
  520. } else {
  521. place = node.place.toLowerCase()
  522. }
  523. env.loadCountry(browserslist.usage, place, browserslist.data)
  524. usage = browserslist.usage[place]
  525. }
  526. }
  527. var versions = Object.keys(usage).sort(function (a, b) {
  528. return usage[b] - usage[a]
  529. })
  530. var coveraged = 0
  531. var result = []
  532. var version
  533. for (var i = 0; i < versions.length; i++) {
  534. version = versions[i]
  535. if (usage[version] === 0) break
  536. coveraged += usage[version]
  537. result.push(version)
  538. if (coveraged >= coverage) break
  539. }
  540. return result
  541. }
  542. var QUERIES = {
  543. last_major_versions: {
  544. matches: ['versions'],
  545. regexp: /^last\s+(\d+)\s+major\s+versions?$/i,
  546. select: function (context, node) {
  547. return Object.keys(agents).reduce(function (selected, name) {
  548. var data = byName(name, context)
  549. if (!data) return selected
  550. var list = getMajorVersions(data.released, node.versions)
  551. list = list.map(nameMapper(data.name))
  552. list = filterJumps(list, data.name, node.versions, context)
  553. return selected.concat(list)
  554. }, [])
  555. }
  556. },
  557. last_versions: {
  558. matches: ['versions'],
  559. regexp: /^last\s+(\d+)\s+versions?$/i,
  560. select: function (context, node) {
  561. return Object.keys(agents).reduce(function (selected, name) {
  562. var data = byName(name, context)
  563. if (!data) return selected
  564. var list = data.released.slice(-node.versions)
  565. list = list.map(nameMapper(data.name))
  566. list = filterJumps(list, data.name, node.versions, context)
  567. return selected.concat(list)
  568. }, [])
  569. }
  570. },
  571. last_electron_major_versions: {
  572. matches: ['versions'],
  573. regexp: /^last\s+(\d+)\s+electron\s+major\s+versions?$/i,
  574. select: function (context, node) {
  575. var validVersions = getMajorVersions(Object.keys(e2c), node.versions)
  576. return validVersions.map(function (i) {
  577. return 'chrome ' + e2c[i]
  578. })
  579. }
  580. },
  581. last_node_major_versions: {
  582. matches: ['versions'],
  583. regexp: /^last\s+(\d+)\s+node\s+major\s+versions?$/i,
  584. select: function (context, node) {
  585. return getMajorVersions(browserslist.nodeVersions, node.versions).map(
  586. function (version) {
  587. return 'node ' + version
  588. }
  589. )
  590. }
  591. },
  592. last_browser_major_versions: {
  593. matches: ['versions', 'browser'],
  594. regexp: /^last\s+(\d+)\s+(\w+)\s+major\s+versions?$/i,
  595. select: function (context, node) {
  596. var data = checkName(node.browser, context)
  597. var validVersions = getMajorVersions(data.released, node.versions)
  598. var list = validVersions.map(nameMapper(data.name))
  599. list = filterJumps(list, data.name, node.versions, context)
  600. return list
  601. }
  602. },
  603. last_electron_versions: {
  604. matches: ['versions'],
  605. regexp: /^last\s+(\d+)\s+electron\s+versions?$/i,
  606. select: function (context, node) {
  607. return Object.keys(e2c)
  608. .slice(-node.versions)
  609. .map(function (i) {
  610. return 'chrome ' + e2c[i]
  611. })
  612. }
  613. },
  614. last_node_versions: {
  615. matches: ['versions'],
  616. regexp: /^last\s+(\d+)\s+node\s+versions?$/i,
  617. select: function (context, node) {
  618. return browserslist.nodeVersions
  619. .slice(-node.versions)
  620. .map(function (version) {
  621. return 'node ' + version
  622. })
  623. }
  624. },
  625. last_browser_versions: {
  626. matches: ['versions', 'browser'],
  627. regexp: /^last\s+(\d+)\s+(\w+)\s+versions?$/i,
  628. select: function (context, node) {
  629. var data = checkName(node.browser, context)
  630. var list = data.released.slice(-node.versions).map(nameMapper(data.name))
  631. list = filterJumps(list, data.name, node.versions, context)
  632. return list
  633. }
  634. },
  635. unreleased_versions: {
  636. matches: [],
  637. regexp: /^unreleased\s+versions$/i,
  638. select: function (context) {
  639. return Object.keys(agents).reduce(function (selected, name) {
  640. var data = byName(name, context)
  641. if (!data) return selected
  642. var list = data.versions.filter(function (v) {
  643. return data.released.indexOf(v) === -1
  644. })
  645. list = list.map(nameMapper(data.name))
  646. return selected.concat(list)
  647. }, [])
  648. }
  649. },
  650. unreleased_electron_versions: {
  651. matches: [],
  652. regexp: /^unreleased\s+electron\s+versions?$/i,
  653. select: function () {
  654. return []
  655. }
  656. },
  657. unreleased_browser_versions: {
  658. matches: ['browser'],
  659. regexp: /^unreleased\s+(\w+)\s+versions?$/i,
  660. select: function (context, node) {
  661. var data = checkName(node.browser, context)
  662. return data.versions
  663. .filter(function (v) {
  664. return data.released.indexOf(v) === -1
  665. })
  666. .map(nameMapper(data.name))
  667. }
  668. },
  669. last_years: {
  670. matches: ['years'],
  671. regexp: /^last\s+(\d*.?\d+)\s+years?$/i,
  672. select: function (context, node) {
  673. return filterByYear(Date.now() - YEAR * node.years, context)
  674. }
  675. },
  676. since_y: {
  677. matches: ['year'],
  678. regexp: /^since (\d+)$/i,
  679. select: sinceQuery
  680. },
  681. since_y_m: {
  682. matches: ['year', 'month'],
  683. regexp: /^since (\d+)-(\d+)$/i,
  684. select: sinceQuery
  685. },
  686. since_y_m_d: {
  687. matches: ['year', 'month', 'day'],
  688. regexp: /^since (\d+)-(\d+)-(\d+)$/i,
  689. select: sinceQuery
  690. },
  691. popularity: {
  692. matches: ['sign', 'popularity'],
  693. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%$/,
  694. select: function (context, node) {
  695. var popularity = parseFloat(node.popularity)
  696. var usage = browserslist.usage.global
  697. return Object.keys(usage).reduce(function (result, version) {
  698. if (node.sign === '>') {
  699. if (usage[version] > popularity) {
  700. result.push(version)
  701. }
  702. } else if (node.sign === '<') {
  703. if (usage[version] < popularity) {
  704. result.push(version)
  705. }
  706. } else if (node.sign === '<=') {
  707. if (usage[version] <= popularity) {
  708. result.push(version)
  709. }
  710. } else if (usage[version] >= popularity) {
  711. result.push(version)
  712. }
  713. return result
  714. }, [])
  715. }
  716. },
  717. popularity_in_my_stats: {
  718. matches: ['sign', 'popularity'],
  719. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%\s+in\s+my\s+stats$/,
  720. select: function (context, node) {
  721. var popularity = parseFloat(node.popularity)
  722. if (!context.customUsage) {
  723. throw new BrowserslistError('Custom usage statistics was not provided')
  724. }
  725. var usage = context.customUsage
  726. return Object.keys(usage).reduce(function (result, version) {
  727. var percentage = usage[version]
  728. if (percentage == null) {
  729. return result
  730. }
  731. if (node.sign === '>') {
  732. if (percentage > popularity) {
  733. result.push(version)
  734. }
  735. } else if (node.sign === '<') {
  736. if (percentage < popularity) {
  737. result.push(version)
  738. }
  739. } else if (node.sign === '<=') {
  740. if (percentage <= popularity) {
  741. result.push(version)
  742. }
  743. } else if (percentage >= popularity) {
  744. result.push(version)
  745. }
  746. return result
  747. }, [])
  748. }
  749. },
  750. popularity_in_config_stats: {
  751. matches: ['sign', 'popularity', 'config'],
  752. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%\s+in\s+(\S+)\s+stats$/,
  753. select: function (context, node) {
  754. var popularity = parseFloat(node.popularity)
  755. var stats = env.loadStat(context, node.config, browserslist.data)
  756. if (stats) {
  757. context.customUsage = {}
  758. for (var browser in stats) {
  759. fillUsage(context.customUsage, browser, stats[browser])
  760. }
  761. }
  762. if (!context.customUsage) {
  763. throw new BrowserslistError('Custom usage statistics was not provided')
  764. }
  765. var usage = context.customUsage
  766. return Object.keys(usage).reduce(function (result, version) {
  767. var percentage = usage[version]
  768. if (percentage == null) {
  769. return result
  770. }
  771. if (node.sign === '>') {
  772. if (percentage > popularity) {
  773. result.push(version)
  774. }
  775. } else if (node.sign === '<') {
  776. if (percentage < popularity) {
  777. result.push(version)
  778. }
  779. } else if (node.sign === '<=') {
  780. if (percentage <= popularity) {
  781. result.push(version)
  782. }
  783. } else if (percentage >= popularity) {
  784. result.push(version)
  785. }
  786. return result
  787. }, [])
  788. }
  789. },
  790. popularity_in_place: {
  791. matches: ['sign', 'popularity', 'place'],
  792. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%\s+in\s+((alt-)?\w\w)$/,
  793. select: function (context, node) {
  794. var popularity = parseFloat(node.popularity)
  795. var place = node.place
  796. if (place.length === 2) {
  797. place = place.toUpperCase()
  798. } else {
  799. place = place.toLowerCase()
  800. }
  801. env.loadCountry(browserslist.usage, place, browserslist.data)
  802. var usage = browserslist.usage[place]
  803. return Object.keys(usage).reduce(function (result, version) {
  804. var percentage = usage[version]
  805. if (percentage == null) {
  806. return result
  807. }
  808. if (node.sign === '>') {
  809. if (percentage > popularity) {
  810. result.push(version)
  811. }
  812. } else if (node.sign === '<') {
  813. if (percentage < popularity) {
  814. result.push(version)
  815. }
  816. } else if (node.sign === '<=') {
  817. if (percentage <= popularity) {
  818. result.push(version)
  819. }
  820. } else if (percentage >= popularity) {
  821. result.push(version)
  822. }
  823. return result
  824. }, [])
  825. }
  826. },
  827. cover: {
  828. matches: ['coverage'],
  829. regexp: /^cover\s+(\d+|\d+\.\d+|\.\d+)%$/i,
  830. select: coverQuery
  831. },
  832. cover_in: {
  833. matches: ['coverage', 'place'],
  834. regexp: /^cover\s+(\d+|\d+\.\d+|\.\d+)%\s+in\s+(my\s+stats|(alt-)?\w\w)$/i,
  835. select: coverQuery
  836. },
  837. supports: {
  838. matches: ['supportType', 'feature'],
  839. regexp: /^(?:(fully|partially)\s+)?supports\s+([\w-]+)$/,
  840. select: function (context, node) {
  841. env.loadFeature(browserslist.cache, node.feature)
  842. var withPartial = node.supportType !== 'fully'
  843. var features = browserslist.cache[node.feature]
  844. var result = []
  845. for (var name in features) {
  846. var data = byName(name, context)
  847. // Only check desktop when latest released mobile has support
  848. var iMax = data.released.length - 1
  849. while (iMax >= 0) {
  850. if (data.released[iMax] in features[name]) break
  851. iMax--
  852. }
  853. var checkDesktop =
  854. context.mobileToDesktop &&
  855. name in browserslist.desktopNames &&
  856. isSupported(features[name][data.released[iMax]], withPartial)
  857. data.versions.forEach(function (version) {
  858. var flags = features[name][version]
  859. if (flags === undefined && checkDesktop) {
  860. flags = features[browserslist.desktopNames[name]][version]
  861. }
  862. if (isSupported(flags, withPartial)) {
  863. result.push(name + ' ' + version)
  864. }
  865. })
  866. }
  867. return result
  868. }
  869. },
  870. electron_range: {
  871. matches: ['from', 'to'],
  872. regexp: /^electron\s+([\d.]+)\s*-\s*([\d.]+)$/i,
  873. select: function (context, node) {
  874. var fromToUse = normalizeElectron(node.from)
  875. var toToUse = normalizeElectron(node.to)
  876. var from = parseFloat(node.from)
  877. var to = parseFloat(node.to)
  878. if (!e2c[fromToUse]) {
  879. throw new BrowserslistError('Unknown version ' + from + ' of electron')
  880. }
  881. if (!e2c[toToUse]) {
  882. throw new BrowserslistError('Unknown version ' + to + ' of electron')
  883. }
  884. return Object.keys(e2c)
  885. .filter(function (i) {
  886. var parsed = parseFloat(i)
  887. return parsed >= from && parsed <= to
  888. })
  889. .map(function (i) {
  890. return 'chrome ' + e2c[i]
  891. })
  892. }
  893. },
  894. node_range: {
  895. matches: ['from', 'to'],
  896. regexp: /^node\s+([\d.]+)\s*-\s*([\d.]+)$/i,
  897. select: function (context, node) {
  898. return browserslist.nodeVersions
  899. .filter(semverFilterLoose('>=', node.from))
  900. .filter(semverFilterLoose('<=', node.to))
  901. .map(function (v) {
  902. return 'node ' + v
  903. })
  904. }
  905. },
  906. browser_range: {
  907. matches: ['browser', 'from', 'to'],
  908. regexp: /^(\w+)\s+([\d.]+)\s*-\s*([\d.]+)$/i,
  909. select: function (context, node) {
  910. var data = checkName(node.browser, context)
  911. var from = parseFloat(normalizeVersion(data, node.from) || node.from)
  912. var to = parseFloat(normalizeVersion(data, node.to) || node.to)
  913. function filter(v) {
  914. var parsed = parseFloat(v)
  915. return parsed >= from && parsed <= to
  916. }
  917. return data.released.filter(filter).map(nameMapper(data.name))
  918. }
  919. },
  920. electron_ray: {
  921. matches: ['sign', 'version'],
  922. regexp: /^electron\s*(>=?|<=?)\s*([\d.]+)$/i,
  923. select: function (context, node) {
  924. var versionToUse = normalizeElectron(node.version)
  925. return Object.keys(e2c)
  926. .filter(generateFilter(node.sign, versionToUse))
  927. .map(function (i) {
  928. return 'chrome ' + e2c[i]
  929. })
  930. }
  931. },
  932. node_ray: {
  933. matches: ['sign', 'version'],
  934. regexp: /^node\s*(>=?|<=?)\s*([\d.]+)$/i,
  935. select: function (context, node) {
  936. return browserslist.nodeVersions
  937. .filter(generateSemverFilter(node.sign, node.version))
  938. .map(function (v) {
  939. return 'node ' + v
  940. })
  941. }
  942. },
  943. browser_ray: {
  944. matches: ['browser', 'sign', 'version'],
  945. regexp: /^(\w+)\s*(>=?|<=?)\s*([\d.]+)$/,
  946. select: function (context, node) {
  947. var version = node.version
  948. var data = checkName(node.browser, context)
  949. var alias = browserslist.versionAliases[data.name][version]
  950. if (alias) version = alias
  951. return data.released
  952. .filter(generateFilter(node.sign, version))
  953. .map(function (v) {
  954. return data.name + ' ' + v
  955. })
  956. }
  957. },
  958. firefox_esr: {
  959. matches: [],
  960. regexp: /^(firefox|ff|fx)\s+esr$/i,
  961. select: function () {
  962. return ['firefox 115', 'firefox 128']
  963. }
  964. },
  965. opera_mini_all: {
  966. matches: [],
  967. regexp: /(operamini|op_mini)\s+all/i,
  968. select: function () {
  969. return ['op_mini all']
  970. }
  971. },
  972. electron_version: {
  973. matches: ['version'],
  974. regexp: /^electron\s+([\d.]+)$/i,
  975. select: function (context, node) {
  976. var versionToUse = normalizeElectron(node.version)
  977. var chrome = e2c[versionToUse]
  978. if (!chrome) {
  979. throw new BrowserslistError(
  980. 'Unknown version ' + node.version + ' of electron'
  981. )
  982. }
  983. return ['chrome ' + chrome]
  984. }
  985. },
  986. node_major_version: {
  987. matches: ['version'],
  988. regexp: /^node\s+(\d+)$/i,
  989. select: nodeQuery
  990. },
  991. node_minor_version: {
  992. matches: ['version'],
  993. regexp: /^node\s+(\d+\.\d+)$/i,
  994. select: nodeQuery
  995. },
  996. node_patch_version: {
  997. matches: ['version'],
  998. regexp: /^node\s+(\d+\.\d+\.\d+)$/i,
  999. select: nodeQuery
  1000. },
  1001. current_node: {
  1002. matches: [],
  1003. regexp: /^current\s+node$/i,
  1004. select: function (context) {
  1005. return [env.currentNode(resolve, context)]
  1006. }
  1007. },
  1008. maintained_node: {
  1009. matches: [],
  1010. regexp: /^maintained\s+node\s+versions$/i,
  1011. select: function (context) {
  1012. var now = Date.now()
  1013. var queries = Object.keys(jsEOL)
  1014. .filter(function (key) {
  1015. return (
  1016. now < Date.parse(jsEOL[key].end) &&
  1017. now > Date.parse(jsEOL[key].start) &&
  1018. isEolReleased(key)
  1019. )
  1020. })
  1021. .map(function (key) {
  1022. return 'node ' + key.slice(1)
  1023. })
  1024. return resolve(queries, context)
  1025. }
  1026. },
  1027. phantomjs_1_9: {
  1028. matches: [],
  1029. regexp: /^phantomjs\s+1.9$/i,
  1030. select: function () {
  1031. return ['safari 5']
  1032. }
  1033. },
  1034. phantomjs_2_1: {
  1035. matches: [],
  1036. regexp: /^phantomjs\s+2.1$/i,
  1037. select: function () {
  1038. return ['safari 6']
  1039. }
  1040. },
  1041. browser_version: {
  1042. matches: ['browser', 'version'],
  1043. regexp: /^(\w+)\s+(tp|[\d.]+)$/i,
  1044. select: function (context, node) {
  1045. var version = node.version
  1046. if (/^tp$/i.test(version)) version = 'TP'
  1047. var data = checkName(node.browser, context)
  1048. var alias = normalizeVersion(data, version)
  1049. if (alias) {
  1050. version = alias
  1051. } else {
  1052. if (version.indexOf('.') === -1) {
  1053. alias = version + '.0'
  1054. } else {
  1055. alias = version.replace(/\.0$/, '')
  1056. }
  1057. alias = normalizeVersion(data, alias)
  1058. if (alias) {
  1059. version = alias
  1060. } else if (context.ignoreUnknownVersions) {
  1061. return []
  1062. } else {
  1063. throw new BrowserslistError(
  1064. 'Unknown version ' + version + ' of ' + node.browser
  1065. )
  1066. }
  1067. }
  1068. return [data.name + ' ' + version]
  1069. }
  1070. },
  1071. browserslist_config: {
  1072. matches: [],
  1073. regexp: /^browserslist config$/i,
  1074. select: function (context) {
  1075. return browserslist(undefined, context)
  1076. }
  1077. },
  1078. extends: {
  1079. matches: ['config'],
  1080. regexp: /^extends (.+)$/i,
  1081. select: function (context, node) {
  1082. return resolve(env.loadQueries(context, node.config), context)
  1083. }
  1084. },
  1085. defaults: {
  1086. matches: [],
  1087. regexp: /^defaults$/i,
  1088. select: function (context) {
  1089. return resolve(browserslist.defaults, context)
  1090. }
  1091. },
  1092. dead: {
  1093. matches: [],
  1094. regexp: /^dead$/i,
  1095. select: function (context) {
  1096. var dead = [
  1097. 'Baidu >= 0',
  1098. 'ie <= 11',
  1099. 'ie_mob <= 11',
  1100. 'bb <= 10',
  1101. 'op_mob <= 12.1',
  1102. 'samsung 4'
  1103. ]
  1104. return resolve(dead, context)
  1105. }
  1106. },
  1107. unknown: {
  1108. matches: [],
  1109. regexp: /^(\w+)$/i,
  1110. select: function (context, node) {
  1111. if (byName(node.query, context)) {
  1112. throw new BrowserslistError(
  1113. 'Specify versions in Browserslist query for browser ' + node.query
  1114. )
  1115. } else {
  1116. throw unknownQuery(node.query)
  1117. }
  1118. }
  1119. }
  1120. }
  1121. // Get and convert Can I Use data
  1122. ;(function () {
  1123. for (var name in agents) {
  1124. var browser = agents[name]
  1125. browserslist.data[name] = {
  1126. name: name,
  1127. versions: normalize(agents[name].versions),
  1128. released: normalize(agents[name].versions.slice(0, -3)),
  1129. releaseDate: agents[name].release_date
  1130. }
  1131. fillUsage(browserslist.usage.global, name, browser.usage_global)
  1132. browserslist.versionAliases[name] = {}
  1133. for (var i = 0; i < browser.versions.length; i++) {
  1134. var full = browser.versions[i]
  1135. if (!full) continue
  1136. if (full.indexOf('-') !== -1) {
  1137. var interval = full.split('-')
  1138. for (var j = 0; j < interval.length; j++) {
  1139. browserslist.versionAliases[name][interval[j]] = full
  1140. }
  1141. }
  1142. }
  1143. }
  1144. browserslist.nodeVersions = jsReleases.map(function (release) {
  1145. return release.version
  1146. })
  1147. })()
  1148. module.exports = browserslist