index.js 33 KB

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