@@ -9,9 +9,9 @@ import Warning from './Warning';
9
9
import SyntaxError from './Error' ;
10
10
import schema from './options.json' ;
11
11
import {
12
- exec ,
13
12
loadConfig ,
14
- getArrayPlugins ,
13
+ getPostcssOptions ,
14
+ exec ,
15
15
getSourceMapAbsolutePath ,
16
16
getSourceMapRelativePath ,
17
17
normalizeSourceMap ,
@@ -38,9 +38,7 @@ export default async function loader(content, sourceMap, meta = {}) {
38
38
baseDataPath : 'options' ,
39
39
} ) ;
40
40
41
- const callback = this . async ( ) ;
42
41
const file = this . resourcePath ;
43
-
44
42
const configOptions =
45
43
typeof options . postcssOptions === 'undefined' ||
46
44
typeof options . postcssOptions . config === 'undefined'
@@ -49,6 +47,8 @@ export default async function loader(content, sourceMap, meta = {}) {
49
47
50
48
let loadedConfig = { } ;
51
49
50
+ const callback = this . async ( ) ;
51
+
52
52
if ( configOptions ) {
53
53
const dataForLoadConfig = {
54
54
path : path . dirname ( file ) ,
@@ -86,112 +86,43 @@ export default async function loader(content, sourceMap, meta = {}) {
86
86
}
87
87
}
88
88
89
- options . postcssOptions = options . postcssOptions || { } ;
90
-
91
- let plugins ;
92
-
93
- const disabledPlugins = [ ] ;
94
-
95
- try {
96
- plugins = [
97
- ...getArrayPlugins ( loadedConfig . plugins , file , false , this ) ,
98
- ...getArrayPlugins (
99
- options . postcssOptions . plugins ,
100
- file ,
101
- disabledPlugins ,
102
- this
103
- ) ,
104
- ] . filter ( ( i ) => ! disabledPlugins . includes ( i . postcssPlugin ) ) ;
105
- } catch ( error ) {
106
- this . emitError ( error ) ;
107
- }
108
-
109
- const mergedOptions = {
110
- ...loadedConfig ,
111
- ...options ,
112
- plugins,
113
- } ;
114
-
115
- mergedOptions . postcssOptions . plugins = plugins ;
116
-
117
- const resultPlugins = mergedOptions . postcssOptions . plugins ;
118
-
119
89
const useSourceMap =
120
90
typeof options . sourceMap !== 'undefined'
121
91
? options . sourceMap
122
92
: this . sourceMap ;
123
93
124
- const sourceMapNormalized =
125
- sourceMap && useSourceMap ? normalizeSourceMap ( sourceMap ) : null ;
94
+ const { plugins, processOptions, needExecute } = getPostcssOptions (
95
+ this ,
96
+ loadedConfig ,
97
+ options . postcssOptions
98
+ ) ;
126
99
127
- if ( sourceMapNormalized ) {
128
- sourceMapNormalized . sources = sourceMapNormalized . sources . map ( ( src ) =>
129
- getSourceMapRelativePath ( src , path . dirname ( file ) )
130
- ) ;
131
- }
132
-
133
- const postcssOptions = {
134
- from : file ,
135
- to : file ,
136
- map : useSourceMap
137
- ? options . sourceMap === 'inline'
138
- ? { inline : true , annotation : false }
139
- : { inline : false , annotation : false }
140
- : false ,
141
- ...mergedOptions . postcssOptions ,
142
- } ;
143
-
144
- if ( postcssOptions . map && sourceMapNormalized ) {
145
- postcssOptions . map . prev = sourceMapNormalized ;
146
- }
147
-
148
- if ( postcssOptions . parser === 'postcss-js' ) {
100
+ if ( options . exec || needExecute ) {
149
101
// eslint-disable-next-line no-param-reassign
150
102
content = exec ( content , this ) ;
151
103
}
152
104
153
- if ( typeof postcssOptions . parser === 'string' ) {
154
- try {
155
- // eslint-disable-next-line import/no-dynamic-require, global-require
156
- postcssOptions . parser = require ( postcssOptions . parser ) ;
157
- } catch ( error ) {
158
- this . emitError (
159
- `Loading PostCSS Parser failed: ${ error . message } \n\n(@${ file } )`
160
- ) ;
161
- }
162
- }
105
+ if ( useSourceMap ) {
106
+ processOptions . map =
107
+ options . sourceMap === 'inline'
108
+ ? { inline : true , annotation : false }
109
+ : { inline : false , annotation : false } ;
163
110
164
- if ( typeof postcssOptions . syntax === 'string' ) {
165
- try {
166
- // eslint-disable-next-line import/no-dynamic-require, global-require
167
- postcssOptions . syntax = require ( postcssOptions . syntax ) ;
168
- } catch ( error ) {
169
- this . emitError (
170
- `Loading PostCSS Syntax failed: ${ error . message } \n\n(@${ file } )`
171
- ) ;
172
- }
173
- }
111
+ if ( sourceMap ) {
112
+ const sourceMapNormalized = normalizeSourceMap ( sourceMap ) ;
174
113
175
- if ( typeof postcssOptions . stringifier === 'string' ) {
176
- try {
177
- // eslint-disable-next-line import/no-dynamic-require, global-require
178
- postcssOptions . stringifier = require ( postcssOptions . stringifier ) ;
179
- } catch ( error ) {
180
- this . emitError (
181
- `Loading PostCSS Stringifier failed: ${ error . message } \n\n(@${ file } )`
114
+ sourceMapNormalized . sources = sourceMapNormalized . sources . map ( ( src ) =>
115
+ getSourceMapRelativePath ( src , path . dirname ( file ) )
182
116
) ;
183
- }
184
- }
185
117
186
- if ( mergedOptions . exec ) {
187
- // eslint-disable-next-line no-param-reassign
188
- content = exec ( content , this ) ;
118
+ processOptions . map . prev = sourceMapNormalized ;
119
+ }
189
120
}
190
121
191
122
let result ;
192
123
193
124
try {
194
- result = await postcss ( resultPlugins ) . process ( content , postcssOptions ) ;
125
+ result = await postcss ( plugins ) . process ( content , processOptions ) ;
195
126
} catch ( error ) {
196
127
if ( error . file ) {
197
128
this . addDependency ( error . file ) ;
@@ -206,14 +137,11 @@ export default async function loader(content, sourceMap, meta = {}) {
206
137
return ;
207
138
}
208
139
209
- const { css, root, processor, messages } = result ;
210
- let { map } = result ;
211
-
212
140
result . warnings ( ) . forEach ( ( warning ) => {
213
141
this . emitWarning ( new Warning ( warning ) ) ;
214
142
} ) ;
215
143
216
- messages . forEach ( ( message ) => {
144
+ result . messages . forEach ( ( message ) => {
217
145
if ( message . type === 'dependency' ) {
218
146
this . addDependency ( message . file ) ;
219
147
}
@@ -228,52 +156,23 @@ export default async function loader(content, sourceMap, meta = {}) {
228
156
}
229
157
} ) ;
230
158
231
- map = map ? map . toJSON ( ) : null ;
159
+ const map = result . map ? result . map . toJSON ( ) : null ;
232
160
233
161
if ( map && useSourceMap ) {
234
162
if ( typeof map . file !== 'undefined' ) {
235
163
delete map . file ;
236
164
}
237
165
238
- map . sources = map . sources . map ( ( src ) =>
239
- getSourceMapAbsolutePath ( src , postcssOptions . to )
240
- ) ;
166
+ map . sources = map . sources . map ( ( src ) => getSourceMapAbsolutePath ( src , file ) ) ;
241
167
}
242
168
243
169
const ast = {
244
170
type : 'postcss' ,
245
- version : processor . version ,
246
- root,
171
+ version : result . processor . version ,
172
+ root : result . result ,
247
173
} ;
248
174
249
- const newMeta = { ...meta , ast, messages } ;
250
-
251
- /**
252
- * @memberof loader
253
- * @callback callback
254
- *
255
- * @param {Object } null Error
256
- * @param {String } css Result (Raw Module)
257
- * @param {Object } map Source Map
258
- */
259
- callback ( null , css , map , newMeta ) ;
260
- }
175
+ const newMeta = { ...meta , ast } ;
261
176
262
- /**
263
- * @author Andrey Sitnik (@ai) <[email protected] >
264
- *
265
- * @license MIT
266
- * @version 3.0.0
267
- *
268
- * @module postcss-loader
269
- *
270
- * @requires path
271
- *
272
- * @requires loader-utils
273
- * @requires schema-utils
274
- *
275
- * @requires postcss
276
- *
277
- * @requires ./Warning.js
278
- * @requires ./SyntaxError.js
279
- */
177
+ callback ( null , result . css , map , newMeta ) ;
178
+ }
0 commit comments