@@ -13,69 +13,55 @@ import Warning from './Warning';
1313import schema from './options.json' ;
1414import { icssParser , importParser , urlParser } from './plugins' ;
1515import {
16- getModulesOptions ,
16+ normalizeOptions ,
17+ shouldUseModulesPlugins ,
18+ shouldUseImportPlugin ,
19+ shouldUseURLPlugin ,
1720 getPreRequester ,
1821 getExportCode ,
1922 getFilter ,
2023 getImportCode ,
2124 getModuleCode ,
2225 getModulesPlugins ,
2326 normalizeSourceMap ,
24- shouldUseModulesPlugins ,
25- isUrlRequestable ,
27+ sortImports ,
2628} from './utils' ;
2729
28- export default function loader ( content , map , meta ) {
29- const options = getOptions ( this ) ;
30+ export default async function loader ( content , map , meta ) {
31+ const rawOptions = getOptions ( this ) ;
3032
31- validateOptions ( schema , options , {
33+ validateOptions ( schema , rawOptions , {
3234 name : 'CSS Loader' ,
3335 baseDataPath : 'options' ,
3436 } ) ;
3537
36- const sourceMap =
37- typeof options . sourceMap === 'boolean' ? options . sourceMap : this . sourceMap ;
3838 const plugins = [ ] ;
39+ const options = normalizeOptions ( rawOptions , this ) ;
3940
40- const exportType = options . onlyLocals ? 'locals' : 'full' ;
41- const preRequester = getPreRequester ( this ) ;
42- const urlHandler = ( url ) =>
43- stringifyRequest ( this , preRequester ( options . importLoaders ) + url ) ;
44-
45- const esModule =
46- typeof options . esModule !== 'undefined' ? options . esModule : true ;
47-
48- let modulesOptions ;
49-
50- if ( shouldUseModulesPlugins ( options . modules , this . resourcePath ) ) {
51- modulesOptions = getModulesOptions ( options , this ) ;
52-
53- if ( modulesOptions . namedExport === true && esModule === false ) {
54- this . emitError (
55- new Error (
56- '`Options.module.namedExport` cannot be used without `options.esModule`'
57- )
58- ) ;
59- }
60-
61- plugins . push ( ...getModulesPlugins ( modulesOptions , this ) ) ;
62-
41+ if ( shouldUseModulesPlugins ( options ) ) {
6342 const icssResolver = this . getResolve ( {
6443 mainFields : [ 'css' , 'style' , 'main' , '...' ] ,
6544 mainFiles : [ 'index' , '...' ] ,
45+ extensions : [ ] ,
46+ conditionNames : [ 'style' ] ,
6647 } ) ;
6748
6849 plugins . push (
50+ ...getModulesPlugins ( options , this ) ,
6951 icssParser ( {
7052 context : this . context ,
7153 rootContext : this . rootContext ,
7254 resolver : icssResolver ,
73- urlHandler,
55+ urlHandler : ( url ) =>
56+ stringifyRequest (
57+ this ,
58+ getPreRequester ( this ) ( options . importLoaders ) + url
59+ ) ,
7460 } )
7561 ) ;
7662 }
7763
78- if ( options . import !== false && exportType === 'full' ) {
64+ if ( shouldUseImportPlugin ( options ) ) {
7965 const resolver = this . getResolve ( {
8066 mainFields : [ 'css' , 'style' , 'main' , '...' ] ,
8167 mainFiles : [ 'index' , '...' ] ,
@@ -90,24 +76,27 @@ export default function loader(content, map, meta) {
9076 rootContext : this . rootContext ,
9177 filter : getFilter ( options . import , this . resourcePath ) ,
9278 resolver,
93- urlHandler,
79+ urlHandler : ( url ) =>
80+ stringifyRequest (
81+ this ,
82+ getPreRequester ( this ) ( options . importLoaders ) + url
83+ ) ,
9484 } )
9585 ) ;
9686 }
9787
98- if ( options . url !== false && exportType === 'full' ) {
88+ if ( shouldUseURLPlugin ( options ) ) {
9989 const urlResolver = this . getResolve ( {
10090 mainFields : [ 'asset' ] ,
10191 conditionNames : [ 'asset' ] ,
92+ extensions : [ ] ,
10293 } ) ;
10394
10495 plugins . push (
10596 urlParser ( {
10697 context : this . context ,
10798 rootContext : this . rootContext ,
108- filter : getFilter ( options . url , this . resourcePath , ( value ) =>
109- isUrlRequestable ( value )
110- ) ,
99+ filter : getFilter ( options . url , this . resourcePath ) ,
111100 resolver : urlResolver ,
112101 urlHandler : ( url ) => stringifyRequest ( this , url ) ,
113102 } )
@@ -130,107 +119,76 @@ export default function loader(content, map, meta) {
130119
131120 const callback = this . async ( ) ;
132121
133- postcss ( plugins )
134- . process ( content , {
122+ let result ;
123+
124+ try {
125+ result = await postcss ( plugins ) . process ( content , {
135126 from : this . resourcePath ,
136127 to : this . resourcePath ,
137128 map : options . sourceMap
138129 ? {
139130 // Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
140- prev : sourceMap && map ? normalizeSourceMap ( map ) : null ,
131+ prev : map ? normalizeSourceMap ( map ) : null ,
141132 inline : false ,
142133 annotation : false ,
143134 }
144135 : false ,
145- } )
146- . then ( ( result ) => {
147- for ( const warning of result . warnings ( ) ) {
148- this . emitWarning ( new Warning ( warning ) ) ;
149- }
150-
151- const imports = [ ] ;
152- const apiImports = [ ] ;
153- const urlReplacements = [ ] ;
154- const icssReplacements = [ ] ;
155- const exports = [ ] ;
156-
157- for ( const message of result . messages ) {
158- // eslint-disable-next-line default-case
159- switch ( message . type ) {
160- case 'import' :
161- imports . push ( message . value ) ;
162- break ;
163- case 'api-import' :
164- apiImports . push ( message . value ) ;
165- break ;
166- case 'url-replacement' :
167- urlReplacements . push ( message . value ) ;
168- break ;
169- case 'icss-replacement' :
170- icssReplacements . push ( message . value ) ;
171- break ;
172- case 'export' :
173- exports . push ( message . value ) ;
174- break ;
175- }
176- }
177-
178- /*
179- * Order
180- * CSS_LOADER_ICSS_IMPORT: [],
181- * CSS_LOADER_AT_RULE_IMPORT: [],
182- * CSS_LOADER_GET_URL_IMPORT: [],
183- * CSS_LOADER_URL_IMPORT: [],
184- * CSS_LOADER_URL_REPLACEMENT: [],
185- * */
186-
187- imports . sort ( ( a , b ) => {
188- return (
189- ( b . order < a . order ) - ( a . order < b . order ) ||
190- ( b . index < a . index ) - ( a . index < b . index )
191- ) ;
192- } ) ;
193- apiImports . sort ( ( a , b ) => {
194- return (
195- ( b . order < a . order ) - ( a . order < b . order ) ||
196- ( b . index < a . index ) - ( a . index < b . index )
197- ) ;
198- } ) ;
199-
200- const importCode = getImportCode (
201- this ,
202- exportType ,
203- imports ,
204- esModule ,
205- modulesOptions
206- ) ;
207- const moduleCode = getModuleCode (
208- result ,
209- exportType ,
210- sourceMap ,
211- apiImports ,
212- urlReplacements ,
213- icssReplacements ,
214- esModule ,
215- modulesOptions
216- ) ;
217- const exportCode = getExportCode (
218- exports ,
219- exportType ,
220- icssReplacements ,
221- esModule ,
222- modulesOptions
223- ) ;
224-
225- return callback ( null , `${ importCode } ${ moduleCode } ${ exportCode } ` ) ;
226- } )
227- . catch ( ( error ) => {
228- if ( error . file ) {
229- this . addDependency ( error . file ) ;
230- }
231-
232- callback (
233- error . name === 'CssSyntaxError' ? new CssSyntaxError ( error ) : error
234- ) ;
235136 } ) ;
137+ } catch ( error ) {
138+ if ( error . file ) {
139+ this . addDependency ( error . file ) ;
140+ }
141+
142+ callback (
143+ error . name === 'CssSyntaxError' ? new CssSyntaxError ( error ) : error
144+ ) ;
145+
146+ return ;
147+ }
148+
149+ for ( const warning of result . warnings ( ) ) {
150+ this . emitWarning ( new Warning ( warning ) ) ;
151+ }
152+
153+ const imports = [ ] ;
154+ const apiImports = [ ] ;
155+ const urlReplacements = [ ] ;
156+ const icssReplacements = [ ] ;
157+ const exports = [ ] ;
158+
159+ for ( const message of result . messages ) {
160+ // eslint-disable-next-line default-case
161+ switch ( message . type ) {
162+ case 'import' :
163+ imports . push ( message . value ) ;
164+ break ;
165+ case 'api-import' :
166+ apiImports . push ( message . value ) ;
167+ break ;
168+ case 'url-replacement' :
169+ urlReplacements . push ( message . value ) ;
170+ break ;
171+ case 'icss-replacement' :
172+ icssReplacements . push ( message . value ) ;
173+ break ;
174+ case 'export' :
175+ exports . push ( message . value ) ;
176+ break ;
177+ }
178+ }
179+
180+ imports . sort ( sortImports ) ;
181+ apiImports . sort ( sortImports ) ;
182+
183+ const importCode = getImportCode ( this , imports , options ) ;
184+ const moduleCode = getModuleCode (
185+ result ,
186+ apiImports ,
187+ urlReplacements ,
188+ icssReplacements ,
189+ options
190+ ) ;
191+ const exportCode = getExportCode ( exports , icssReplacements , options ) ;
192+
193+ callback ( null , `${ importCode } ${ moduleCode } ${ exportCode } ` ) ;
236194}
0 commit comments