@@ -39,197 +39,215 @@ function exec(code, loaderContext) {
39
39
* @param {String } content Source
40
40
* @param {Object } sourceMap Source Map
41
41
*
42
- * @return {cb } cb Result
42
+ * @return {callback } callback Result
43
43
*/
44
44
45
- export default function loader ( content , sourceMap , meta = { } ) {
45
+ export default async function loader ( content , sourceMap , meta = { } ) {
46
46
const options = getOptions ( this ) ;
47
47
48
48
validateOptions ( schema , options , {
49
49
name : 'PostCSS Loader' ,
50
50
baseDataPath : 'options' ,
51
51
} ) ;
52
52
53
- const cb = this . async ( ) ;
53
+ const callback = this . async ( ) ;
54
54
const file = this . resourcePath ;
55
+ let config ;
56
+
57
+ const { length } = Object . keys ( options ) . filter ( ( option ) => {
58
+ switch ( option ) {
59
+ // case 'exec':
60
+ case 'ident' :
61
+ case 'config' :
62
+ case 'sourceMap' :
63
+ return false ;
64
+ default :
65
+ return option ;
66
+ }
67
+ } ) ;
55
68
56
- Promise . resolve ( )
57
- . then ( ( ) => {
58
- const { length } = Object . keys ( options ) . filter ( ( option ) => {
59
- switch ( option ) {
60
- // case 'exec':
61
- case 'ident' :
62
- case 'config' :
63
- case 'sourceMap' :
64
- return false ;
65
- default :
66
- return option ;
67
- }
68
- } ) ;
69
-
70
- if ( length ) {
71
- return parseOptions . call ( this , options ) ;
72
- }
73
-
74
- const rc = {
75
- path : path . dirname ( file ) ,
76
- ctx : {
77
- file : {
78
- extname : path . extname ( file ) ,
79
- dirname : path . dirname ( file ) ,
80
- basename : path . basename ( file ) ,
81
- } ,
82
- options : { } ,
83
- } ,
84
- } ;
85
-
86
- if ( options . config ) {
87
- if ( options . config . path ) {
88
- rc . path = path . resolve ( options . config . path ) ;
89
- }
90
-
91
- if ( options . config . ctx ) {
92
- rc . ctx . options = options . config . ctx ;
93
- }
94
- }
95
-
96
- rc . ctx . webpack = this ;
97
-
98
- return postcssrc ( rc . ctx , rc . path ) ;
99
- } )
100
- . then ( ( config = { } ) => {
101
- if ( config . file ) {
102
- this . addDependency ( config . file ) ;
103
- }
104
-
105
- // Disable override `to` option from `postcss.config.js`
106
- if ( config . options . to ) {
107
- // eslint-disable-next-line no-param-reassign
108
- delete config . options . to ;
109
- }
110
- // Disable override `from` option from `postcss.config.js`
111
- if ( config . options . from ) {
112
- // eslint-disable-next-line no-param-reassign
113
- delete config . options . from ;
114
- }
115
-
116
- const plugins = config . plugins || [ ] ;
117
-
118
- const postcssOptions = Object . assign (
119
- {
120
- from : file ,
121
- map : options . sourceMap
122
- ? options . sourceMap === 'inline'
123
- ? { inline : true , annotation : false }
124
- : { inline : false , annotation : false }
125
- : false ,
69
+ if ( length ) {
70
+ try {
71
+ config = await parseOptions . call ( this , options ) ;
72
+ } catch ( error ) {
73
+ callback ( error ) ;
74
+
75
+ return ;
76
+ }
77
+ } else {
78
+ const rc = {
79
+ path : path . dirname ( file ) ,
80
+ ctx : {
81
+ file : {
82
+ extname : path . extname ( file ) ,
83
+ dirname : path . dirname ( file ) ,
84
+ basename : path . basename ( file ) ,
126
85
} ,
127
- config . options
128
- ) ;
129
-
130
- // Loader Exec (Deprecated)
131
- // https://webpack.js.org/api/loaders/#deprecated-context-properties
132
- if ( postcssOptions . parser === 'postcss-js' ) {
133
- // eslint-disable-next-line no-param-reassign
134
- content = exec ( content , this ) ;
135
- }
136
-
137
- if ( typeof postcssOptions . parser === 'string' ) {
138
- // eslint-disable-next-line import/no-dynamic-require,global-require
139
- postcssOptions . parser = require ( postcssOptions . parser ) ;
140
- }
86
+ options : { } ,
87
+ } ,
88
+ } ;
141
89
142
- if ( typeof postcssOptions . syntax === 'string' ) {
143
- // eslint-disable-next-line import/no-dynamic-require,global-require
144
- postcssOptions . syntax = require ( postcssOptions . syntax ) ;
90
+ if ( options . config ) {
91
+ if ( options . config . path ) {
92
+ rc . path = path . resolve ( options . config . path ) ;
145
93
}
146
94
147
- if ( typeof postcssOptions . stringifier === 'string' ) {
148
- // eslint-disable-next-line import/no-dynamic-require,global-require
149
- postcssOptions . stringifier = require ( postcssOptions . stringifier ) ;
150
- }
151
-
152
- // Loader API Exec (Deprecated)
153
- // https://webpack.js.org/api/loaders/#deprecated-context-properties
154
- if ( config . exec ) {
155
- // eslint-disable-next-line no-param-reassign
156
- content = exec ( content , this ) ;
157
- }
158
-
159
- if ( options . sourceMap && typeof sourceMap === 'string' ) {
160
- // eslint-disable-next-line no-param-reassign
161
- sourceMap = JSON . parse ( sourceMap ) ;
162
- }
163
-
164
- if ( options . sourceMap && sourceMap ) {
165
- postcssOptions . map . prev = sourceMap ;
95
+ if ( options . config . ctx ) {
96
+ rc . ctx . options = options . config . ctx ;
166
97
}
98
+ }
99
+
100
+ rc . ctx . webpack = this ;
101
+
102
+ try {
103
+ config = await postcssrc ( rc . ctx , rc . path ) ;
104
+ } catch ( error ) {
105
+ callback ( error ) ;
106
+
107
+ return ;
108
+ }
109
+ }
110
+
111
+ if ( typeof config === 'undefined' ) {
112
+ config = { } ;
113
+ }
114
+
115
+ if ( config . file ) {
116
+ this . addDependency ( config . file ) ;
117
+ }
118
+
119
+ // Disable override `to` option from `postcss.config.js`
120
+ if ( config . options . to ) {
121
+ // eslint-disable-next-line no-param-reassign
122
+ delete config . options . to ;
123
+ }
124
+ // Disable override `from` option from `postcss.config.js`
125
+ if ( config . options . from ) {
126
+ // eslint-disable-next-line no-param-reassign
127
+ delete config . options . from ;
128
+ }
129
+
130
+ const plugins = config . plugins || [ ] ;
131
+
132
+ const postcssOptions = Object . assign (
133
+ {
134
+ from : file ,
135
+ map : options . sourceMap
136
+ ? options . sourceMap === 'inline'
137
+ ? { inline : true , annotation : false }
138
+ : { inline : false , annotation : false }
139
+ : false ,
140
+ } ,
141
+ config . options
142
+ ) ;
143
+
144
+ // Loader Exec (Deprecated)
145
+ // https://webpack.js.org/api/loaders/#deprecated-context-properties
146
+ if ( postcssOptions . parser === 'postcss-js' ) {
147
+ // eslint-disable-next-line no-param-reassign
148
+ content = exec ( content , this ) ;
149
+ }
150
+
151
+ if ( typeof postcssOptions . parser === 'string' ) {
152
+ // eslint-disable-next-line import/no-dynamic-require,global-require
153
+ postcssOptions . parser = require ( postcssOptions . parser ) ;
154
+ }
155
+
156
+ if ( typeof postcssOptions . syntax === 'string' ) {
157
+ // eslint-disable-next-line import/no-dynamic-require,global-require
158
+ postcssOptions . syntax = require ( postcssOptions . syntax ) ;
159
+ }
160
+
161
+ if ( typeof postcssOptions . stringifier === 'string' ) {
162
+ // eslint-disable-next-line import/no-dynamic-require,global-require
163
+ postcssOptions . stringifier = require ( postcssOptions . stringifier ) ;
164
+ }
165
+
166
+ // Loader API Exec (Deprecated)
167
+ // https://webpack.js.org/api/loaders/#deprecated-context-properties
168
+ if ( config . exec ) {
169
+ // eslint-disable-next-line no-param-reassign
170
+ content = exec ( content , this ) ;
171
+ }
172
+
173
+ if ( options . sourceMap && typeof sourceMap === 'string' ) {
174
+ // eslint-disable-next-line no-param-reassign
175
+ sourceMap = JSON . parse ( sourceMap ) ;
176
+ }
177
+
178
+ if ( options . sourceMap && sourceMap ) {
179
+ postcssOptions . map . prev = sourceMap ;
180
+ }
181
+
182
+ let result ;
183
+
184
+ try {
185
+ result = await postcss ( plugins ) . process ( content , postcssOptions ) ;
186
+ } catch ( error ) {
187
+ if ( error . file ) {
188
+ this . addDependency ( error . file ) ;
189
+ }
190
+
191
+ if ( error . name === 'CssSyntaxError' ) {
192
+ callback ( new SyntaxError ( error ) ) ;
193
+ } else {
194
+ callback ( error ) ;
195
+ }
196
+
197
+ return ;
198
+ }
199
+
200
+ const { css, root, processor, messages } = result ;
201
+ let { map } = result ;
202
+
203
+ result . warnings ( ) . forEach ( ( warning ) => {
204
+ this . emitWarning ( new Warning ( warning ) ) ;
205
+ } ) ;
167
206
168
- return postcss ( plugins )
169
- . process ( content , postcssOptions )
170
- . then ( ( result ) => {
171
- const { css, root, processor, messages } = result ;
172
- let { map } = result ;
173
-
174
- result . warnings ( ) . forEach ( ( warning ) => {
175
- this . emitWarning ( new Warning ( warning ) ) ;
176
- } ) ;
177
-
178
- messages . forEach ( ( msg ) => {
179
- if ( msg . type === 'dependency' ) {
180
- this . addDependency ( msg . file ) ;
181
- }
182
- } ) ;
183
-
184
- map = map ? map . toJSON ( ) : null ;
185
-
186
- if ( map ) {
187
- map . file = path . resolve ( map . file ) ;
188
- map . sources = map . sources . map ( ( src ) => path . resolve ( src ) ) ;
189
- }
190
-
191
- const ast = {
192
- type : 'postcss' ,
193
- version : processor . version ,
194
- root,
195
- } ;
196
-
197
- const newMeta = { ...meta , ast, messages } ;
198
-
199
- if ( this . loaderIndex === 0 ) {
200
- /**
201
- * @memberof loader
202
- * @callback cb
203
- *
204
- * @param {Object } null Error
205
- * @param {String } css Result (JS Module)
206
- * @param {Object } map Source Map
207
- */
208
- cb ( null , `module.exports = ${ JSON . stringify ( css ) } ` , map ) ;
209
-
210
- return null ;
211
- }
212
-
213
- /**
214
- * @memberof loader
215
- * @callback cb
216
- *
217
- * @param {Object } null Error
218
- * @param {String } css Result (Raw Module)
219
- * @param {Object } map Source Map
220
- */
221
- cb ( null , css , map , newMeta ) ;
222
-
223
- return null ;
224
- } ) ;
225
- } )
226
- . catch ( ( err ) => {
227
- if ( err . file ) {
228
- this . addDependency ( err . file ) ;
229
- }
207
+ messages . forEach ( ( msg ) => {
208
+ if ( msg . type === 'dependency' ) {
209
+ this . addDependency ( msg . file ) ;
210
+ }
211
+ } ) ;
230
212
231
- return err . name === 'CssSyntaxError' ? cb ( new SyntaxError ( err ) ) : cb ( err ) ;
232
- } ) ;
213
+ map = map ? map . toJSON ( ) : null ;
214
+
215
+ if ( map ) {
216
+ map . file = path . resolve ( map . file ) ;
217
+ map . sources = map . sources . map ( ( src ) => path . resolve ( src ) ) ;
218
+ }
219
+
220
+ const ast = {
221
+ type : 'postcss' ,
222
+ version : processor . version ,
223
+ root,
224
+ } ;
225
+
226
+ const newMeta = { ...meta , ast, messages } ;
227
+
228
+ if ( this . loaderIndex === 0 ) {
229
+ /**
230
+ * @memberof loader
231
+ * @callback callback
232
+ *
233
+ * @param {Object } null Error
234
+ * @param {String } css Result (JS Module)
235
+ * @param {Object } map Source Map
236
+ */
237
+ callback ( null , `module.exports = ${ JSON . stringify ( css ) } ` , map ) ;
238
+
239
+ return ;
240
+ }
241
+
242
+ /**
243
+ * @memberof loader
244
+ * @callback callback
245
+ *
246
+ * @param {Object } null Error
247
+ * @param {String } css Result (Raw Module)
248
+ * @param {Object } map Source Map
249
+ */
250
+ callback ( null , css , map , newMeta ) ;
233
251
}
234
252
235
253
/**
0 commit comments