UNPKG

4.14 kBJavaScriptView Raw
1"use strict";
2
3var browserslist = require('browserslist');
4
5var postcss = require('postcss');
6
7var agents = require('caniuse-lite').agents;
8
9var chalk = require('chalk');
10
11var Browsers = require('./browsers');
12
13var Prefixes = require('./prefixes');
14
15var data = require('../data/prefixes');
16
17var info = require('./info');
18
19var WARNING = '\n' + ' Replace Autoprefixer `browsers` option to Browserslist config.\n' + ' Use `browserslist` key in `package.json` or `.browserslistrc` file.\n' + '\n' + ' Using `browsers` option cause some error. 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
21function isPlainObject(obj) {
22 return Object.prototype.toString.apply(obj) === '[object Object]';
23}
24
25var cache = {};
26
27function 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
45module.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 if (chalk && chalk.red) {
79 console.warn(chalk.red(WARNING.replace(/`[^`]+`/g, function (i) {
80 return chalk.yellow(i.slice(1, -1));
81 })));
82 } else {
83 console.warn(WARNING);
84 }
85 }
86
87 reqs = options.browsers;
88 }
89
90 if (typeof options.grid === 'undefined') {
91 options.grid = false;
92 }
93
94 var brwlstOpts = {
95 ignoreUnknownVersions: options.ignoreUnknownVersions,
96 stats: options.stats
97 };
98
99 function loadPrefixes(opts) {
100 var d = module.exports.data;
101 var browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts);
102 var key = browsers.selected.join(', ') + JSON.stringify(options);
103
104 if (!cache[key]) {
105 cache[key] = new Prefixes(d.prefixes, browsers, options);
106 }
107
108 return cache[key];
109 }
110
111 function plugin(css, result) {
112 var prefixes = loadPrefixes({
113 from: css.source && css.source.input.file,
114 env: options.env
115 });
116 timeCapsule(result, prefixes);
117
118 if (options.remove !== false) {
119 prefixes.processor.remove(css, result);
120 }
121
122 if (options.add !== false) {
123 prefixes.processor.add(css, result);
124 }
125 }
126
127 plugin.options = options;
128 plugin.browsers = reqs;
129
130 plugin.info = function (opts) {
131 opts = opts || {};
132 opts.from = opts.from || process.cwd();
133 return info(loadPrefixes(opts));
134 };
135
136 return plugin;
137});
138/**
139 * Autoprefixer data
140 */
141
142module.exports.data = {
143 browsers: agents,
144 prefixes: data
145};
146/**
147 * Autoprefixer default browsers
148 */
149
150module.exports.defaults = browserslist.defaults;
151/**
152 * Inspect with default Autoprefixer
153 */
154
155module.exports.info = function () {
156 return module.exports().info();
157};
\No newline at end of file