index.js 32 KB

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