@@ -6,18 +6,24 @@ const filenameToInterfaceName = (filename) => {
6
6
. replace ( / \W + ( \w ) / g, ( _ , c ) => c . toUpperCase ( ) ) ;
7
7
} ;
8
8
9
- const cssModuleToTypescriptInterfaceProperties = ( cssModuleKeys , indent = ' ' ) => {
10
- return cssModuleKeys
9
+ const cssModuleToTypescriptInterfaceProperties = ( cssModuleKeys , orderAlphabetically , indent = ' ' ) => {
10
+ return sortCssModuleKeys ( cssModuleKeys , orderAlphabetically )
11
11
. map ( ( key ) => `${ indent } '${ key } ': string;` )
12
12
. join ( '\n' ) ;
13
13
} ;
14
14
15
- const cssModuleToNamedExports = ( cssModuleKeys ) => {
16
- return cssModuleKeys
15
+ const cssModuleToNamedExports = ( cssModuleKeys , orderAlphabetically ) => {
16
+ return sortCssModuleKeys ( cssModuleKeys , orderAlphabetically )
17
17
. map ( ( key ) => `export const ${ key } : string;` )
18
18
. join ( '\n' ) ;
19
19
} ;
20
20
21
+ const sortCssModuleKeys = ( cssModuleKeys , orderAlphabetically ) => {
22
+ return orderAlphabetically
23
+ ? [ ...cssModuleKeys ] . sort ( )
24
+ : [ ...cssModuleKeys ]
25
+ } ;
26
+
21
27
const allWordsRegexp = / ^ \w + $ / i;
22
28
export const filterNonWordClasses = ( cssModuleKeys ) => {
23
29
const filteredClassNames = cssModuleKeys . filter ( classname => allWordsRegexp . test ( classname ) ) ;
@@ -80,15 +86,15 @@ export const filenameToTypingsFilename = (filename) => {
80
86
return path . join ( dirName , `${ baseName } .d.ts` ) ;
81
87
} ;
82
88
83
- export const generateNamedExports = ( cssModuleKeys ) => {
84
- const namedExports = cssModuleToNamedExports ( cssModuleKeys ) ;
89
+ export const generateNamedExports = ( cssModuleKeys , orderAlphabetically ) => {
90
+ const namedExports = cssModuleToNamedExports ( cssModuleKeys , orderAlphabetically ) ;
85
91
return ( `${ namedExports }
86
92
` ) ;
87
93
} ;
88
94
89
- export const generateGenericExportInterface = ( cssModuleKeys , filename , indent ) => {
95
+ export const generateGenericExportInterface = ( cssModuleKeys , filename , orderAlphabetically , indent ) => {
90
96
const interfaceName = filenameToInterfaceName ( filename ) ;
91
- const interfaceProperties = cssModuleToTypescriptInterfaceProperties ( cssModuleKeys , indent ) ;
97
+ const interfaceProperties = cssModuleToTypescriptInterfaceProperties ( cssModuleKeys , orderAlphabetically , indent ) ;
92
98
return (
93
99
`export interface ${ interfaceName } {
94
100
${ interfaceProperties }
0 commit comments