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 | import warnForDuplicates from "./warn-for-duplicates"
|
7 |
|
8 | const plugin = postcss.plugin("postcss-cssnext", (options) => {
|
9 | options = {
|
10 | console: console,
|
11 | warnForDuplicates: true,
|
12 | features: {},
|
13 |
|
14 |
|
15 | ...options,
|
16 | }
|
17 |
|
18 | const features = options.features
|
19 |
|
20 |
|
21 | const pluginsToPropagateBrowserOption = [ "autoprefixer", "rem" ]
|
22 | pluginsToPropagateBrowserOption.forEach((name) => {
|
23 | const feature = features[name]
|
24 |
|
25 | if (feature !== false) {
|
26 | features[name] = {
|
27 | browsers: (
|
28 | feature && feature.browsers
|
29 | ? feature.browsers
|
30 | : options.browsers
|
31 | ),
|
32 | ...(feature || {}),
|
33 | }
|
34 | }
|
35 | })
|
36 |
|
37 |
|
38 | if (features.autoprefixer && features.autoprefixer.browsers === undefined) {
|
39 | delete features.autoprefixer.browsers
|
40 | }
|
41 |
|
42 | const processor = postcss()
|
43 |
|
44 |
|
45 | Object.keys(libraryFeatures).forEach(key => {
|
46 |
|
47 |
|
48 | if (
|
49 |
|
50 | features[key] !== false &&
|
51 | (
|
52 |
|
53 | features[key] === true ||
|
54 |
|
55 |
|
56 | featuresActivationMap[key] === undefined ||
|
57 |
|
58 |
|
59 | (
|
60 | featuresActivationMap[key] &&
|
61 | featuresActivationMap[key][0] &&
|
62 | !isSupported(featuresActivationMap[key][0], options.browsers)
|
63 | )
|
64 | )
|
65 | ) {
|
66 | const plugin = libraryFeatures[key](
|
67 | typeof features[key] === "object"
|
68 | ? { ...features[key] }
|
69 | : undefined
|
70 | )
|
71 | processor.use(plugin)
|
72 | }
|
73 | })
|
74 |
|
75 | if (options.warnForDuplicates) {
|
76 | processor.use(warnForDuplicates({
|
77 | keys: Object.keys(libraryFeatures),
|
78 | console: options.console,
|
79 | }))
|
80 | }
|
81 |
|
82 | return processor
|
83 | })
|
84 |
|
85 |
|
86 | plugin.features = libraryFeatures
|
87 |
|
88 | module.exports = plugin
|