wishthis/node_modules/autoprefixer/lib/autoprefixer.js

165 lines
4.1 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')
let pico = require('picocolors')
let Browsers = require('./browsers')
let Prefixes = require('./prefixes')
let dataPrefixes = require('../data/prefixes')
let getInfo = require('./info')
let autoprefixerData = { browsers: agents, prefixes: dataPrefixes }
const WARNING =
'\n' +
' Replace Autoprefixer `browsers` option to Browserslist config.\n' +
' Use `browserslist` key in `package.json` or `.browserslistrc` file.\n' +
'\n' +
' Using `browsers` option can cause errors. Browserslist config can\n' +
' be used for Babel, Autoprefixer, postcss-normalize and other tools.\n' +
'\n' +
' If you really need to use option, rename it to `overrideBrowserslist`.\n' +
'\n' +
' Learn more at:\n' +
' https://github.com/browserslist/browserslist#readme\n' +
' https://twitter.com/browserslist\n' +
'\n'
2022-01-21 08:28:41 +00:00
function isPlainObject(obj) {
2023-08-17 09:47:40 +00:00
return Object.prototype.toString.apply(obj) === '[object Object]'
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let cache = new Map()
2022-01-21 08:28:41 +00:00
function timeCapsule(result, prefixes) {
if (prefixes.browsers.selected.length === 0) {
2023-08-17 09:47:40 +00:00
return
2022-01-21 08:28:41 +00:00
}
if (prefixes.add.selectors.length > 0) {
2023-08-17 09:47:40 +00:00
return
2022-01-21 08:28:41 +00:00
}
if (Object.keys(prefixes.add).length > 2) {
2023-08-17 09:47:40 +00:00
return
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
/* c8 ignore next 11 */
result.warn(
'Autoprefixer target browsers do not need any prefixes.' +
'You do not need Autoprefixer anymore.\n' +
'Check your Browserslist config to be sure that your targets ' +
'are set up correctly.\n' +
'\n' +
' Learn more at:\n' +
' https://github.com/postcss/autoprefixer#readme\n' +
' https://github.com/browserslist/browserslist#readme\n' +
'\n'
)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
module.exports = plugin
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
function plugin(...reqs) {
let options
2022-01-21 08:28:41 +00:00
if (reqs.length === 1 && isPlainObject(reqs[0])) {
2023-08-17 09:47:40 +00:00
options = reqs[0]
reqs = undefined
} else if (reqs.length === 0 || (reqs.length === 1 && !reqs[0])) {
reqs = undefined
2022-01-21 08:28:41 +00:00
} else if (reqs.length <= 2 && (Array.isArray(reqs[0]) || !reqs[0])) {
2023-08-17 09:47:40 +00:00
options = reqs[1]
reqs = reqs[0]
2022-01-21 08:28:41 +00:00
} else if (typeof reqs[reqs.length - 1] === 'object') {
2023-08-17 09:47:40 +00:00
options = reqs.pop()
2022-01-21 08:28:41 +00:00
}
if (!options) {
2023-08-17 09:47:40 +00:00
options = {}
2022-01-21 08:28:41 +00:00
}
if (options.browser) {
2023-08-17 09:47:40 +00:00
throw new Error(
'Change `browser` option to `overrideBrowserslist` in Autoprefixer'
)
2022-01-21 08:28:41 +00:00
} else if (options.browserslist) {
2023-08-17 09:47:40 +00:00
throw new Error(
'Change `browserslist` option to `overrideBrowserslist` in Autoprefixer'
)
2022-01-21 08:28:41 +00:00
}
if (options.overrideBrowserslist) {
2023-08-17 09:47:40 +00:00
reqs = options.overrideBrowserslist
2022-01-21 08:28:41 +00:00
} else if (options.browsers) {
if (typeof console !== 'undefined' && console.warn) {
2023-08-17 09:47:40 +00:00
console.warn(
pico.red(WARNING.replace(/`[^`]+`/g, i => pico.yellow(i.slice(1, -1))))
)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
reqs = options.browsers
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
let brwlstOpts = {
env: options.env,
2022-01-21 08:28:41 +00:00
ignoreUnknownVersions: options.ignoreUnknownVersions,
2023-08-17 09:47:40 +00:00
stats: options.stats
}
2022-01-21 08:28:41 +00:00
function loadPrefixes(opts) {
2023-08-17 09:47:40 +00:00
let d = autoprefixerData
let browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts)
let key = browsers.selected.join(', ') + JSON.stringify(options)
2022-01-21 08:28:41 +00:00
2023-08-17 09:47:40 +00:00
if (!cache.has(key)) {
cache.set(key, new Prefixes(d.prefixes, browsers, options))
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return cache.get(key)
2022-01-21 08:28:41 +00:00
}
2023-08-17 09:47:40 +00:00
return {
browsers: reqs,
info(opts) {
opts = opts || {}
opts.from = opts.from || process.cwd()
return getInfo(loadPrefixes(opts))
},
options,
postcssPlugin: 'autoprefixer',
prepare(result) {
let prefixes = loadPrefixes({
env: options.env,
from: result.opts.from
})
return {
OnceExit(root) {
timeCapsule(result, prefixes)
if (options.remove !== false) {
prefixes.processor.remove(root, result)
}
if (options.add !== false) {
prefixes.processor.add(root, result)
}
}
}
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
plugin.postcss = true
2022-01-21 08:28:41 +00:00
/**
* Autoprefixer data
*/
2023-08-17 09:47:40 +00:00
plugin.data = autoprefixerData
2022-01-21 08:28:41 +00:00
/**
* Autoprefixer default browsers
*/
2023-08-17 09:47:40 +00:00
plugin.defaults = browserslist.defaults
2022-01-21 08:28:41 +00:00
/**
* Inspect with default Autoprefixer
*/
2023-08-17 09:47:40 +00:00
plugin.info = () => plugin().info()