1 | import postcss from "postcss"
|
2 | import { isSupported } from "caniuse-api"
|
3 |
|
4 | import libraryFeatures from "./features"
|
5 | import featuresActivationMap from "./features-activation-map"
|
6 |
|
7 | const plugin = postcss.plugin("postcss-cssnext", (options) => {
|
8 | options = {
|
9 | features: {},
|
10 |
|
11 |
|
12 | ...options,
|
13 | }
|
14 |
|
15 | const features = options.features
|
16 |
|
17 |
|
18 | if (features.autoprefixer !== false) {
|
19 | features.autoprefixer = {
|
20 | browsers: (
|
21 | features.autoprefixer && features.autoprefixer.browsers
|
22 | ? features.autoprefixer.browsers
|
23 | : options.browsers
|
24 | ),
|
25 | ...(features.autoprefixer || {}),
|
26 | }
|
27 |
|
28 |
|
29 | if (features.autoprefixer.browsers === undefined) {
|
30 | delete features.autoprefixer.browsers
|
31 | }
|
32 | }
|
33 |
|
34 | const processor = postcss()
|
35 |
|
36 |
|
37 | Object.keys(libraryFeatures).forEach(key => {
|
38 |
|
39 |
|
40 | if (
|
41 |
|
42 | features[key] !== false &&
|
43 | (
|
44 |
|
45 | features[key] === true ||
|
46 |
|
47 |
|
48 | featuresActivationMap[key] === undefined ||
|
49 |
|
50 |
|
51 | (
|
52 | featuresActivationMap[key] &&
|
53 | featuresActivationMap[key][0] &&
|
54 | !isSupported(featuresActivationMap[key][0], options.browsers)
|
55 | )
|
56 | )
|
57 | ) {
|
58 | const plugin = libraryFeatures[key](
|
59 | typeof features[key] === "object"
|
60 | ? { ...features[key] }
|
61 | : undefined
|
62 | )
|
63 | processor.use(plugin)
|
64 | }
|
65 | })
|
66 |
|
67 | return processor
|
68 | })
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | plugin.features = libraryFeatures
|
79 |
|
80 | export default plugin
|