browsers.js 2.1 KB

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