Skip to content

Commit 1fca02e

Browse files
authored
Merge pull request #1 from FinestV/master
Add orderAlphabetically option
2 parents 2bb351f + 3137d46 commit 1fca02e

9 files changed

+74
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ export const barBaz: string;
5757

5858
`css-loader` exports mappings to `exports.locals` which is incompatible with the `namedExport`-option unless paired with `extract-text-webpack-plugin` or `style-loader`. They move the exported properties from `exports.locals` to `exports` making them required for `namedExport` to work, and `namedExport` required for them to work. *Always combine usage of `extract-text-webpack-plugin` or `style-loader` with the `namedExport`-option.*
5959

60+
### `orderAlphabetically`-option
61+
Orders generated exports or interface properties alphabetically. This is useful when committing the .d.ts files as the default ordering is not always consistent and can change from commit to commit.
62+
e.g.:
63+
64+
```js
65+
{ test: /\.css$/, loader: 'typings-for-css-modules-loader?modules&orderAlphabetically' }
66+
```
67+
6068
### `silent`-option
6169
To silence the loader because you get annoyed by its warnings or for other reasons, you can simply pass the "silent" query to the loader and it will shut up.
6270
e.g.:

src/cssModuleToInterface.js

Lines changed: 14 additions & 8 deletions
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

Lines changed: 4 additions & 2 deletions
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

test/entry.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {locals as stylesBase} from './example.css';
22
import {locals as stylesCamelCase} from './example-camelcase.css';
3+
import {locals as stylesOrderAlphabetically} from './example-orderalphabetically.css';
34
import * as stylesNamedExport from './example-namedexport.css';
45
import * as stylesCamelCasedNamedExport from './example-camelcase-namedexport.css';
6+
import * as stylesNamedExportOrderAlphabetically from './example-namedexport-orderalphabetically.css';
57
import './example-no-css-modules.css';
68
import * as compose from './example-compose.css';
79

10+
811
const foo = stylesBase.foo;
912
const barBaz = stylesBase['bar-baz'];
1013

@@ -17,4 +20,12 @@ const fooNamedExport = stylesNamedExport.foo;
1720
const fooCamelCaseNamedExport = stylesCamelCasedNamedExport.foo;
1821
const barBazCamelCaseNamedExport = stylesCamelCasedNamedExport.barBaz;
1922

23+
const fooStylesOrderAlphabetically = stylesOrderAlphabetically.foo;
24+
const barStylesOrderAlphabetically = stylesOrderAlphabetically.bar;
25+
const bazStylesOrderAlphabetically = stylesOrderAlphabetically.baz;
26+
27+
const fooNamedExportOrderAlhpabetically = stylesNamedExportOrderAlphabetically.foo;
28+
const barNamedExportOrderAlhpabetically = stylesNamedExportOrderAlphabetically.bar;
29+
const bazNamedExportOrderAlhpabetically = stylesNamedExportOrderAlphabetically.baz;
30+
2031
const composed = compose.test;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo {
2+
color: white;
3+
}
4+
5+
.baz {
6+
color: red;
7+
}
8+
9+
.bar {
10+
color: green;
11+
}
12+

test/example-orderalphabetically.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo {
2+
color: white;
3+
}
4+
5+
.baz {
6+
color: red;
7+
}
8+
9+
.bar {
10+
color: green;
11+
}
12+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const bar: string;
2+
export const baz: string;
3+
export const foo: string;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface IExampleOrderalphabeticallyCss {
2+
'bar': string;
3+
'baz': string;
4+
'foo': string;
5+
}
6+
7+
export const locals: IExampleOrderalphabeticallyCss;

test/webpack.config.babel.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ module.exports = {
1212
{ test: /example-namedexport\.css$/, loader: '../src/index.js?modules&namedExport' },
1313
{ test: /example-camelcase-namedexport\.css$/, loader: '../src/index.js?modules&camelCase&namedExport' },
1414
{ test: /example-no-css-modules\.css$/, loader: '../src/index.js' },
15-
{ test: /example-compose\.css$/, loader: '../src/index.js?modules&camelCase&namedExport' }
15+
{ test: /example-compose\.css$/, loader: '../src/index.js?modules&camelCase&namedExport' },
16+
{ test: /example-orderalphabetically\.css$/, loader: '../src/index.js?modules&orderAlphabetically' },
17+
{ test: /example-namedexport-orderalphabetically\.css$/, loader: '../src/index.js?modules&namedExport&orderAlphabetically' }
1618
]
1719
}
1820
};

0 commit comments

Comments
 (0)