| 1 |
let browserslist = require('browserslist') |
| 2 |
let { agents } = require('caniuse-lite/dist/unpacker/agents') |
| 3 |
let pico = require('picocolors') |
| 4 |
|
| 5 |
let dataPrefixes = require('../data/prefixes') |
| 6 |
let Browsers = require('./browsers') |
| 7 |
let getInfo = require('./info') |
| 8 |
let Prefixes = require('./prefixes') |
| 9 |
|
| 10 |
let autoprefixerData = { browsers: agents, prefixes: dataPrefixes } |
| 11 |
|
| 12 |
const WARNING = |
| 13 |
'\n' + |
| 14 |
' Replace Autoprefixer `browsers` option to Browserslist config.\n' + |
| 15 |
' Use `browserslist` key in `package.json` or `.browserslistrc` file.\n' + |
| 16 |
'\n' + |
| 17 |
' Using `browsers` option can cause errors. Browserslist config can\n' + |
| 18 |
' be used for Babel, Autoprefixer, postcss-normalize and other tools.\n' + |
| 19 |
'\n' + |
| 20 |
' If you really need to use option, rename it to `overrideBrowserslist`.\n' + |
| 21 |
'\n' + |
| 22 |
' Learn more at:\n' + |
| 23 |
' https://github.com/browserslist/browserslist#readme\n' + |
| 24 |
' https://twitter.com/browserslist\n' + |
| 25 |
'\n' |
| 26 |
|
| 27 |
function isPlainObject(obj) { |
| 28 |
return Object.prototype.toString.apply(obj) === '[object Object]' |
| 29 |
} |
| 30 |
|
| 31 |
let cache = new Map() |
| 32 |
|
| 33 |
function timeCapsule(result, prefixes) { |
| 34 |
if (prefixes.browsers.selected.length === 0) { |
| 35 |
return |
| 36 |
} |
| 37 |
if (prefixes.add.selectors.length > 0) { |
| 38 |
return |
| 39 |
} |
| 40 |
if (Object.keys(prefixes.add).length > 2) { |
| 41 |
return |
| 42 |
} |
| 43 |
|
| 44 |
result.warn( |
| 45 |
'Autoprefixer target browsers do not need any prefixes.' + |
| 46 |
'You do not need Autoprefixer anymore.\n' + |
| 47 |
'Check your Browserslist config to be sure that your targets ' + |
| 48 |
'are set up correctly.\n' + |
| 49 |
'\n' + |
| 50 |
' Learn more at:\n' + |
| 51 |
' https://github.com/postcss/autoprefixer#readme\n' + |
| 52 |
' https://github.com/browserslist/browserslist#readme\n' + |
| 53 |
'\n' |
| 54 |
) |
| 55 |
} |
| 56 |
|
| 57 |
module.exports = plugin |
| 58 |
|
| 59 |
function plugin(...reqs) { |
| 60 |
let options |
| 61 |
if (reqs.length === 1 && isPlainObject(reqs[0])) { |
| 62 |
options = reqs[0] |
| 63 |
reqs = undefined |
| 64 |
} else if (reqs.length === 0 || (reqs.length === 1 && !reqs[0])) { |
| 65 |
reqs = undefined |
| 66 |
} else if (reqs.length <= 2 && (Array.isArray(reqs[0]) || !reqs[0])) { |
| 67 |
options = reqs[1] |
| 68 |
reqs = reqs[0] |
| 69 |
} else if (typeof reqs[reqs.length - 1] === 'object') { |
| 70 |
options = reqs.pop() |
| 71 |
} |
| 72 |
|
| 73 |
if (!options) { |
| 74 |
options = {} |
| 75 |
} |
| 76 |
|
| 77 |
if (options.browser) { |
| 78 |
throw new Error( |
| 79 |
'Change `browser` option to `overrideBrowserslist` in Autoprefixer' |
| 80 |
) |
| 81 |
} else if (options.browserslist) { |
| 82 |
throw new Error( |
| 83 |
'Change `browserslist` option to `overrideBrowserslist` in Autoprefixer' |
| 84 |
) |
| 85 |
} |
| 86 |
|
| 87 |
if (options.overrideBrowserslist) { |
| 88 |
reqs = options.overrideBrowserslist |
| 89 |
} else if (options.browsers) { |
| 90 |
if (typeof console !== 'undefined' && console.warn) { |
| 91 |
console.warn( |
| 92 |
pico.red(WARNING.replace(/`[^`]+`/g, i => pico.yellow(i.slice(1, -1)))) |
| 93 |
) |
| 94 |
} |
| 95 |
reqs = options.browsers |
| 96 |
} |
| 97 |
|
| 98 |
let brwlstOpts = { |
| 99 |
env: options.env, |
| 100 |
ignoreUnknownVersions: options.ignoreUnknownVersions, |
| 101 |
stats: options.stats |
| 102 |
} |
| 103 |
|
| 104 |
function loadPrefixes(opts) { |
| 105 |
let d = autoprefixerData |
| 106 |
let browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts) |
| 107 |
let key = browsers.selected.join(', ') + JSON.stringify(options) |
| 108 |
|
| 109 |
if (!cache.has(key)) { |
| 110 |
cache.set(key, new Prefixes(d.prefixes, browsers, options)) |
| 111 |
} |
| 112 |
|
| 113 |
return cache.get(key) |
| 114 |
} |
| 115 |
|
| 116 |
return { |
| 117 |
browsers: reqs, |
| 118 |
|
| 119 |
info(opts) { |
| 120 |
opts = opts || {} |
| 121 |
opts.from = opts.from || process.cwd() |
| 122 |
return getInfo(loadPrefixes(opts)) |
| 123 |
}, |
| 124 |
|
| 125 |
options, |
| 126 |
|
| 127 |
postcssPlugin: 'autoprefixer', |
| 128 |
prepare(result) { |
| 129 |
let prefixes = loadPrefixes({ |
| 130 |
env: options.env, |
| 131 |
from: result.opts.from |
| 132 |
}) |
| 133 |
|
| 134 |
return { |
| 135 |
OnceExit(root) { |
| 136 |
timeCapsule(result, prefixes) |
| 137 |
if (options.remove !== false) { |
| 138 |
prefixes.processor.remove(root, result) |
| 139 |
} |
| 140 |
if (options.add !== false) { |
| 141 |
prefixes.processor.add(root, result) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
plugin.postcss = true |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
plugin.data = autoprefixerData |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
plugin.defaults = browserslist.defaults |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
plugin.info = () => plugin().info() |