browsers.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. var browserslist = require('browserslist');
  3. var agents = require('caniuse-lite').agents;
  4. var utils = require('./utils');
  5. var Browsers = /*#__PURE__*/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. this.prefixesCache = [];
  14. for (var name in agents) {
  15. this.prefixesCache.push("-" + agents[name].prefix + "-");
  16. }
  17. this.prefixesCache = utils.uniq(this.prefixesCache).sort(function (a, b) {
  18. return b.length - a.length;
  19. });
  20. return this.prefixesCache;
  21. }
  22. /**
  23. * Check is value contain any possible prefix
  24. */
  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. this.data = data;
  34. this.options = options || {};
  35. this.browserslistOpts = browserslistOpts || {};
  36. this.selected = this.parse(requirements);
  37. }
  38. /**
  39. * Return browsers selected by requirements
  40. */
  41. var _proto = Browsers.prototype;
  42. _proto.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. return browserslist(requirements, opts);
  49. }
  50. /**
  51. * Return prefix for selected browser
  52. */
  53. ;
  54. _proto.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. ;
  69. _proto.isSelected = function isSelected(browser) {
  70. return this.selected.includes(browser);
  71. };
  72. return Browsers;
  73. }();
  74. module.exports = Browsers;