UNPKG

2.55 kBJavaScriptView Raw
1import postcss from "postcss"
2import { isSupported } from "caniuse-api"
3
4import libraryFeatures from "./features"
5import featuresActivationMap from "./features-activation-map"
6import warnForDuplicates from "./warn-for-duplicates"
7import warnForDeprecations from "./warn-for-deprecations"
8
9const plugin = postcss.plugin("postcss-cssnext", (options) => {
10 options = {
11 console: console,
12 warnForDuplicates: true,
13 warnForDeprecations: true,
14 features: {},
15 // options.browsers is deliberately undefined by default to inherit
16 // browserslist default behavior
17 ...options,
18 }
19
20 const features = options.features
21
22 // propagate browsers option to plugins that supports it
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 // autoprefixer doesn't like an "undefined" value. Related to coffee ?
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 // features
53 Object.keys(libraryFeatures).forEach(key => {
54 // feature is auto enabled if: not disable && (enabled || no data yet ||
55 // !supported yet)
56 if (
57 // feature is not disabled
58 features[key] !== false &&
59 (
60 // feature is enabled
61 features[key] === true ||
62
63 // feature don't have any browsers data (yet)
64 featuresActivationMap[key] === undefined ||
65
66 // feature is not yet supported by the browsers scope
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// es5/6 support
94plugin.features = libraryFeatures
95
96module.exports = plugin