@@ -18,19 +18,54 @@ import type {
18
18
StyleModuleMapType
19
19
} from './types' ;
20
20
21
- const getTokens = ( runner , cssSourceFilePath : string , filetypes ) : StyleModuleMapType => {
21
+ type FileTypeOptions = { |
22
+ syntax : string ,
23
+ plugins : Array < string >
24
+ | } ;
25
+
26
+ const getFiletypeOptions = ( cssSourceFilePath : string , filetypes : Object ) : ?( string | FileTypeOptions ) = > {
22
27
const extension = cssSourceFilePath . substr ( cssSourceFilePath . lastIndexOf ( '.' ) ) ;
23
- const syntax = filetypes [ extension ] ;
28
+ const filetype = filetypes ? filetypes [ extension ] : null ;
24
29
25
- const options : Object = {
26
- from : cssSourceFilePath
27
- } ;
30
+ return filetype ;
31
+ }
32
+
33
+ const getSyntax = ( filetypeOptions : ?( string | FileTypeOptions ) ) => {
34
+ if ( ! filetypeOptions ) {
35
+ return null ;
36
+ }
37
+
38
+ if ( typeof filetypeOptions === 'string' ) {
39
+ // eslint-disable-next-line import/no-dynamic-require, global-require
40
+ return require ( filetypeOptions ) ;
41
+ }
42
+
43
+ if ( typeof filetypeOptions === 'object' ) {
44
+ // eslint-disable-next-line import/no-dynamic-require, global-require
45
+ return require ( filetypeOptions . syntax ) ;
46
+ }
47
+ }
28
48
29
- if ( syntax ) {
49
+ const getExtraPlugins = ( filetypeOptions : ?( string | FileTypeOptions ) ) : Array < any > => {
50
+ console. log ( filetypeOptions ) ;
51
+ if ( ! filetypeOptions ) {
52
+ return [ ] ;
53
+ }
54
+
55
+ if ( typeof filetypeOptions === 'object' ) {
30
56
// eslint-disable-next-line import/no-dynamic-require, global-require
31
- options . syntax = require ( syntax ) ;
57
+ return filetypeOptions . plugins . map ( plugin => require ( plugin ) ) ;
32
58
}
33
59
60
+ return [ ] ;
61
+ }
62
+
63
+ const getTokens = ( runner , cssSourceFilePath : string , filetypeOptions : ?( string | FileTypeOptions ) ) : StyleModuleMapType => {
64
+ const options : Object = {
65
+ from : cssSourceFilePath ,
66
+ syntax : getSyntax ( filetypeOptions )
67
+ } ;
68
+
34
69
const lazyResult = runner
35
70
. process ( readFileSync ( cssSourceFilePath , 'utf-8' ) , options ) ;
36
71
@@ -58,14 +93,19 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
58
93
context : options . context || process . cwd ( )
59
94
} ) ;
60
95
96
+ const filetypeOptions = getFiletypeOptions ( cssSourceFilePath , options . filetypes ) ;
97
+
61
98
const fetch = ( to : string , from : string ) => {
62
99
const fromDirectoryPath = dirname ( from ) ;
63
100
const toPath = resolve ( fromDirectoryPath , to ) ;
64
101
65
- return getTokens ( runner , toPath , options . filetypes ) ;
102
+ return getTokens ( runner , toPath , filetypeOptions ) ;
66
103
} ;
67
104
105
+ const extraPlugins = getExtraPlugins ( filetypeOptions ) ;
106
+
68
107
const plugins = [
108
+ ...extraPlugins ,
69
109
Values ,
70
110
LocalByDefault ,
71
111
ExtractImports ,
@@ -79,5 +119,5 @@ export default (cssSourceFilePath: string, options: OptionsType): StyleModuleMap
79
119
80
120
runner = postcss ( plugins ) ;
81
121
82
- return getTokens ( runner , cssSourceFilePath , options . filetypes ) ;
122
+ return getTokens ( runner , cssSourceFilePath , filetypeOptions ) ;
83
123
} ;
0 commit comments