index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. var caniuse = require('caniuse-db/data.json').agents;
  2. var path = require('path');
  3. var fs = require('fs');
  4. var FLOAT = /^\d+(\.\d+)?$/;
  5. function uniq(array) {
  6. var filtered = [];
  7. for ( var i = 0; i < array.length; i++ ) {
  8. if ( filtered.indexOf(array[i]) === -1 ) filtered.push(array[i]);
  9. }
  10. return filtered;
  11. }
  12. function BrowserslistError(message) {
  13. this.name = 'BrowserslistError';
  14. this.message = message || '';
  15. if ( Error.captureStackTrace ) {
  16. Error.captureStackTrace(this, BrowserslistError);
  17. }
  18. }
  19. BrowserslistError.prototype = Error.prototype;
  20. function error(name) {
  21. var obj = new BrowserslistError(name);
  22. obj.browserslist = true;
  23. throw obj;
  24. }
  25. // Helpers
  26. var normalize = function (versions) {
  27. return versions.filter(function (version) {
  28. return typeof version === 'string';
  29. });
  30. };
  31. var fillUsage = function (result, name, data) {
  32. for ( var i in data ) {
  33. result[name + ' ' + i] = data[i];
  34. }
  35. };
  36. // Return array of browsers by selection queries:
  37. //
  38. // browserslist('IE >= 10, IE 8') //=> ['ie 11', 'ie 10', 'ie 8']
  39. var browserslist = function (selections, opts) {
  40. if ( typeof opts === 'undefined' ) opts = { };
  41. if ( typeof selections === 'undefined' || selections === null ) {
  42. if ( process.env.BROWSERSLIST ) {
  43. selections = process.env.BROWSERSLIST;
  44. } else if ( opts.config || process.env.BROWSERSLIST_CONFIG ) {
  45. var file = opts.config || process.env.BROWSERSLIST_CONFIG;
  46. if ( fs.existsSync(file) && fs.statSync(file).isFile() ) {
  47. selections = browserslist.parseConfig( fs.readFileSync(file) );
  48. } else {
  49. error('Can\'t read ' + file + ' config');
  50. }
  51. } else {
  52. var config = browserslist.readConfig(opts.path);
  53. if ( config !== false ) {
  54. selections = config;
  55. } else {
  56. selections = browserslist.defaults;
  57. }
  58. }
  59. }
  60. if ( typeof selections === 'string' ) {
  61. selections = selections.split(/,\s*/);
  62. }
  63. if ( opts.stats || process.env.BROWSERSLIST_STATS ) {
  64. browserslist.usage.custom = { };
  65. var stats = opts.stats || process.env.BROWSERSLIST_STATS;
  66. if ( typeof stats === 'string' ) {
  67. try {
  68. stats = JSON.parse(fs.readFileSync(stats));
  69. } catch (e) {
  70. error('Can\'t read ' + stats);
  71. }
  72. }
  73. if ( 'dataByBrowser' in stats ) {
  74. // Allow to use the data as-is from the caniuse.com website
  75. stats = stats.dataByBrowser;
  76. }
  77. for ( var browser in stats ) {
  78. fillUsage(browserslist.usage.custom, browser, stats[browser]);
  79. }
  80. }
  81. var result = [];
  82. var exclude, query, match, array, used;
  83. selections.forEach(function (selection) {
  84. if ( selection.trim() === '' ) return;
  85. exclude = false;
  86. used = false;
  87. if ( selection.indexOf('not ') === 0 ) {
  88. selection = selection.slice(4);
  89. exclude = true;
  90. }
  91. for ( var i in browserslist.queries ) {
  92. query = browserslist.queries[i];
  93. match = selection.match(query.regexp);
  94. if ( match ) {
  95. array = query.select.apply(browserslist, match.slice(1));
  96. if ( exclude ) {
  97. result = result.filter(function (j) {
  98. return array.indexOf(j) === -1;
  99. });
  100. } else {
  101. result = result.concat(array);
  102. }
  103. used = true;
  104. break;
  105. }
  106. }
  107. if ( !used ) {
  108. error('Unknown browser query `' + selection + '`');
  109. }
  110. });
  111. result = uniq(result);
  112. return result.filter(function (i) {
  113. var version = i.split(' ')[1];
  114. if ( version === '0' ) {
  115. var name = i.split(' ')[0];
  116. return !result.some(function (j) {
  117. return j !== i && j.split(' ')[0] === name;
  118. });
  119. } else {
  120. return true;
  121. }
  122. }).sort(function (name1, name2) {
  123. name1 = name1.split(' ');
  124. name2 = name2.split(' ');
  125. if ( name1[0] === name2[0] ) {
  126. if ( FLOAT.test(name1[1]) && FLOAT.test(name2[1]) ) {
  127. var d = parseFloat(name2[1]) - parseFloat(name1[1]);
  128. if ( d > 0 ) {
  129. return 1;
  130. } else if ( d < 0 ) {
  131. return -1;
  132. } else {
  133. return 0;
  134. }
  135. } else {
  136. return name2[1].localeCompare(name1[1]);
  137. }
  138. } else {
  139. return name1[0].localeCompare(name2[0]);
  140. }
  141. });
  142. };
  143. var normalizeVersion = function (data, version) {
  144. if ( data.versions.indexOf(version) !== -1 ) {
  145. return version;
  146. } else {
  147. return browserslist.versionAliases[data.name][version];
  148. }
  149. };
  150. var loadCountryStatistics = function (country) {
  151. if (!browserslist.usage[country]) {
  152. var usage = { };
  153. var data = require(
  154. 'caniuse-db/region-usage-json/' + country + '.json');
  155. for ( var i in data.data ) {
  156. fillUsage(usage, i, data.data[i]);
  157. }
  158. browserslist.usage[country] = usage;
  159. }
  160. };
  161. // Will be filled by Can I Use data below
  162. browserslist.data = { };
  163. browserslist.usage = {
  164. global: { },
  165. custom: null
  166. };
  167. // Default browsers query
  168. browserslist.defaults = [
  169. '> 1%',
  170. 'last 2 versions',
  171. 'Firefox ESR'
  172. ];
  173. // What browsers will be used in `last n version` query
  174. browserslist.major = ['safari', 'opera', 'ios_saf', 'ie_mob', 'ie',
  175. 'edge', 'firefox', 'chrome'];
  176. // Browser names aliases
  177. browserslist.aliases = {
  178. fx: 'firefox',
  179. ff: 'firefox',
  180. ios: 'ios_saf',
  181. explorer: 'ie',
  182. blackberry: 'bb',
  183. explorermobile: 'ie_mob',
  184. operamini: 'op_mini',
  185. operamobile: 'op_mob',
  186. chromeandroid: 'and_chr',
  187. firefoxandroid: 'and_ff',
  188. ucandroid: 'and_uc'
  189. };
  190. // Aliases to work with joined versions like `ios_saf 7.0-7.1`
  191. browserslist.versionAliases = { };
  192. // Get browser data by alias or case insensitive name
  193. browserslist.byName = function (name) {
  194. name = name.toLowerCase();
  195. name = browserslist.aliases[name] || name;
  196. return browserslist.data[name];
  197. };
  198. // Get browser data by alias or case insensitive name and throw error
  199. // on unknown browser
  200. browserslist.checkName = function (name) {
  201. var data = browserslist.byName(name);
  202. if ( !data ) error('Unknown browser ' + name);
  203. return data;
  204. };
  205. // Find config, read file and parse it
  206. browserslist.readConfig = function (from) {
  207. if ( from === false ) return false;
  208. if ( !fs.readFileSync || !fs.existsSync || !fs.statSync ) return false;
  209. if ( typeof from === 'undefined' ) from = '.';
  210. var dirs = path.resolve(from).split(path.sep);
  211. var config;
  212. while ( dirs.length ) {
  213. config = dirs.concat(['browserslist']).join(path.sep);
  214. if ( fs.existsSync(config) && fs.statSync(config).isFile() ) {
  215. return browserslist.parseConfig( fs.readFileSync(config) );
  216. }
  217. dirs.pop();
  218. }
  219. return false;
  220. };
  221. // Return browsers market coverage
  222. browserslist.coverage = function (browsers, country) {
  223. if (country && country !== 'global') {
  224. country = country.toUpperCase();
  225. loadCountryStatistics(country);
  226. } else {
  227. country = 'global'; // Default value
  228. }
  229. return browsers.reduce(function (all, i) {
  230. var usage = browserslist.usage[country][i];
  231. if (usage === undefined) {
  232. // Sometimes, Caniuse consolidates country usage data into a single
  233. // "version 0" entry. This is usually when there is only 1 version.
  234. usage = browserslist.usage[country][i.replace(/ [\d.]+$/, ' 0')];
  235. }
  236. return all + (usage || 0);
  237. }, 0);
  238. };
  239. // Return array of queries from config content
  240. browserslist.parseConfig = function (string) {
  241. return string.toString()
  242. .replace(/#[^\n]*/g, '')
  243. .split(/\n/)
  244. .map(function (i) {
  245. return i.trim();
  246. }).filter(function (i) {
  247. return i !== '';
  248. });
  249. };
  250. browserslist.queries = {
  251. lastVersions: {
  252. regexp: /^last\s+(\d+)\s+versions?$/i,
  253. select: function (versions) {
  254. var selected = [];
  255. browserslist.major.forEach(function (name) {
  256. var data = browserslist.byName(name);
  257. if ( !data ) return;
  258. var array = data.released.slice(-versions);
  259. array = array.map(function (v) {
  260. return data.name + ' ' + v;
  261. });
  262. selected = selected.concat(array);
  263. });
  264. return selected;
  265. }
  266. },
  267. lastByBrowser: {
  268. regexp: /^last\s+(\d+)\s+(\w+)\s+versions?$/i,
  269. select: function (versions, name) {
  270. var data = browserslist.checkName(name);
  271. return data.released.slice(-versions).map(function (v) {
  272. return data.name + ' ' + v;
  273. });
  274. }
  275. },
  276. globalStatistics: {
  277. regexp: /^>\s*(\d*\.?\d+)%$/,
  278. select: function (popularity) {
  279. popularity = parseFloat(popularity);
  280. var result = [];
  281. for ( var version in browserslist.usage.global ) {
  282. if ( browserslist.usage.global[version] > popularity ) {
  283. result.push(version);
  284. }
  285. }
  286. return result;
  287. }
  288. },
  289. customStatistics: {
  290. regexp: /^>\s*(\d*\.?\d+)%\s+in\s+my\s+stats$/,
  291. select: function (popularity) {
  292. popularity = parseFloat(popularity);
  293. var result = [];
  294. var usage = browserslist.usage.custom;
  295. if ( !usage ) {
  296. error('Custom usage statistics was not provided');
  297. }
  298. for ( var version in usage ) {
  299. if ( usage[version] > popularity ) {
  300. result.push(version);
  301. }
  302. }
  303. return result;
  304. }
  305. },
  306. countryStatistics: {
  307. regexp: /^>\s*(\d*\.?\d+)%\s+in\s+(\w\w)$/,
  308. select: function (popularity, country) {
  309. popularity = parseFloat(popularity);
  310. country = country.toUpperCase();
  311. var result = [];
  312. loadCountryStatistics(country);
  313. var usage = browserslist.usage[country];
  314. for ( var version in usage ) {
  315. if ( usage[version] > popularity ) {
  316. result.push(version);
  317. }
  318. }
  319. return result;
  320. }
  321. },
  322. range: {
  323. regexp: /^(\w+)\s+([\d\.]+)\s*-\s*([\d\.]+)$/i,
  324. select: function (name, from, to) {
  325. var data = browserslist.checkName(name);
  326. from = parseFloat(normalizeVersion(data, from) || from);
  327. to = parseFloat(normalizeVersion(data, to) || to);
  328. var filter = function (v) {
  329. var parsed = parseFloat(v);
  330. return parsed >= from && parsed <= to;
  331. };
  332. return data.released.filter(filter).map(function (v) {
  333. return data.name + ' ' + v;
  334. });
  335. }
  336. },
  337. versions: {
  338. regexp: /^(\w+)\s*(>=?|<=?)\s*([\d\.]+)$/,
  339. select: function (name, sign, version) {
  340. var data = browserslist.checkName(name);
  341. var alias = normalizeVersion(data, version);
  342. if ( alias ) {
  343. version = alias;
  344. }
  345. version = parseFloat(version);
  346. var filter;
  347. if ( sign === '>' ) {
  348. filter = function (v) {
  349. return parseFloat(v) > version;
  350. };
  351. } else if ( sign === '>=' ) {
  352. filter = function (v) {
  353. return parseFloat(v) >= version;
  354. };
  355. } else if ( sign === '<' ) {
  356. filter = function (v) {
  357. return parseFloat(v) < version;
  358. };
  359. } else if ( sign === '<=' ) {
  360. filter = function (v) {
  361. return parseFloat(v) <= version;
  362. };
  363. }
  364. return data.released.filter(filter).map(function (v) {
  365. return data.name + ' ' + v;
  366. });
  367. }
  368. },
  369. esr: {
  370. regexp: /^(firefox|ff|fx)\s+esr$/i,
  371. select: function () {
  372. return ['firefox 45'];
  373. }
  374. },
  375. opMini: {
  376. regexp: /(operamini|op_mini)\s+all/i,
  377. select: function () {
  378. return ['op_mini all'];
  379. }
  380. },
  381. direct: {
  382. regexp: /^(\w+)\s+(tp|[\d\.]+)$/i,
  383. select: function (name, version) {
  384. if ( /tp/i.test(version) ) version = 'TP';
  385. var data = browserslist.checkName(name);
  386. var alias = normalizeVersion(data, version);
  387. if ( alias ) {
  388. version = alias;
  389. } else {
  390. if ( version.indexOf('.') === -1 ) {
  391. alias = version + '.0';
  392. } else if ( /\.0$/.test(version) ) {
  393. alias = version.replace(/\.0$/, '');
  394. }
  395. alias = normalizeVersion(data, alias);
  396. if ( alias ) {
  397. version = alias;
  398. } else {
  399. error('Unknown version ' + version + ' of ' + name);
  400. }
  401. }
  402. return [data.name + ' ' + version];
  403. }
  404. },
  405. defaults: {
  406. regexp: /^defaults$/i,
  407. select: function () {
  408. return browserslist(browserslist.defaults);
  409. }
  410. }
  411. };
  412. // Get and convert Can I Use data
  413. (function () {
  414. for ( var name in caniuse ) {
  415. browserslist.data[name] = {
  416. name: name,
  417. versions: normalize(caniuse[name].versions),
  418. released: normalize(caniuse[name].versions.slice(0, -3))
  419. };
  420. fillUsage(browserslist.usage.global, name, caniuse[name].usage_global);
  421. browserslist.versionAliases[name] = { };
  422. for ( var i = 0; i < caniuse[name].versions.length; i++ ) {
  423. if ( !caniuse[name].versions[i] ) continue;
  424. var full = caniuse[name].versions[i];
  425. if ( full.indexOf('-') !== -1 ) {
  426. var interval = full.split('-');
  427. for ( var j = 0; j < interval.length; j++ ) {
  428. browserslist.versionAliases[name][interval[j]] = full;
  429. }
  430. }
  431. }
  432. }
  433. }());
  434. module.exports = browserslist;