@@ -9,9 +9,9 @@ import Warning from './Warning';
99import SyntaxError from './Error' ;
1010import schema from './options.json' ;
1111import {
12- exec ,
1312 loadConfig ,
14- getArrayPlugins ,
13+ getPostcssOptions ,
14+ exec ,
1515 getSourceMapAbsolutePath ,
1616 getSourceMapRelativePath ,
1717 normalizeSourceMap ,
@@ -38,9 +38,7 @@ export default async function loader(content, sourceMap, meta = {}) {
3838 baseDataPath : 'options' ,
3939 } ) ;
4040
41- const callback = this . async ( ) ;
4241 const file = this . resourcePath ;
43-
4442 const configOptions =
4543 typeof options . postcssOptions === 'undefined' ||
4644 typeof options . postcssOptions . config === 'undefined'
@@ -49,6 +47,8 @@ export default async function loader(content, sourceMap, meta = {}) {
4947
5048 let loadedConfig = { } ;
5149
50+ const callback = this . async ( ) ;
51+
5252 if ( configOptions ) {
5353 const dataForLoadConfig = {
5454 path : path . dirname ( file ) ,
@@ -86,112 +86,43 @@ export default async function loader(content, sourceMap, meta = {}) {
8686 }
8787 }
8888
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-
11989 const useSourceMap =
12090 typeof options . sourceMap !== 'undefined'
12191 ? options . sourceMap
12292 : this . sourceMap ;
12393
124- const sourceMapNormalized =
125- sourceMap && useSourceMap ? normalizeSourceMap ( sourceMap ) : null ;
94+ const { plugins, processOptions, needExecute } = getPostcssOptions (
95+ this ,
96+ loadedConfig ,
97+ options . postcssOptions
98+ ) ;
12699
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 ) {
149101 // eslint-disable-next-line no-param-reassign
150102 content = exec ( content , this ) ;
151103 }
152104
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 } ;
163110
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 ) ;
174113
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 ) )
182116 ) ;
183- }
184- }
185117
186- if ( mergedOptions . exec ) {
187- // eslint-disable-next-line no-param-reassign
188- content = exec ( content , this ) ;
118+ processOptions . map . prev = sourceMapNormalized ;
119+ }
189120 }
190121
191122 let result ;
192123
193124 try {
194- result = await postcss ( resultPlugins ) . process ( content , postcssOptions ) ;
125+ result = await postcss ( plugins ) . process ( content , processOptions ) ;
195126 } catch ( error ) {
196127 if ( error . file ) {
197128 this . addDependency ( error . file ) ;
@@ -206,14 +137,11 @@ export default async function loader(content, sourceMap, meta = {}) {
206137 return ;
207138 }
208139
209- const { css, root, processor, messages } = result ;
210- let { map } = result ;
211-
212140 result . warnings ( ) . forEach ( ( warning ) => {
213141 this . emitWarning ( new Warning ( warning ) ) ;
214142 } ) ;
215143
216- messages . forEach ( ( message ) => {
144+ result . messages . forEach ( ( message ) => {
217145 if ( message . type === 'dependency' ) {
218146 this . addDependency ( message . file ) ;
219147 }
@@ -228,52 +156,23 @@ export default async function loader(content, sourceMap, meta = {}) {
228156 }
229157 } ) ;
230158
231- map = map ? map . toJSON ( ) : null ;
159+ const map = result . map ? result . map . toJSON ( ) : null ;
232160
233161 if ( map && useSourceMap ) {
234162 if ( typeof map . file !== 'undefined' ) {
235163 delete map . file ;
236164 }
237165
238- map . sources = map . sources . map ( ( src ) =>
239- getSourceMapAbsolutePath ( src , postcssOptions . to )
240- ) ;
166+ map . sources = map . sources . map ( ( src ) => getSourceMapAbsolutePath ( src , file ) ) ;
241167 }
242168
243169 const ast = {
244170 type : 'postcss' ,
245- version : processor . version ,
246- root,
171+ version : result . processor . version ,
172+ root : result . result ,
247173 } ;
248174
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 } ;
261176
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