wishthis/node_modules/autoprefixer/lib/browsers.js

80 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-08-17 09:47:40 +00:00
let browserslist = require('browserslist')
let { agents } = require('caniuse-lite/dist/unpacker/agents')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let utils = require('./utils')
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
class Browsers {
constructor(data, requirements, options, browserslistOpts) {
this.data = data
this.options = options || {}
this.browserslistOpts = browserslistOpts || {}
this.selected = this.parse(requirements)
}
2022-01-21 08:28:41 +00:00
/**
2023-08-17 09:47:40 +00:00
* Return all prefixes for default browser data
*/
static prefixes() {
2022-01-21 08:28:41 +00:00
if (this.prefixesCache) {
2023-08-17 09:47:40 +00:00
return this.prefixesCache
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
this.prefixesCache = []
for (let name in agents) {
this.prefixesCache.push(`-${agents[name].prefix}-`)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
this.prefixesCache = utils
.uniq(this.prefixesCache)
.sort((a, b) => b.length - a.length)
return this.prefixesCache
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
/**
* Check is value contain any possible prefix
*/
static withPrefix(value) {
2022-01-21 08:28:41 +00:00
if (!this.prefixesRegexp) {
2023-08-17 09:47:40 +00:00
this.prefixesRegexp = new RegExp(this.prefixes().join('|'))
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return this.prefixesRegexp.test(value)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
/**
* Is browser is selected by requirements
*/
isSelected(browser) {
return this.selected.includes(browser)
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
/**
* Return browsers selected by requirements
*/
parse(requirements) {
let opts = {}
for (let i in this.browserslistOpts) {
opts[i] = this.browserslistOpts[i]
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
opts.path = this.options.from
return browserslist(requirements, opts)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
/**
* Return prefix for selected browser
*/
prefix(browser) {
let [name, version] = browser.split(' ')
let data = this.data[name]
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
let prefix = data.prefix_exceptions && data.prefix_exceptions[version]
2022-01-21 08:28:41 +00:00
if (!prefix) {
2023-08-17 09:47:40 +00:00
prefix = data.prefix
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return `-${prefix}-`
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
}
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
module.exports = Browsers