1
- import autoprefixer from 'autoprefixer'
1
+ import autoprefixer from 'autoprefixer' ;
2
2
import browserslist from 'browserslist' ;
3
3
import cssdb from 'cssdb' ;
4
- import postcss from 'postcss' ;
5
4
import plugins from './lib/plugins-by-id' ;
6
5
import getTransformedInsertions from './lib/get-transformed-insertions' ;
7
6
import getUnsupportedBrowsersByFeature from './lib/get-unsupported-browsers-by-feature' ;
8
7
import idsByExecutionOrder from './lib/ids-by-execution-order' ;
9
8
import writeToExports from './lib/write-to-exports' ;
10
9
11
- export default postcss . plugin ( 'postcss-preset-env' , opts => {
10
+ const plugin = opts => {
12
11
// initialize options
13
12
const features = Object ( Object ( opts ) . features ) ;
14
13
const insertBefore = Object ( Object ( opts ) . insertBefore ) ;
@@ -17,13 +16,13 @@ export default postcss.plugin('postcss-preset-env', opts => {
17
16
const stage = 'stage' in Object ( opts )
18
17
? opts . stage === false
19
18
? 5
20
- : parseInt ( opts . stage ) || 0
21
- : 2 ;
19
+ : parseInt ( opts . stage ) || 0
20
+ : 2 ;
22
21
const autoprefixerOptions = Object ( opts ) . autoprefixer ;
23
22
const sharedOpts = initializeSharedOpts ( Object ( opts ) ) ;
24
23
const stagedAutoprefixer = autoprefixerOptions === false
25
24
? ( ) => { }
26
- : autoprefixer ( Object . assign ( { overrideBrowserslist : browsers } , autoprefixerOptions ) ) ;
25
+ : autoprefixer ( Object . assign ( { overrideBrowserslist : browsers } , autoprefixerOptions ) ) ;
27
26
28
27
// polyfillable features (those with an available postcss plugin)
29
28
const polyfillableFeatures = cssdb . concat (
@@ -61,26 +60,35 @@ export default postcss.plugin('postcss-preset-env', opts => {
61
60
const stagedFeatures = polyfillableFeatures . filter (
62
61
feature => feature . id in features
63
62
? features [ feature . id ]
64
- : feature . stage >= stage
63
+ : feature . stage >= stage
65
64
) . map (
66
- feature => ( {
67
- browsers : feature . browsers ,
68
- plugin : typeof feature . plugin . process === 'function'
69
- ? features [ feature . id ] === true
70
- ? sharedOpts
71
- // if the plugin is enabled and has shared options
72
- ? feature . plugin ( Object . assign ( { } , sharedOpts ) )
73
- // otherwise, if the plugin is enabled
74
- : feature . plugin ( )
75
- : sharedOpts
65
+ feature => {
66
+ let options ;
67
+ let plugin ;
68
+
69
+ if ( features [ feature . id ] === true ) {
70
+ // if the plugin is enabled
71
+ options = sharedOpts ? Object . assign ( { } , sharedOpts ) : undefined ;
72
+ } else {
73
+ options = sharedOpts
76
74
// if the plugin has shared options and individual options
77
- ? feature . plugin ( Object . assign ( { } , sharedOpts , features [ feature . id ] ) )
78
- // if the plugin has individual options
79
- : feature . plugin ( Object . assign ( { } , features [ feature . id ] ) )
80
- // if the plugin is already initialized
81
- : feature . plugin ,
82
- id : feature . id
83
- } )
75
+ ? Object . assign ( { } , sharedOpts , features [ feature . id ] )
76
+ // if the plugin has individual options
77
+ : Object . assign ( { } , features [ feature . id ] ) ;
78
+ }
79
+
80
+ if ( feature . plugin . postcss ) {
81
+ plugin = feature . plugin ( options ) ;
82
+ } else {
83
+ plugin = feature . plugin ;
84
+ }
85
+
86
+ return {
87
+ browsers : feature . browsers ,
88
+ plugin,
89
+ id : feature . id
90
+ } ;
91
+ }
84
92
) ;
85
93
86
94
// browsers supported by the configuration
@@ -90,35 +98,28 @@ export default postcss.plugin('postcss-preset-env', opts => {
90
98
const supportedFeatures = stagedFeatures . filter (
91
99
feature => feature . id in features
92
100
? features [ feature . id ]
93
- : supportedBrowsers . some (
94
- supportedBrowser => browserslist ( feature . browsers , {
95
- ignoreUnknownVersions : true
96
- } ) . some (
97
- polyfillBrowser => polyfillBrowser === supportedBrowser
101
+ : supportedBrowsers . some (
102
+ supportedBrowser => browserslist ( feature . browsers , {
103
+ ignoreUnknownVersions : true
104
+ } ) . some (
105
+ polyfillBrowser => polyfillBrowser === supportedBrowser
106
+ )
98
107
)
99
- )
100
108
) ;
101
109
102
- return ( root , result ) => {
103
- // polyfills run in execution order
104
- const polyfills = supportedFeatures . reduce (
105
- ( promise , feature ) => promise . then (
106
- ( ) => feature . plugin ( result . root , result )
107
- ) ,
108
- Promise . resolve ( )
109
- ) . then (
110
- ( ) => stagedAutoprefixer ( result . root , result )
111
- ) . then (
112
- ( ) => {
113
- if ( Object ( opts ) . exportTo ) {
114
- writeToExports ( sharedOpts . exportTo , opts . exportTo ) ;
115
- }
116
- }
117
- )
110
+ const usedPlugins = supportedFeatures . map ( feature => feature . plugin ) ;
111
+ usedPlugins . push ( stagedAutoprefixer ) ;
118
112
119
- return polyfills ;
113
+ return {
114
+ postcssPlugin : 'postcss-preset-env' ,
115
+ plugins : usedPlugins ,
116
+ OnceExit : function ( ) {
117
+ if ( Object ( opts ) . exportTo ) {
118
+ writeToExports ( sharedOpts . exportTo , opts . exportTo ) ;
119
+ }
120
+ }
120
121
} ;
121
- } ) ;
122
+ }
122
123
123
124
const initializeSharedOpts = opts => {
124
125
if ( 'importFrom' in opts || 'exportTo' in opts || 'preserve' in opts ) {
@@ -145,3 +146,7 @@ const initializeSharedOpts = opts => {
145
146
146
147
return false ;
147
148
} ;
149
+
150
+ plugin . postcss = true ;
151
+
152
+ export default plugin ;
0 commit comments