browsers.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. var browserslist = require('browserslist');
  4. var utils = require('./utils');
  5. var Browsers = function () {
  6. /**
  7. * Return all prefixes for default browser data
  8. */
  9. Browsers.prefixes = function prefixes() {
  10. if (this.prefixesCache) {
  11. return this.prefixesCache;
  12. }
  13. var data = require('caniuse-lite').agents;
  14. this.prefixesCache = [];
  15. for (var name in data) {
  16. this.prefixesCache.push('-' + data[name].prefix + '-');
  17. }
  18. this.prefixesCache = utils.uniq(this.prefixesCache).sort(function (a, b) {
  19. return b.length - a.length;
  20. });
  21. return this.prefixesCache;
  22. };
  23. /**
  24. * Check is value contain any possibe prefix
  25. */
  26. Browsers.withPrefix = function withPrefix(value) {
  27. if (!this.prefixesRegexp) {
  28. this.prefixesRegexp = new RegExp(this.prefixes().join('|'));
  29. }
  30. return this.prefixesRegexp.test(value);
  31. };
  32. function Browsers(data, requirements, options, stats) {
  33. _classCallCheck(this, Browsers);
  34. this.data = data;
  35. this.options = options || {};
  36. this.stats = stats;
  37. this.selected = this.parse(requirements);
  38. }
  39. /**
  40. * Return browsers selected by requirements
  41. */
  42. Browsers.prototype.parse = function parse(requirements) {
  43. return browserslist(requirements, {
  44. stats: this.stats,
  45. path: this.options.from,
  46. env: this.options.env
  47. });
  48. };
  49. /**
  50. * Return prefix for selected browser
  51. */
  52. Browsers.prototype.prefix = function prefix(browser) {
  53. var _browser$split = browser.split(' '),
  54. name = _browser$split[0],
  55. version = _browser$split[1];
  56. var data = this.data[name];
  57. var prefix = data.prefix_exceptions && data.prefix_exceptions[version];
  58. if (!prefix) {
  59. prefix = data.prefix;
  60. }
  61. return '-' + prefix + '-';
  62. };
  63. /**
  64. * Is browser is selected by requirements
  65. */
  66. Browsers.prototype.isSelected = function isSelected(browser) {
  67. return this.selected.indexOf(browser) !== -1;
  68. };
  69. return Browsers;
  70. }();
  71. module.exports = Browsers;