browsers.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, browserslistOpts) {
  33. _classCallCheck(this, Browsers);
  34. this.data = data;
  35. this.options = options || {};
  36. this.browserslistOpts = browserslistOpts || {};
  37. this.selected = this.parse(requirements);
  38. }
  39. /**
  40. * Return browsers selected by requirements
  41. */
  42. Browsers.prototype.parse = function parse(requirements) {
  43. var opts = {};
  44. for (var i in this.browserslistOpts) {
  45. opts[i] = this.browserslistOpts[i];
  46. }
  47. opts.path = this.options.from;
  48. opts.env = this.options.env;
  49. return browserslist(requirements, opts);
  50. };
  51. /**
  52. * Return prefix for selected browser
  53. */
  54. Browsers.prototype.prefix = function prefix(browser) {
  55. var _browser$split = browser.split(' '),
  56. name = _browser$split[0],
  57. version = _browser$split[1];
  58. var data = this.data[name];
  59. var prefix = data.prefix_exceptions && data.prefix_exceptions[version];
  60. if (!prefix) {
  61. prefix = data.prefix;
  62. }
  63. return '-' + prefix + '-';
  64. };
  65. /**
  66. * Is browser is selected by requirements
  67. */
  68. Browsers.prototype.isSelected = function isSelected(browser) {
  69. return this.selected.indexOf(browser) !== -1;
  70. };
  71. return Browsers;
  72. }();
  73. module.exports = Browsers;