1
1
import cssLoader from 'css-loader' ;
2
2
import cssLocalsLoader from 'css-loader/locals' ;
3
+ import loaderUtils from 'loader-utils' ;
3
4
import {
4
5
generateInterface ,
5
6
filenameToTypingsFilename ,
6
7
} from './cssModuleToInterface' ;
7
8
import * as persist from './persist' ;
8
9
10
+ function delegateToCssLoader ( ctx , input , callback ) {
11
+ ctx . async = ( ) => callback ;
12
+ cssLoader . call ( ctx , input ) ;
13
+ }
14
+
9
15
module . exports = function ( input ) {
10
16
if ( this . cacheable ) this . cacheable ( ) ;
11
17
12
18
// mock async step 1 - css loader is async, we need to intercept this so we get async ourselves
13
19
const callback = this . async ( ) ;
20
+
21
+ const query = loaderUtils . parseQuery ( this . query ) ;
22
+ const moduleMode = query . modules || query . module ;
23
+ if ( ! moduleMode ) {
24
+ console . warn ( 'Typings for CSS-Modules: option `modules` is not active - skipping extraction work...' ) ;
25
+ return delegateToCssLoader ( ctx , input , callback ) ;
26
+ }
27
+
14
28
// mock async step 2 - offer css loader a "fake" callback
15
29
this . async = ( ) => ( err , content ) => {
16
30
const cssmodules = this . exec ( content , this . resource ) ;
@@ -20,8 +34,7 @@ module.exports = function(input) {
20
34
const cssModuleInterface = generateInterface ( cssmodules , requestedResource ) ;
21
35
persist . writeToFileIfChanged ( cssModuleInterfaceFilename , cssModuleInterface ) ;
22
36
// mock async step 3 - make `async` return the actual callback again before calling the 'real' css-loader
23
- this . async = ( ) => callback ;
24
- cssLoader . call ( this , input ) ;
37
+ delegateToCssLoader ( this , input , callback ) ;
25
38
} ;
26
39
cssLocalsLoader . call ( this , input ) ;
27
40
}
0 commit comments