You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1038 lines
29 KiB

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