UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2
3var browserslist = require('browserslist');
4
5var postcss = require('postcss');
6
7var agents = require('caniuse-lite').agents;
8
9var kleur = require('kleur/colors');
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 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
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 (kleur.red) {
79 console.warn(kleur.red(WARNING.replace(/`[^`]+`/g, function (i) {
80 return kleur.yellow(i.slice(1, -1));
81 })));
82 } else {
83 console.warn(WARNING);
84 }
85 }
86
87 reqs = options.browsers;
88 }
89
90 var brwlstOpts = {
91 ignoreUnknownVersions: options.ignoreUnknownVersions,
92 stats: options.stats
93 };
94
95 function loadPrefixes(opts) {
96 var d = module.exports.data;
97 var browsers = new Browsers(d.browsers, reqs, opts, brwlstOpts);
98 var key = browsers.selected.join(', ') + JSON.stringify(options);
99
100 if (!cache[key]) {
101 cache[key] = new Prefixes(d.prefixes, browsers, options);
102 }
103
104 return cache[key];
105 }
106
107 function plugin(css, result) {
108 var prefixes = loadPrefixes({
109 from: css.source && css.source.input.file,
110 env: options.env
111 });
112 timeCapsule(result, prefixes);
113
114 if (options.remove !== false) {
115 prefixes.processor.remove(css, result);
116 }
117
118 if (options.add !== false) {
119 prefixes.processor.add(css, result);
120 }
121 }
122
123 plugin.options = options;
124 plugin.browsers = reqs;
125
126 plugin.info = function (opts) {
127 opts = opts || {};
128 opts.from = opts.from || process.cwd();
129 return info(loadPrefixes(opts));
130 };
131
132 return plugin;
133});
134/**
135 * Autoprefixer data
136 */
137
138module.exports.data = {
139 browsers: agents,
140 prefixes: data
141};
142/**
143 * Autoprefixer default browsers
144 */
145
146module.exports.defaults = browserslist.defaults;
147/**
148 * Inspect with default Autoprefixer
149 */
150
151module.exports.info = function () {
152 return module.exports().info();
153};
\No newline at end of file