index.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 env = require('./node') // Will load browser.js in webpack
  8. var YEAR = 365.259641 * 24 * 60 * 60 * 1000
  9. var ANDROID_EVERGREEN_FIRST = 37
  10. var QUERY_OR = 1
  11. var QUERY_AND = 2
  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. // Helpers
  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 mapVersions(data, map) {
  204. data.versions = data.versions.map(function (i) {
  205. return map[i] || i
  206. })
  207. data.released = data.released.map(function (i) {
  208. return map[i] || i
  209. })
  210. var fixedDate = {}
  211. for (var i in data.releaseDate) {
  212. fixedDate[map[i] || i] = data.releaseDate[i]
  213. }
  214. data.releaseDate = fixedDate
  215. return data
  216. }
  217. function byName(name, context) {
  218. name = name.toLowerCase()
  219. name = browserslist.aliases[name] || name
  220. if (context.mobileToDesktop && browserslist.desktopNames[name]) {
  221. var desktop = browserslist.data[browserslist.desktopNames[name]]
  222. if (name === 'android') {
  223. return normalizeAndroidData(cloneData(browserslist.data[name]), desktop)
  224. } else {
  225. var cloned = cloneData(desktop)
  226. cloned.name = name
  227. if (name === 'op_mob') {
  228. cloned = mapVersions(cloned, { '10.0-10.1': '10' })
  229. }
  230. return cloned
  231. }
  232. }
  233. return browserslist.data[name]
  234. }
  235. function normalizeAndroidVersions(androidVersions, chromeVersions) {
  236. var firstEvergreen = ANDROID_EVERGREEN_FIRST
  237. var last = chromeVersions[chromeVersions.length - 1]
  238. return androidVersions
  239. .filter(function (version) {
  240. return /^(?:[2-4]\.|[34]$)/.test(version)
  241. })
  242. .concat(chromeVersions.slice(firstEvergreen - last - 1))
  243. }
  244. function normalizeAndroidData(android, chrome) {
  245. android.released = normalizeAndroidVersions(android.released, chrome.released)
  246. android.versions = normalizeAndroidVersions(android.versions, chrome.versions)
  247. return android
  248. }
  249. function checkName(name, context) {
  250. var data = byName(name, context)
  251. if (!data) throw new BrowserslistError('Unknown browser ' + name)
  252. return data
  253. }
  254. function unknownQuery(query) {
  255. return new BrowserslistError(
  256. 'Unknown browser query `' +
  257. query +
  258. '`. ' +
  259. 'Maybe you are using old Browserslist or made typo in query.'
  260. )
  261. }
  262. function filterAndroid(list, versions, context) {
  263. if (context.mobileToDesktop) return list
  264. var released = browserslist.data.android.released
  265. var last = released[released.length - 1]
  266. var diff = last - ANDROID_EVERGREEN_FIRST - versions
  267. if (diff > 0) {
  268. return list.slice(-1)
  269. } else {
  270. return list.slice(diff - 1)
  271. }
  272. }
  273. /**
  274. * Resolves queries into a browser list.
  275. * @param {string|string[]} queries Queries to combine.
  276. * Either an array of queries or a long string of queries.
  277. * @param {object} [context] Optional arguments to
  278. * the select function in `queries`.
  279. * @returns {string[]} A list of browsers
  280. */
  281. function resolve(queries, context) {
  282. if (Array.isArray(queries)) {
  283. queries = flatten(queries.map(parse))
  284. } else {
  285. queries = parse(queries)
  286. }
  287. return queries.reduce(function (result, query, index) {
  288. var selection = query.queryString
  289. var isExclude = selection.indexOf('not ') === 0
  290. if (isExclude) {
  291. if (index === 0) {
  292. throw new BrowserslistError(
  293. 'Write any browsers query (for instance, `defaults`) ' +
  294. 'before `' +
  295. selection +
  296. '`'
  297. )
  298. }
  299. selection = selection.slice(4)
  300. }
  301. for (var i = 0; i < QUERIES.length; i++) {
  302. var type = QUERIES[i]
  303. var match = selection.match(type.regexp)
  304. if (match) {
  305. var args = [context].concat(match.slice(1))
  306. var array = type.select.apply(browserslist, args).map(function (j) {
  307. var parts = j.split(' ')
  308. if (parts[1] === '0') {
  309. return parts[0] + ' ' + byName(parts[0], context).versions[0]
  310. } else {
  311. return j
  312. }
  313. })
  314. switch (query.type) {
  315. case QUERY_AND:
  316. if (isExclude) {
  317. return result.filter(function (j) {
  318. return array.indexOf(j) === -1
  319. })
  320. } else {
  321. return result.filter(function (j) {
  322. return array.indexOf(j) !== -1
  323. })
  324. }
  325. case QUERY_OR:
  326. default:
  327. if (isExclude) {
  328. var filter = {}
  329. array.forEach(function (j) {
  330. filter[j] = true
  331. })
  332. return result.filter(function (j) {
  333. return !filter[j]
  334. })
  335. }
  336. return result.concat(array)
  337. }
  338. }
  339. }
  340. throw unknownQuery(selection)
  341. }, [])
  342. }
  343. var cache = {}
  344. /**
  345. * Return array of browsers by selection queries.
  346. *
  347. * @param {(string|string[])} [queries=browserslist.defaults] Browser queries.
  348. * @param {object} [opts] Options.
  349. * @param {string} [opts.path="."] Path to processed file.
  350. * It will be used to find config files.
  351. * @param {string} [opts.env="production"] Processing environment.
  352. * It will be used to take right
  353. * queries from config file.
  354. * @param {string} [opts.config] Path to config file with queries.
  355. * @param {object} [opts.stats] Custom browser usage statistics
  356. * for "> 1% in my stats" query.
  357. * @param {boolean} [opts.ignoreUnknownVersions=false] Do not throw on unknown
  358. * version in direct query.
  359. * @param {boolean} [opts.dangerousExtend] Disable security checks
  360. * for extend query.
  361. * @param {boolean} [opts.throwOnMissing] Throw error on missing env.
  362. * @param {boolean} [opts.mobileToDesktop] Alias mobile browsers to the desktop
  363. * version when Can I Use doesn't have
  364. * data about the specified version.
  365. * @returns {string[]} Array with browser names in Can I Use.
  366. *
  367. * @example
  368. * browserslist('IE >= 10, IE 8') //=> ['ie 11', 'ie 10', 'ie 8']
  369. */
  370. function browserslist(queries, opts) {
  371. if (typeof opts === 'undefined') opts = {}
  372. if (typeof opts.path === 'undefined') {
  373. opts.path = path.resolve ? path.resolve('.') : '.'
  374. }
  375. if (typeof queries === 'undefined' || queries === null) {
  376. var config = browserslist.loadConfig(opts)
  377. if (config) {
  378. queries = config
  379. } else {
  380. queries = browserslist.defaults
  381. }
  382. }
  383. if (!(typeof queries === 'string' || Array.isArray(queries))) {
  384. throw new BrowserslistError(
  385. 'Browser queries must be an array or string. Got ' + typeof queries + '.'
  386. )
  387. }
  388. var context = {
  389. ignoreUnknownVersions: opts.ignoreUnknownVersions,
  390. dangerousExtend: opts.dangerousExtend,
  391. mobileToDesktop: opts.mobileToDesktop,
  392. path: opts.path,
  393. env: opts.env
  394. }
  395. env.oldDataWarning(browserslist.data)
  396. var stats = env.getStat(opts, browserslist.data)
  397. if (stats) {
  398. context.customUsage = {}
  399. for (var browser in stats) {
  400. fillUsage(context.customUsage, browser, stats[browser])
  401. }
  402. }
  403. var cacheKey = JSON.stringify([queries, context])
  404. if (cache[cacheKey]) return cache[cacheKey]
  405. var result = uniq(resolve(queries, context)).sort(function (name1, name2) {
  406. name1 = name1.split(' ')
  407. name2 = name2.split(' ')
  408. if (name1[0] === name2[0]) {
  409. // assumptions on caniuse data
  410. // 1) version ranges never overlaps
  411. // 2) if version is not a range, it never contains `-`
  412. var version1 = name1[1].split('-')[0]
  413. var version2 = name2[1].split('-')[0]
  414. return compareSemver(version2.split('.'), version1.split('.'))
  415. } else {
  416. return compare(name1[0], name2[0])
  417. }
  418. })
  419. if (!process.env.BROWSERSLIST_DISABLE_CACHE) {
  420. cache[cacheKey] = result
  421. }
  422. return result
  423. }
  424. function parse(queries) {
  425. var qs = []
  426. do {
  427. queries = doMatch(queries, qs)
  428. } while (queries)
  429. return qs
  430. }
  431. function doMatch(string, qs) {
  432. var or = /^(?:,\s*|\s+or\s+)(.*)/i
  433. var and = /^\s+and\s+(.*)/i
  434. return find(string, function (parsed, n, max) {
  435. if (and.test(parsed)) {
  436. qs.unshift({ type: QUERY_AND, queryString: parsed.match(and)[1] })
  437. return true
  438. } else if (or.test(parsed)) {
  439. qs.unshift({ type: QUERY_OR, queryString: parsed.match(or)[1] })
  440. return true
  441. } else if (n === max) {
  442. qs.unshift({ type: QUERY_OR, queryString: parsed.trim() })
  443. return true
  444. }
  445. return false
  446. })
  447. }
  448. function find(string, predicate) {
  449. for (var n = 1, max = string.length; n <= max; n++) {
  450. var parsed = string.substr(-n, n)
  451. if (predicate(parsed, n, max)) {
  452. return string.slice(0, -n)
  453. }
  454. }
  455. return ''
  456. }
  457. function flatten(array) {
  458. if (!Array.isArray(array)) return [array]
  459. return array.reduce(function (a, b) {
  460. return a.concat(flatten(b))
  461. }, [])
  462. }
  463. // Will be filled by Can I Use data below
  464. browserslist.cache = {}
  465. browserslist.data = {}
  466. browserslist.usage = {
  467. global: {},
  468. custom: null
  469. }
  470. // Default browsers query
  471. browserslist.defaults = ['> 0.5%', 'last 2 versions', 'Firefox ESR', 'not dead']
  472. // Browser names aliases
  473. browserslist.aliases = {
  474. fx: 'firefox',
  475. ff: 'firefox',
  476. ios: 'ios_saf',
  477. explorer: 'ie',
  478. blackberry: 'bb',
  479. explorermobile: 'ie_mob',
  480. operamini: 'op_mini',
  481. operamobile: 'op_mob',
  482. chromeandroid: 'and_chr',
  483. firefoxandroid: 'and_ff',
  484. ucandroid: 'and_uc',
  485. qqandroid: 'and_qq'
  486. }
  487. // Can I Use only provides a few versions for some browsers (e.g. and_chr).
  488. // Fallback to a similar browser for unknown versions
  489. browserslist.desktopNames = {
  490. and_chr: 'chrome',
  491. and_ff: 'firefox',
  492. ie_mob: 'ie',
  493. op_mob: 'opera',
  494. android: 'chrome' // has extra processing logic
  495. }
  496. // Aliases to work with joined versions like `ios_saf 7.0-7.1`
  497. browserslist.versionAliases = {}
  498. browserslist.clearCaches = env.clearCaches
  499. browserslist.parseConfig = env.parseConfig
  500. browserslist.readConfig = env.readConfig
  501. browserslist.findConfig = env.findConfig
  502. browserslist.loadConfig = env.loadConfig
  503. /**
  504. * Return browsers market coverage.
  505. *
  506. * @param {string[]} browsers Browsers names in Can I Use.
  507. * @param {string|object} [stats="global"] Which statistics should be used.
  508. * Country code or custom statistics.
  509. * Pass `"my stats"` to load statistics
  510. * from Browserslist files.
  511. *
  512. * @return {number} Total market coverage for all selected browsers.
  513. *
  514. * @example
  515. * browserslist.coverage(browserslist('> 1% in US'), 'US') //=> 83.1
  516. */
  517. browserslist.coverage = function (browsers, stats) {
  518. var data
  519. if (typeof stats === 'undefined') {
  520. data = browserslist.usage.global
  521. } else if (stats === 'my stats') {
  522. var opts = {}
  523. opts.path = path.resolve ? path.resolve('.') : '.'
  524. var customStats = env.getStat(opts)
  525. if (!customStats) {
  526. throw new BrowserslistError('Custom usage statistics was not provided')
  527. }
  528. data = {}
  529. for (var browser in customStats) {
  530. fillUsage(data, browser, customStats[browser])
  531. }
  532. } else if (typeof stats === 'string') {
  533. if (stats.length > 2) {
  534. stats = stats.toLowerCase()
  535. } else {
  536. stats = stats.toUpperCase()
  537. }
  538. env.loadCountry(browserslist.usage, stats, browserslist.data)
  539. data = browserslist.usage[stats]
  540. } else {
  541. if ('dataByBrowser' in stats) {
  542. stats = stats.dataByBrowser
  543. }
  544. data = {}
  545. for (var name in stats) {
  546. for (var version in stats[name]) {
  547. data[name + ' ' + version] = stats[name][version]
  548. }
  549. }
  550. }
  551. return browsers.reduce(function (all, i) {
  552. var usage = data[i]
  553. if (usage === undefined) {
  554. usage = data[i.replace(/ \S+$/, ' 0')]
  555. }
  556. return all + (usage || 0)
  557. }, 0)
  558. }
  559. function nodeQuery(context, version) {
  560. var matched = browserslist.nodeVersions.filter(function (i) {
  561. return isVersionsMatch(i, version)
  562. })
  563. if (matched.length === 0) {
  564. if (context.ignoreUnknownVersions) {
  565. return []
  566. } else {
  567. throw new BrowserslistError('Unknown version ' + version + ' of Node.js')
  568. }
  569. }
  570. return ['node ' + matched[matched.length - 1]]
  571. }
  572. function sinceQuery(context, year, month, date) {
  573. year = parseInt(year)
  574. month = parseInt(month || '01') - 1
  575. date = parseInt(date || '01')
  576. return filterByYear(Date.UTC(year, month, date, 0, 0, 0), context)
  577. }
  578. function coverQuery(context, coverage, statMode) {
  579. coverage = parseFloat(coverage)
  580. var usage = browserslist.usage.global
  581. if (statMode) {
  582. if (statMode.match(/^my\s+stats$/i)) {
  583. if (!context.customUsage) {
  584. throw new BrowserslistError('Custom usage statistics was not provided')
  585. }
  586. usage = context.customUsage
  587. } else {
  588. var place
  589. if (statMode.length === 2) {
  590. place = statMode.toUpperCase()
  591. } else {
  592. place = statMode.toLowerCase()
  593. }
  594. env.loadCountry(browserslist.usage, place, browserslist.data)
  595. usage = browserslist.usage[place]
  596. }
  597. }
  598. var versions = Object.keys(usage).sort(function (a, b) {
  599. return usage[b] - usage[a]
  600. })
  601. var coveraged = 0
  602. var result = []
  603. var version
  604. for (var i = 0; i < versions.length; i++) {
  605. version = versions[i]
  606. if (usage[version] === 0) break
  607. coveraged += usage[version]
  608. result.push(version)
  609. if (coveraged >= coverage) break
  610. }
  611. return result
  612. }
  613. var QUERIES = [
  614. {
  615. regexp: /^last\s+(\d+)\s+major\s+versions?$/i,
  616. select: function (context, versions) {
  617. return Object.keys(agents).reduce(function (selected, name) {
  618. var data = byName(name, context)
  619. if (!data) return selected
  620. var list = getMajorVersions(data.released, versions)
  621. list = list.map(nameMapper(data.name))
  622. if (data.name === 'android') {
  623. list = filterAndroid(list, versions, context)
  624. }
  625. return selected.concat(list)
  626. }, [])
  627. }
  628. },
  629. {
  630. regexp: /^last\s+(\d+)\s+versions?$/i,
  631. select: function (context, versions) {
  632. return Object.keys(agents).reduce(function (selected, name) {
  633. var data = byName(name, context)
  634. if (!data) return selected
  635. var list = data.released.slice(-versions)
  636. list = list.map(nameMapper(data.name))
  637. if (data.name === 'android') {
  638. list = filterAndroid(list, versions, context)
  639. }
  640. return selected.concat(list)
  641. }, [])
  642. }
  643. },
  644. {
  645. regexp: /^last\s+(\d+)\s+electron\s+major\s+versions?$/i,
  646. select: function (context, versions) {
  647. var validVersions = getMajorVersions(Object.keys(e2c), versions)
  648. return validVersions.map(function (i) {
  649. return 'chrome ' + e2c[i]
  650. })
  651. }
  652. },
  653. {
  654. regexp: /^last\s+(\d+)\s+node\s+major\s+versions?$/i,
  655. select: function (context, versions) {
  656. return getMajorVersions(browserslist.nodeVersions, versions).map(
  657. function (version) {
  658. return 'node ' + version
  659. }
  660. )
  661. }
  662. },
  663. {
  664. regexp: /^last\s+(\d+)\s+(\w+)\s+major\s+versions?$/i,
  665. select: function (context, versions, name) {
  666. var data = checkName(name, context)
  667. var validVersions = getMajorVersions(data.released, versions)
  668. var list = validVersions.map(nameMapper(data.name))
  669. if (data.name === 'android') {
  670. list = filterAndroid(list, versions, context)
  671. }
  672. return list
  673. }
  674. },
  675. {
  676. regexp: /^last\s+(\d+)\s+electron\s+versions?$/i,
  677. select: function (context, versions) {
  678. return Object.keys(e2c)
  679. .slice(-versions)
  680. .map(function (i) {
  681. return 'chrome ' + e2c[i]
  682. })
  683. }
  684. },
  685. {
  686. regexp: /^last\s+(\d+)\s+node\s+versions?$/i,
  687. select: function (context, versions) {
  688. return browserslist.nodeVersions.slice(-versions).map(function (version) {
  689. return 'node ' + version
  690. })
  691. }
  692. },
  693. {
  694. regexp: /^last\s+(\d+)\s+(\w+)\s+versions?$/i,
  695. select: function (context, versions, name) {
  696. var data = checkName(name, context)
  697. var list = data.released.slice(-versions).map(nameMapper(data.name))
  698. if (data.name === 'android') {
  699. list = filterAndroid(list, versions, context)
  700. }
  701. return list
  702. }
  703. },
  704. {
  705. regexp: /^unreleased\s+versions$/i,
  706. select: function (context) {
  707. return Object.keys(agents).reduce(function (selected, name) {
  708. var data = byName(name, context)
  709. if (!data) return selected
  710. var list = data.versions.filter(function (v) {
  711. return data.released.indexOf(v) === -1
  712. })
  713. list = list.map(nameMapper(data.name))
  714. return selected.concat(list)
  715. }, [])
  716. }
  717. },
  718. {
  719. regexp: /^unreleased\s+electron\s+versions?$/i,
  720. select: function () {
  721. return []
  722. }
  723. },
  724. {
  725. regexp: /^unreleased\s+(\w+)\s+versions?$/i,
  726. select: function (context, name) {
  727. var data = checkName(name, context)
  728. return data.versions
  729. .filter(function (v) {
  730. return data.released.indexOf(v) === -1
  731. })
  732. .map(nameMapper(data.name))
  733. }
  734. },
  735. {
  736. regexp: /^last\s+(\d*.?\d+)\s+years?$/i,
  737. select: function (context, years) {
  738. return filterByYear(Date.now() - YEAR * years, context)
  739. }
  740. },
  741. {
  742. regexp: /^since (\d+)$/i,
  743. select: sinceQuery
  744. },
  745. {
  746. regexp: /^since (\d+)-(\d+)$/i,
  747. select: sinceQuery
  748. },
  749. {
  750. regexp: /^since (\d+)-(\d+)-(\d+)$/i,
  751. select: sinceQuery
  752. },
  753. {
  754. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%$/,
  755. select: function (context, sign, popularity) {
  756. popularity = parseFloat(popularity)
  757. var usage = browserslist.usage.global
  758. return Object.keys(usage).reduce(function (result, version) {
  759. if (sign === '>') {
  760. if (usage[version] > popularity) {
  761. result.push(version)
  762. }
  763. } else if (sign === '<') {
  764. if (usage[version] < popularity) {
  765. result.push(version)
  766. }
  767. } else if (sign === '<=') {
  768. if (usage[version] <= popularity) {
  769. result.push(version)
  770. }
  771. } else if (usage[version] >= popularity) {
  772. result.push(version)
  773. }
  774. return result
  775. }, [])
  776. }
  777. },
  778. {
  779. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%\s+in\s+my\s+stats$/,
  780. select: function (context, sign, popularity) {
  781. popularity = parseFloat(popularity)
  782. if (!context.customUsage) {
  783. throw new BrowserslistError('Custom usage statistics was not provided')
  784. }
  785. var usage = context.customUsage
  786. return Object.keys(usage).reduce(function (result, version) {
  787. var percentage = usage[version]
  788. if (percentage == null) {
  789. return result
  790. }
  791. if (sign === '>') {
  792. if (percentage > popularity) {
  793. result.push(version)
  794. }
  795. } else if (sign === '<') {
  796. if (percentage < popularity) {
  797. result.push(version)
  798. }
  799. } else if (sign === '<=') {
  800. if (percentage <= popularity) {
  801. result.push(version)
  802. }
  803. } else if (percentage >= popularity) {
  804. result.push(version)
  805. }
  806. return result
  807. }, [])
  808. }
  809. },
  810. {
  811. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%\s+in\s+(\S+)\s+stats$/,
  812. select: function (context, sign, popularity, name) {
  813. popularity = parseFloat(popularity)
  814. var stats = env.loadStat(context, name, browserslist.data)
  815. if (stats) {
  816. context.customUsage = {}
  817. for (var browser in stats) {
  818. fillUsage(context.customUsage, browser, stats[browser])
  819. }
  820. }
  821. if (!context.customUsage) {
  822. throw new BrowserslistError('Custom usage statistics was not provided')
  823. }
  824. var usage = context.customUsage
  825. return Object.keys(usage).reduce(function (result, version) {
  826. var percentage = usage[version]
  827. if (percentage == null) {
  828. return result
  829. }
  830. if (sign === '>') {
  831. if (percentage > popularity) {
  832. result.push(version)
  833. }
  834. } else if (sign === '<') {
  835. if (percentage < popularity) {
  836. result.push(version)
  837. }
  838. } else if (sign === '<=') {
  839. if (percentage <= popularity) {
  840. result.push(version)
  841. }
  842. } else if (percentage >= popularity) {
  843. result.push(version)
  844. }
  845. return result
  846. }, [])
  847. }
  848. },
  849. {
  850. regexp: /^(>=?|<=?)\s*(\d+|\d+\.\d+|\.\d+)%\s+in\s+((alt-)?\w\w)$/,
  851. select: function (context, sign, popularity, place) {
  852. popularity = parseFloat(popularity)
  853. if (place.length === 2) {
  854. place = place.toUpperCase()
  855. } else {
  856. place = place.toLowerCase()
  857. }
  858. env.loadCountry(browserslist.usage, place, browserslist.data)
  859. var usage = browserslist.usage[place]
  860. return Object.keys(usage).reduce(function (result, version) {
  861. var percentage = usage[version]
  862. if (percentage == null) {
  863. return result
  864. }
  865. if (sign === '>') {
  866. if (percentage > popularity) {
  867. result.push(version)
  868. }
  869. } else if (sign === '<') {
  870. if (percentage < popularity) {
  871. result.push(version)
  872. }
  873. } else if (sign === '<=') {
  874. if (percentage <= popularity) {
  875. result.push(version)
  876. }
  877. } else if (percentage >= popularity) {
  878. result.push(version)
  879. }
  880. return result
  881. }, [])
  882. }
  883. },
  884. {
  885. regexp: /^cover\s+(\d+|\d+\.\d+|\.\d+)%$/i,
  886. select: coverQuery
  887. },
  888. {
  889. regexp: /^cover\s+(\d+|\d+\.\d+|\.\d+)%\s+in\s+(my\s+stats|(alt-)?\w\w)$/i,
  890. select: coverQuery
  891. },
  892. {
  893. regexp: /^supports\s+([\w-]+)$/,
  894. select: function (context, feature) {
  895. env.loadFeature(browserslist.cache, feature)
  896. var features = browserslist.cache[feature]
  897. return Object.keys(features).reduce(function (result, version) {
  898. var flags = features[version]
  899. if (flags.indexOf('y') >= 0 || flags.indexOf('a') >= 0) {
  900. result.push(version)
  901. }
  902. return result
  903. }, [])
  904. }
  905. },
  906. {
  907. regexp: /^electron\s+([\d.]+)\s*-\s*([\d.]+)$/i,
  908. select: function (context, from, to) {
  909. var fromToUse = normalizeElectron(from)
  910. var toToUse = normalizeElectron(to)
  911. if (!e2c[fromToUse]) {
  912. throw new BrowserslistError('Unknown version ' + from + ' of electron')
  913. }
  914. if (!e2c[toToUse]) {
  915. throw new BrowserslistError('Unknown version ' + to + ' of electron')
  916. }
  917. from = parseFloat(from)
  918. to = parseFloat(to)
  919. return Object.keys(e2c)
  920. .filter(function (i) {
  921. var parsed = parseFloat(i)
  922. return parsed >= from && parsed <= to
  923. })
  924. .map(function (i) {
  925. return 'chrome ' + e2c[i]
  926. })
  927. }
  928. },
  929. {
  930. regexp: /^node\s+([\d.]+)\s*-\s*([\d.]+)$/i,
  931. select: function (context, from, to) {
  932. return browserslist.nodeVersions
  933. .filter(semverFilterLoose('>=', from))
  934. .filter(semverFilterLoose('<=', to))
  935. .map(function (v) {
  936. return 'node ' + v
  937. })
  938. }
  939. },
  940. {
  941. regexp: /^(\w+)\s+([\d.]+)\s*-\s*([\d.]+)$/i,
  942. select: function (context, name, from, to) {
  943. var data = checkName(name, context)
  944. from = parseFloat(normalizeVersion(data, from) || from)
  945. to = parseFloat(normalizeVersion(data, to) || to)
  946. function filter(v) {
  947. var parsed = parseFloat(v)
  948. return parsed >= from && parsed <= to
  949. }
  950. return data.released.filter(filter).map(nameMapper(data.name))
  951. }
  952. },
  953. {
  954. regexp: /^electron\s*(>=?|<=?)\s*([\d.]+)$/i,
  955. select: function (context, sign, version) {
  956. var versionToUse = normalizeElectron(version)
  957. return Object.keys(e2c)
  958. .filter(generateFilter(sign, versionToUse))
  959. .map(function (i) {
  960. return 'chrome ' + e2c[i]
  961. })
  962. }
  963. },
  964. {
  965. regexp: /^node\s*(>=?|<=?)\s*([\d.]+)$/i,
  966. select: function (context, sign, version) {
  967. return browserslist.nodeVersions
  968. .filter(generateSemverFilter(sign, version))
  969. .map(function (v) {
  970. return 'node ' + v
  971. })
  972. }
  973. },
  974. {
  975. regexp: /^(\w+)\s*(>=?|<=?)\s*([\d.]+)$/,
  976. select: function (context, name, sign, version) {
  977. var data = checkName(name, context)
  978. var alias = browserslist.versionAliases[data.name][version]
  979. if (alias) {
  980. version = alias
  981. }
  982. return data.released
  983. .filter(generateFilter(sign, version))
  984. .map(function (v) {
  985. return data.name + ' ' + v
  986. })
  987. }
  988. },
  989. {
  990. regexp: /^(firefox|ff|fx)\s+esr$/i,
  991. select: function () {
  992. return ['firefox 91']
  993. }
  994. },
  995. {
  996. regexp: /(operamini|op_mini)\s+all/i,
  997. select: function () {
  998. return ['op_mini all']
  999. }
  1000. },
  1001. {
  1002. regexp: /^electron\s+([\d.]+)$/i,
  1003. select: function (context, version) {
  1004. var versionToUse = normalizeElectron(version)
  1005. var chrome = e2c[versionToUse]
  1006. if (!chrome) {
  1007. throw new BrowserslistError(
  1008. 'Unknown version ' + version + ' of electron'
  1009. )
  1010. }
  1011. return ['chrome ' + chrome]
  1012. }
  1013. },
  1014. {
  1015. regexp: /^node\s+(\d+)$/i,
  1016. select: nodeQuery
  1017. },
  1018. {
  1019. regexp: /^node\s+(\d+\.\d+)$/i,
  1020. select: nodeQuery
  1021. },
  1022. {
  1023. regexp: /^node\s+(\d+\.\d+\.\d+)$/i,
  1024. select: nodeQuery
  1025. },
  1026. {
  1027. regexp: /^current\s+node$/i,
  1028. select: function (context) {
  1029. return [env.currentNode(resolve, context)]
  1030. }
  1031. },
  1032. {
  1033. regexp: /^maintained\s+node\s+versions$/i,
  1034. select: function (context) {
  1035. var now = Date.now()
  1036. var queries = Object.keys(jsEOL)
  1037. .filter(function (key) {
  1038. return (
  1039. now < Date.parse(jsEOL[key].end) &&
  1040. now > Date.parse(jsEOL[key].start) &&
  1041. isEolReleased(key)
  1042. )
  1043. })
  1044. .map(function (key) {
  1045. return 'node ' + key.slice(1)
  1046. })
  1047. return resolve(queries, context)
  1048. }
  1049. },
  1050. {
  1051. regexp: /^phantomjs\s+1.9$/i,
  1052. select: function () {
  1053. return ['safari 5']
  1054. }
  1055. },
  1056. {
  1057. regexp: /^phantomjs\s+2.1$/i,
  1058. select: function () {
  1059. return ['safari 6']
  1060. }
  1061. },
  1062. {
  1063. regexp: /^(\w+)\s+(tp|[\d.]+)$/i,
  1064. select: function (context, name, version) {
  1065. if (/^tp$/i.test(version)) version = 'TP'
  1066. var data = checkName(name, context)
  1067. var alias = normalizeVersion(data, version)
  1068. if (alias) {
  1069. version = alias
  1070. } else {
  1071. if (version.indexOf('.') === -1) {
  1072. alias = version + '.0'
  1073. } else {
  1074. alias = version.replace(/\.0$/, '')
  1075. }
  1076. alias = normalizeVersion(data, alias)
  1077. if (alias) {
  1078. version = alias
  1079. } else if (context.ignoreUnknownVersions) {
  1080. return []
  1081. } else {
  1082. throw new BrowserslistError(
  1083. 'Unknown version ' + version + ' of ' + name
  1084. )
  1085. }
  1086. }
  1087. return [data.name + ' ' + version]
  1088. }
  1089. },
  1090. {
  1091. regexp: /^browserslist config$/i,
  1092. select: function (context) {
  1093. return browserslist(undefined, context)
  1094. }
  1095. },
  1096. {
  1097. regexp: /^extends (.+)$/i,
  1098. select: function (context, name) {
  1099. return resolve(env.loadQueries(context, name), context)
  1100. }
  1101. },
  1102. {
  1103. regexp: /^defaults$/i,
  1104. select: function (context) {
  1105. return resolve(browserslist.defaults, context)
  1106. }
  1107. },
  1108. {
  1109. regexp: /^dead$/i,
  1110. select: function (context) {
  1111. var dead = [
  1112. 'Baidu >= 0',
  1113. 'ie <= 10',
  1114. 'ie_mob <= 11',
  1115. 'bb <= 10',
  1116. 'op_mob <= 12.1',
  1117. 'samsung 4'
  1118. ]
  1119. return resolve(dead, context)
  1120. }
  1121. },
  1122. {
  1123. regexp: /^(\w+)$/i,
  1124. select: function (context, name) {
  1125. if (byName(name, context)) {
  1126. throw new BrowserslistError(
  1127. 'Specify versions in Browserslist query for browser ' + name
  1128. )
  1129. } else {
  1130. throw unknownQuery(name)
  1131. }
  1132. }
  1133. }
  1134. ]
  1135. // Get and convert Can I Use data
  1136. ;(function () {
  1137. for (var name in agents) {
  1138. var browser = agents[name]
  1139. browserslist.data[name] = {
  1140. name: name,
  1141. versions: normalize(agents[name].versions),
  1142. released: normalize(agents[name].versions.slice(0, -3)),
  1143. releaseDate: agents[name].release_date
  1144. }
  1145. fillUsage(browserslist.usage.global, name, browser.usage_global)
  1146. browserslist.versionAliases[name] = {}
  1147. for (var i = 0; i < browser.versions.length; i++) {
  1148. var full = browser.versions[i]
  1149. if (!full) continue
  1150. if (full.indexOf('-') !== -1) {
  1151. var interval = full.split('-')
  1152. for (var j = 0; j < interval.length; j++) {
  1153. browserslist.versionAliases[name][interval[j]] = full
  1154. }
  1155. }
  1156. }
  1157. }
  1158. browserslist.versionAliases.op_mob['59'] = '58'
  1159. browserslist.nodeVersions = jsReleases.map(function (release) {
  1160. return release.version
  1161. })
  1162. })()
  1163. module.exports = browserslist