1 | let { yellow, red } = require('nanocolors')
|
2 | let browserslist = require('browserslist')
|
3 | let { agents } = require('caniuse-lite')
|
4 |
|
5 | let Browsers = require('./browsers')
|
6 | let Prefixes = require('./prefixes')
|
7 | let dataPrefixes = require('../data/prefixes')
|
8 | let getInfo = require('./info')
|
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 |
|
45 | result.warn(
|
46 | 'Autoprefixer target browsers do not need any prefixes.' +
|
47 | 'You do not need Autoprefixer anymore.\n' +
|
48 | 'Check your Browserslist config to be sure that your targets ' +
|
49 | 'are set up correctly.\n' +
|
50 | '\n' +
|
51 | ' Learn more at:\n' +
|
52 | ' https://github.com/postcss/autoprefixer#readme\n' +
|
53 | ' https://github.com/browserslist/browserslist#readme\n' +
|
54 | '\n'
|
55 | )
|
56 | }
|
57 |
|
58 | module.exports = plugin
|
59 |
|
60 | function plugin(...reqs) {
|
61 | let options
|
62 | if (reqs.length === 1 && isPlainObject(reqs[0])) {
|
63 | options = reqs[0]
|
64 | reqs = undefined
|
65 | } else if (reqs.length === 0 || (reqs.length === 1 && !reqs[0])) {
|
66 | reqs = undefined
|
67 | } else if (reqs.length <= 2 && (Array.isArray(reqs[0]) || !reqs[0])) {
|
68 | options = reqs[1]
|
69 | reqs = reqs[0]
|
70 | } else if (typeof reqs[reqs.length - 1] === 'object') {
|
71 | options = reqs.pop()
|
72 | }
|
73 |
|
74 | if (!options) {
|
75 | options = {}
|
76 | }
|
77 |
|
78 | if (options.browser) {
|
79 | throw new Error(
|
80 | 'Change `browser` option to `overrideBrowserslist` in Autoprefixer'
|
81 | )
|
82 | } else if (options.browserslist) {
|
83 | throw new Error(
|
84 | 'Change `browserslist` option to `overrideBrowserslist` in Autoprefixer'
|
85 | )
|
86 | }
|
87 |
|
88 | if (options.overrideBrowserslist) {
|
89 | reqs = options.overrideBrowserslist
|
90 | } else if (options.browsers) {
|
91 | if (typeof console !== 'undefined' && console.warn) {
|
92 | console.warn(
|
93 | red(WARNING.replace(/`[^`]+`/g, i => yellow(i.slice(1, -1))))
|
94 | )
|
95 | }
|
96 | reqs = options.browsers
|
97 | }
|
98 |
|
99 | let brwlstOpts = {
|
100 | ignoreUnknownVersions: options.ignoreUnknownVersions,
|
101 | stats: options.stats,
|
102 | env: options.env
|
103 | }
|
104 |
|
105 | function loadPrefixes(opts) {
|
106 | let d = autoprefixerData
|
107 | let browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts)
|
108 | let key = browsers.selected.join(', ') + JSON.stringify(options)
|
109 |
|
110 | if (!cache.has(key)) {
|
111 | cache.set(key, new Prefixes(d.prefixes, browsers, options))
|
112 | }
|
113 |
|
114 | return cache.get(key)
|
115 | }
|
116 |
|
117 | return {
|
118 | postcssPlugin: 'autoprefixer',
|
119 |
|
120 | prepare(result) {
|
121 | let prefixes = loadPrefixes({
|
122 | from: result.opts.from,
|
123 | env: options.env
|
124 | })
|
125 |
|
126 | return {
|
127 | OnceExit(root) {
|
128 | timeCapsule(result, prefixes)
|
129 | if (options.remove !== false) {
|
130 | prefixes.processor.remove(root, result)
|
131 | }
|
132 | if (options.add !== false) {
|
133 | prefixes.processor.add(root, result)
|
134 | }
|
135 | }
|
136 | }
|
137 | },
|
138 |
|
139 | info(opts) {
|
140 | opts = opts || {}
|
141 | opts.from = opts.from || process.cwd()
|
142 | return getInfo(loadPrefixes(opts))
|
143 | },
|
144 |
|
145 | options,
|
146 | browsers: reqs
|
147 | }
|
148 | }
|
149 |
|
150 | plugin.postcss = true
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | plugin.data = autoprefixerData
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | plugin.defaults = browserslist.defaults
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | plugin.info = () => plugin().info()
|