1 | "use strict";
|
---|
2 |
|
---|
3 | var browserslist = require('browserslist');
|
---|
4 |
|
---|
5 | var postcss = require('postcss');
|
---|
6 |
|
---|
7 | var agents = require('caniuse-lite').agents;
|
---|
8 |
|
---|
9 | var pico = require('picocolors');
|
---|
10 |
|
---|
11 | var Browsers = require('./browsers');
|
---|
12 |
|
---|
13 | var Prefixes = require('./prefixes');
|
---|
14 |
|
---|
15 | var data = require('../data/prefixes');
|
---|
16 |
|
---|
17 | var info = require('./info');
|
---|
18 |
|
---|
19 | var 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 \n' + ' can 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';
|
---|
20 |
|
---|
21 | function isPlainObject(obj) {
|
---|
22 | return Object.prototype.toString.apply(obj) === '[object Object]';
|
---|
23 | }
|
---|
24 |
|
---|
25 | var cache = {};
|
---|
26 |
|
---|
27 | function timeCapsule(result, prefixes) {
|
---|
28 | if (prefixes.browsers.selected.length === 0) {
|
---|
29 | return;
|
---|
30 | }
|
---|
31 |
|
---|
32 | if (prefixes.add.selectors.length > 0) {
|
---|
33 | return;
|
---|
34 | }
|
---|
35 |
|
---|
36 | if (Object.keys(prefixes.add).length > 2) {
|
---|
37 | return;
|
---|
38 | }
|
---|
39 | /* istanbul ignore next */
|
---|
40 |
|
---|
41 |
|
---|
42 | result.warn('Greetings, time traveller. ' + 'We are in the golden age of prefix-less CSS, ' + 'where Autoprefixer is no longer needed for your stylesheet.');
|
---|
43 | }
|
---|
44 |
|
---|
45 | module.exports = postcss.plugin('autoprefixer', function () {
|
---|
46 | for (var _len = arguments.length, reqs = new Array(_len), _key = 0; _key < _len; _key++) {
|
---|
47 | reqs[_key] = arguments[_key];
|
---|
48 | }
|
---|
49 |
|
---|
50 | var options;
|
---|
51 |
|
---|
52 | if (reqs.length === 1 && isPlainObject(reqs[0])) {
|
---|
53 | options = reqs[0];
|
---|
54 | reqs = undefined;
|
---|
55 | } else if (reqs.length === 0 || reqs.length === 1 && !reqs[0]) {
|
---|
56 | reqs = undefined;
|
---|
57 | } else if (reqs.length <= 2 && (Array.isArray(reqs[0]) || !reqs[0])) {
|
---|
58 | options = reqs[1];
|
---|
59 | reqs = reqs[0];
|
---|
60 | } else if (typeof reqs[reqs.length - 1] === 'object') {
|
---|
61 | options = reqs.pop();
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (!options) {
|
---|
65 | options = {};
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (options.browser) {
|
---|
69 | throw new Error('Change `browser` option to `overrideBrowserslist` in Autoprefixer');
|
---|
70 | } else if (options.browserslist) {
|
---|
71 | throw new Error('Change `browserslist` option to `overrideBrowserslist` in Autoprefixer');
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (options.overrideBrowserslist) {
|
---|
75 | reqs = options.overrideBrowserslist;
|
---|
76 | } else if (options.browsers) {
|
---|
77 | if (typeof console !== 'undefined' && console.warn) {
|
---|
78 | console.warn(pico.red(WARNING.replace(/`[^`]+`/g, function (i) {
|
---|
79 | return pico.yellow(i.slice(1, -1));
|
---|
80 | })));
|
---|
81 | }
|
---|
82 |
|
---|
83 | reqs = options.browsers;
|
---|
84 | }
|
---|
85 |
|
---|
86 | var brwlstOpts = {
|
---|
87 | ignoreUnknownVersions: options.ignoreUnknownVersions,
|
---|
88 | stats: options.stats,
|
---|
89 | env: options.env
|
---|
90 | };
|
---|
91 |
|
---|
92 | function loadPrefixes(opts) {
|
---|
93 | var d = module.exports.data;
|
---|
94 | var browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts);
|
---|
95 | var key = browsers.selected.join(', ') + JSON.stringify(options);
|
---|
96 |
|
---|
97 | if (!cache[key]) {
|
---|
98 | cache[key] = new Prefixes(d.prefixes, browsers, options);
|
---|
99 | }
|
---|
100 |
|
---|
101 | return cache[key];
|
---|
102 | }
|
---|
103 |
|
---|
104 | function plugin(css, result) {
|
---|
105 | var prefixes = loadPrefixes({
|
---|
106 | from: css.source && css.source.input.file,
|
---|
107 | env: options.env
|
---|
108 | });
|
---|
109 | timeCapsule(result, prefixes);
|
---|
110 |
|
---|
111 | if (options.remove !== false) {
|
---|
112 | prefixes.processor.remove(css, result);
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (options.add !== false) {
|
---|
116 | prefixes.processor.add(css, result);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | plugin.options = options;
|
---|
121 | plugin.browsers = reqs;
|
---|
122 |
|
---|
123 | plugin.info = function (opts) {
|
---|
124 | opts = opts || {};
|
---|
125 | opts.from = opts.from || process.cwd();
|
---|
126 | return info(loadPrefixes(opts));
|
---|
127 | };
|
---|
128 |
|
---|
129 | return plugin;
|
---|
130 | });
|
---|
131 | /**
|
---|
132 | * Autoprefixer data
|
---|
133 | */
|
---|
134 |
|
---|
135 | module.exports.data = {
|
---|
136 | browsers: agents,
|
---|
137 | prefixes: data
|
---|
138 | };
|
---|
139 | /**
|
---|
140 | * Autoprefixer default browsers
|
---|
141 | */
|
---|
142 |
|
---|
143 | module.exports.defaults = browserslist.defaults;
|
---|
144 | /**
|
---|
145 | * Inspect with default Autoprefixer
|
---|
146 | */
|
---|
147 |
|
---|
148 | module.exports.info = function () {
|
---|
149 | return module.exports().info();
|
---|
150 | }; |
---|