UNPKG

2.34 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"
7
8const plugin = postcss.plugin("postcss-cssnext", (options) => {
9 options = {
10 console: console,
11 warnForDuplicates: true,
12 features: {},
13 // options.browsers is deliberately undefined by default to inherit
14 // browserslist default behavior
15 ...options,
16 }
17
18 const features = options.features
19
20 // propagate browsers option to plugins that supports it
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 // autoprefixer doesn't like an "undefined" value. Related to coffee ?
38 if (features.autoprefixer && features.autoprefixer.browsers === undefined) {
39 delete features.autoprefixer.browsers
40 }
41
42 const processor = postcss()
43
44 // features
45 Object.keys(libraryFeatures).forEach(key => {
46 // feature is auto enabled if: not disable && (enabled || no data yet ||
47 // !supported yet)
48 if (
49 // feature is not disabled
50 features[key] !== false &&
51 (
52 // feature is enabled
53 features[key] === true ||
54
55 // feature don't have any browsers data (yet)
56 featuresActivationMap[key] === undefined ||
57
58 // feature is not yet supported by the browsers scope
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// es5/6 support
86plugin.features = libraryFeatures
87
88module.exports = plugin