browsers.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. opts.env = this.options.env;
  49. return browserslist(requirements, opts);
  50. }
  51. /**
  52. * Return prefix for selected browser
  53. */
  54. ;
  55. _proto.prefix = function prefix(browser) {
  56. var _browser$split = browser.split(' '),
  57. name = _browser$split[0],
  58. version = _browser$split[1];
  59. var data = this.data[name];
  60. var prefix = data.prefix_exceptions && data.prefix_exceptions[version];
  61. if (!prefix) {
  62. prefix = data.prefix;
  63. }
  64. return "-" + prefix + "-";
  65. }
  66. /**
  67. * Is browser is selected by requirements
  68. */
  69. ;
  70. _proto.isSelected = function isSelected(browser) {
  71. return this.selected.includes(browser);
  72. };
  73. return Browsers;
  74. }();
  75. module.exports = Browsers;