UNPKG

2.22 kBJavaScriptView Raw
1import postcss from "postcss"
2import { isSupported } from "caniuse-api"
3
4import libraryFeatures from "./features"
5import featuresActivationMap from "./features-activation-map"
6
7const plugin = postcss.plugin("postcss-cssnext", (options) => {
8 options = {
9 features: {},
10 // options.browsers is deliberately undefined by default to inherit
11 // browserslist default behavior
12 ...options,
13 }
14
15 const features = options.features
16
17 // propagate browsers option to autoprefixer
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 // autoprefixer doesn't like an "undefined" value. Related to coffee ?
29 if (features.autoprefixer.browsers === undefined) {
30 delete features.autoprefixer.browsers
31 }
32 }
33
34 const processor = postcss()
35
36 // features
37 Object.keys(libraryFeatures).forEach(key => {
38 // feature is auto enabled if: not disable && (enabled || no data yet ||
39 // !supported yet)
40 if (
41 // feature is not disabled
42 features[key] !== false &&
43 (
44 // feature is enabled
45 features[key] === true ||
46
47 // feature don't have any browsers data (yet)
48 featuresActivationMap[key] === undefined ||
49
50 // feature is not yet supported by the browsers scope
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// according to the way babel transpile es6 module
71// we cannot use the following syntax to export features
72//
73// export { libraryFeatures as features }
74//
75// babel only add `module.exports = exports["default"];` if there is only one
76// thing exported
77// so we add `features` as a plugin property
78plugin.features = libraryFeatures
79
80export default plugin