UNPKG

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