Skip to content

Commit 4f451ce

Browse files
committed
Add orderAlphabetically to the loader options.
Use this option to sort the module key strings alphabetically
1 parent 2bb351f commit 4f451ce

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/cssModuleToInterface.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ const filenameToInterfaceName = (filename) => {
66
.replace(/\W+(\w)/g, (_, c) => c.toUpperCase());
77
};
88

9-
const cssModuleToTypescriptInterfaceProperties = (cssModuleKeys, indent = ' ') => {
10-
return cssModuleKeys
9+
const cssModuleToTypescriptInterfaceProperties = (cssModuleKeys, orderAlphabetically, indent = ' ') => {
10+
return sortCssModuleKeys(cssModuleKeys, orderAlphabetically)
1111
.map((key) => `${indent}'${key}': string;`)
1212
.join('\n');
1313
};
1414

15-
const cssModuleToNamedExports = (cssModuleKeys) => {
16-
return cssModuleKeys
15+
const cssModuleToNamedExports = (cssModuleKeys, orderAlphabetically) => {
16+
return sortCssModuleKeys(cssModuleKeys, orderAlphabetically)
1717
.map((key) => `export const ${key}: string;`)
1818
.join('\n');
1919
};
2020

21+
const sortCssModuleKeys = (cssModuleKeys, orderAlphabetically) => {
22+
return orderAlphabetically
23+
? [...cssModuleKeys].sort()
24+
: [...cssModuleKeys]
25+
};
26+
2127
const allWordsRegexp = /^\w+$/i;
2228
export const filterNonWordClasses = (cssModuleKeys) => {
2329
const filteredClassNames = cssModuleKeys.filter(classname => allWordsRegexp.test(classname));
@@ -80,15 +86,15 @@ export const filenameToTypingsFilename = (filename) => {
8086
return path.join(dirName, `${baseName}.d.ts`);
8187
};
8288

83-
export const generateNamedExports = (cssModuleKeys) => {
84-
const namedExports = cssModuleToNamedExports(cssModuleKeys);
89+
export const generateNamedExports = (cssModuleKeys, orderAlphabetically) => {
90+
const namedExports = cssModuleToNamedExports(cssModuleKeys, orderAlphabetically);
8591
return (`${namedExports}
8692
`);
8793
};
8894

89-
export const generateGenericExportInterface = (cssModuleKeys, filename, indent) => {
95+
export const generateGenericExportInterface = (cssModuleKeys, filename, orderAlphabetically, indent) => {
9096
const interfaceName = filenameToInterfaceName(filename);
91-
const interfaceProperties = cssModuleToTypescriptInterfaceProperties(cssModuleKeys, indent);
97+
const interfaceProperties = cssModuleToTypescriptInterfaceProperties(cssModuleKeys, orderAlphabetically, indent);
9298
return (
9399
`export interface ${interfaceName} {
94100
${interfaceProperties}

src/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ module.exports = function(...input) {
5151
}
5252
}
5353

54+
query.orderAlphabetically = !!query.orderAlphabetically;
55+
5456
let cssModuleDefinition;
5557
if (!query.namedExport) {
56-
cssModuleDefinition = generateGenericExportInterface(cssModuleKeys, filename);
58+
cssModuleDefinition = generateGenericExportInterface(cssModuleKeys, filename, query.orderAlphabetically);
5759
} else {
5860
const [cleanedDefinitions, skippedDefinitions,] = filterNonWordClasses(cssModuleKeys);
5961
if (skippedDefinitions.length > 0 && !query.camelCase) {
@@ -72,7 +74,7 @@ These can be accessed using the object literal syntax; eg styles['delete'] inste
7274
`.yellow);
7375
}
7476

75-
cssModuleDefinition = generateNamedExports(nonReservedWordDefinitions);
77+
cssModuleDefinition = generateNamedExports(nonReservedWordDefinitions, query.orderAlphabetically);
7678
}
7779
if (cssModuleDefinition.trim() === '') {
7880
// Ensure empty CSS modules export something

0 commit comments

Comments
 (0)