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