index.js 33 KB

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