Nessuna descrizione

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. var caniuse = require('caniuse-db/data').agents;
  2. var path = require('path');
  3. var fs = require('fs');
  4. var uniq = function (array) {
  5. var filtered = [];
  6. for ( var i = 0; i < array.length; i++ ) {
  7. if ( filtered.indexOf(array[i]) === -1 ) filtered.push(array[i]);
  8. }
  9. return filtered;
  10. };
  11. // Return array of browsers by selection queries:
  12. //
  13. // browserslist('IE >= 10, IE 8') //=> ['ie 11', 'ie 10', 'ie 8']
  14. var browserslist = function (selections, opts) {
  15. if ( typeof opts === 'undefined' ) opts = { };
  16. if ( typeof selections === 'undefined' || selections === null ) {
  17. if ( process.env.BROWSERSLIST ) {
  18. selections = process.env.BROWSERSLIST;
  19. } else if ( opts.config || process.env.BROWSERSLIST_CONFIG ) {
  20. var file = opts.config || process.env.BROWSERSLIST_CONFIG;
  21. if ( fs.existsSync(file) && fs.statSync(file).isFile() ) {
  22. selections = browserslist.parseConfig( fs.readFileSync(file) );
  23. } else {
  24. throw 'Can\'t read ' + file + ' config';
  25. }
  26. } else {
  27. var config = browserslist.readConfig(opts.path);
  28. if ( config !== false ) {
  29. selections = config;
  30. } else {
  31. selections = browserslist.defaults;
  32. }
  33. }
  34. }
  35. if ( typeof selections === 'string' ) {
  36. selections = selections.split(/,\s*/);
  37. }
  38. var result = [];
  39. var query, match, array, used;
  40. selections.forEach(function (selection) {
  41. if ( selection.trim() === '' ) return;
  42. used = false;
  43. for ( var i in browserslist.queries ) {
  44. query = browserslist.queries[i];
  45. match = selection.match(query.regexp);
  46. if ( match ) {
  47. array = query.select.apply(browserslist, match.slice(1));
  48. result = result.concat(array);
  49. used = true;
  50. break;
  51. }
  52. }
  53. if ( !used ) {
  54. throw 'Unknown browser query `' + selection + '`';
  55. }
  56. });
  57. return uniq(result).sort(function (name1, name2) {
  58. name1 = name1.split(' ');
  59. name2 = name2.split(' ');
  60. if ( name1[0] === name2[0] ) {
  61. var d = parseFloat(name2[1]) - parseFloat(name1[1]);
  62. if ( d > 0 ) {
  63. return 1;
  64. } else if ( d < 0 ) {
  65. return -1;
  66. } else {
  67. return 0;
  68. }
  69. } else {
  70. return name1[0].localeCompare(name2[0]);
  71. }
  72. });
  73. };
  74. // Helpers
  75. var normalizeVersion = function (data, version) {
  76. if ( data.versions.indexOf(version) !== -1 ) {
  77. return version;
  78. } else {
  79. var alias = browserslist.versionAliases[data.name][version];
  80. if ( alias ) return alias;
  81. }
  82. };
  83. var normalize = function (versions) {
  84. return versions.filter(function (version) {
  85. return typeof version === 'string';
  86. });
  87. };
  88. var fillUsage = function (result, name, data) {
  89. for ( var i in data ) {
  90. result[name + ' ' + i] = data[i];
  91. }
  92. };
  93. // Will be filled by Can I Use data below
  94. browserslist.data = { };
  95. browserslist.usage = {
  96. global: { }
  97. };
  98. // Default browsers query
  99. browserslist.defaults = [
  100. '> 1%',
  101. 'last 2 versions',
  102. 'Firefox ESR',
  103. 'Opera 12.1'
  104. ];
  105. // What browsers will be used in `last n version` query
  106. browserslist.major = ['safari', 'opera', 'ios_saf', 'ie_mob', 'ie',
  107. 'firefox', 'chrome'];
  108. // Browser names aliases
  109. browserslist.aliases = {
  110. fx: 'firefox',
  111. ff: 'firefox',
  112. ios: 'ios_saf',
  113. explorer: 'ie',
  114. blackberry: 'bb',
  115. explorermobile: 'ie_mob',
  116. operamini: 'op_mini',
  117. operamobile: 'op_mob',
  118. chromeandroid: 'and_chr',
  119. firefoxandroid: 'and_ff'
  120. };
  121. // Aliases ot work with joined versions like `ios_saf 7.0-7.1`
  122. browserslist.versionAliases = { };
  123. // Get browser data by alias or case insensitive name
  124. browserslist.byName = function (name) {
  125. name = name.toLowerCase();
  126. name = browserslist.aliases[name] || name;
  127. return browserslist.data[name];
  128. };
  129. // Get browser data by alias or case insensitive name and throw error
  130. // on unknown browser
  131. browserslist.checkName = function (name) {
  132. var data = browserslist.byName(name);
  133. if ( !data ) throw 'Unknown browser ' + name;
  134. return data;
  135. };
  136. // Find config, read file and parse it
  137. browserslist.readConfig = function (from) {
  138. if ( from === false ) return false;
  139. if ( !fs.readFileSync ) return false;
  140. if ( typeof from === 'undefined' ) from = '.';
  141. var dirs = path.resolve(from).split(path.sep);
  142. var config;
  143. while ( dirs.length ) {
  144. config = dirs.concat(['browserslist']).join(path.sep);
  145. if ( fs.existsSync(config) && fs.statSync(config).isFile() ) {
  146. return browserslist.parseConfig( fs.readFileSync(config) );
  147. }
  148. dirs.pop();
  149. }
  150. return false;
  151. };
  152. // Return array of queries from config content
  153. browserslist.parseConfig = function (string) {
  154. return string.toString()
  155. .replace(/#[^\n]*/g, '')
  156. .split(/\n/)
  157. .map(function (i) {
  158. return i.trim();
  159. })
  160. .filter(function (i) {
  161. return i !== '';
  162. });
  163. };
  164. browserslist.queries = {
  165. lastVersions: {
  166. regexp: /^last (\d+) versions?$/i,
  167. select: function (versions) {
  168. var selected = [];
  169. browserslist.major.forEach(function (name) {
  170. var data = browserslist.byName(name);
  171. if ( !data ) return;
  172. var array = data.released.slice(-versions);
  173. array = array.map(function (v) {
  174. return data.name + ' ' + v;
  175. });
  176. selected = selected.concat(array);
  177. });
  178. return selected;
  179. }
  180. },
  181. lastByBrowser: {
  182. regexp: /^last (\d+) (\w+) versions?$/i,
  183. select: function (versions, name) {
  184. var data = browserslist.checkName(name);
  185. return data.released.slice(-versions).map(function (v) {
  186. return data.name + ' ' + v;
  187. });
  188. }
  189. },
  190. globalStatistics: {
  191. regexp: /^> (\d+\.?\d*)%$/,
  192. select: function (popularity) {
  193. popularity = parseFloat(popularity);
  194. var result = [];
  195. for ( var version in browserslist.usage.global ) {
  196. if ( browserslist.usage.global[version] > popularity ) {
  197. result.push(version);
  198. }
  199. }
  200. return result;
  201. }
  202. },
  203. countryStatistics: {
  204. regexp: /^> (\d+\.?\d*)% in (\w\w)$/,
  205. select: function (popularity, country) {
  206. popularity = parseFloat(popularity);
  207. country = country.toUpperCase();
  208. var result = [];
  209. var usage = browserslist.usage[country];
  210. if ( !usage ) {
  211. usage = { };
  212. var data = require('caniuse-db/region-usage-json/' + country);
  213. for ( var i in data.data ) {
  214. fillUsage(usage, i, data.data[i]);
  215. }
  216. browserslist.usage[country] = usage;
  217. }
  218. for ( var version in usage ) {
  219. if ( usage[version] > popularity ) {
  220. result.push(version);
  221. }
  222. }
  223. return result;
  224. }
  225. },
  226. versions: {
  227. regexp: /^(\w+) (>=?|<=?)\s*([\d\.]+)/,
  228. select: function (name, sign, version) {
  229. var data = browserslist.checkName(name);
  230. var alias = normalizeVersion(data, version);
  231. if ( alias ) {
  232. version = alias;
  233. }
  234. version = parseFloat(version);
  235. var filter;
  236. if ( sign === '>' ) {
  237. filter = function (v) {
  238. return parseFloat(v) > version;
  239. };
  240. } else if ( sign === '>=' ) {
  241. filter = function (v) {
  242. return parseFloat(v) >= version;
  243. };
  244. } else if ( sign === '<' ) {
  245. filter = function (v) {
  246. return parseFloat(v) < version;
  247. };
  248. } else if ( sign === '<=' ) {
  249. filter = function (v) {
  250. return parseFloat(v) <= version;
  251. };
  252. }
  253. return data.released.filter(filter).map(function (v) {
  254. return data.name + ' ' + v;
  255. });
  256. }
  257. },
  258. esr: {
  259. regexp: /^(firefox|ff|fx) esr$/i,
  260. select: function () {
  261. return ['firefox 31'];
  262. }
  263. },
  264. direct: {
  265. regexp: /^(\w+) ([\d\.]+)$/,
  266. select: function (name, version) {
  267. var data = browserslist.checkName(name);
  268. var alias = normalizeVersion(data, version);
  269. if ( alias ) {
  270. version = alias;
  271. } else {
  272. if ( version.indexOf('.') === -1 ) {
  273. alias = version + '.0';
  274. } else if ( /\.0$/.test(version) ) {
  275. alias = version.replace(/\.0$/, '');
  276. }
  277. alias = normalizeVersion(data, alias);
  278. if ( alias ) {
  279. version = alias;
  280. } else {
  281. throw 'Unknown version ' + version + ' of ' + name;
  282. }
  283. }
  284. return [data.name + ' ' + version];
  285. }
  286. }
  287. };
  288. // Get and convert Can I Use data
  289. (function () {
  290. for ( var name in caniuse ) {
  291. browserslist.data[name] = {
  292. name: name,
  293. versions: normalize(caniuse[name].versions),
  294. released: normalize(caniuse[name].versions.slice(0, -3))
  295. };
  296. fillUsage(browserslist.usage.global, name, caniuse[name].usage_global);
  297. browserslist.versionAliases[name] = { };
  298. for ( var i = 0; i < caniuse[name].versions.length; i++ ) {
  299. if ( !caniuse[name].versions[i] ) continue;
  300. var full = caniuse[name].versions[i];
  301. if ( full.indexOf('-') !== -1 ) {
  302. var interval = full.split('-');
  303. for ( var j = 0; j < interval.length; j++ ) {
  304. browserslist.versionAliases[name][ interval[j] ] = full;
  305. }
  306. }
  307. }
  308. }
  309. })();
  310. module.exports = browserslist;