Skip to content

Commit e7342df

Browse files
busheemattcompiles
authored andcommitted
feat: Support all possible class names characters
BREAKING CHANGE: Updates the declaration template. This change allows any possible class name to be exported - even those whose names don't comply with typescript variable name constraints. Resolves seek-oss#6.
1 parent 90f3131 commit e7342df

File tree

7 files changed

+43
-21
lines changed

7 files changed

+43
-21
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ There are currently a lot of [solutions to this problem](https://www.npmjs.com/s
1414

1515
- Ensures committed TypeScript declarations are in sync with the code that generated them via the [`verify` mode](#verify-mode).
1616

17-
- Doesn't silently ignore invalid TypeScript identifiers. If a class name is not valid TypeScript (e.g. `.foo-bar`, instead of `.fooBar`), `tsc` should report an error.
18-
1917
## Usage
2018

2119
Place `css-modules-typescript-loader` directly after `css-loader` in your webpack config.

index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const LineDiff = require('line-diff');
66
const bannerMessage =
77
'// This file is automatically generated.\n// Please do not change this file!';
88

9+
const cssModuleExport = 'declare var cssExports: CssExports;\nexport = cssExports;\n';
10+
911
const getNoDeclarationFileError = ({ filename }) =>
1012
new Error(
1113
`Generated type declaration does not exist. Run webpack and commit the type declaration for '${filename}'`
@@ -19,12 +21,13 @@ const getTypeMismatchError = ({ filename, expected, actual }) => {
1921
);
2022
};
2123

22-
const cssModuleToNamedExports = cssModuleKeys => {
23-
return cssModuleKeys
24+
const cssModuleToInterface = (cssModuleKeys) => {
25+
const interfaceFields = cssModuleKeys
2426
.sort()
25-
.map(key => `export const ${key}: string;`)
26-
.join('\n')
27-
.concat('\n');
27+
.map(key => ` '${key}': string;`)
28+
.join('\n');
29+
30+
return `interface CssExports {\n${interfaceFields}\n}`;
2831
};
2932

3033
const filenameToTypingsFilename = filename => {
@@ -70,9 +73,7 @@ module.exports = function(content, ...rest) {
7073
}
7174
}
7275

73-
const cssModuleDefinition = `${bannerMessage}\n${cssModuleToNamedExports(
74-
cssModuleKeys
75-
)}`;
76+
const cssModuleDefinition = `${bannerMessage}\n${cssModuleToInterface(cssModuleKeys)}\n${cssModuleExport}`;
7677

7778
if (mode === 'verify') {
7879
read((err, fileContents) => {

test/emit-declaration/__snapshots__/emit-declaration.test.js.snap

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
exports[`Can emit valid declaration 1`] = `
44
"// This file is automatically generated.
55
// Please do not change this file!
6-
export const otherClass: string;
7-
export const someClass: string;
8-
export const validClass: string;
6+
interface CssExports {
7+
'otherClass': string;
8+
'someClass': string;
9+
'validClass': string;
10+
}
11+
declare var cssExports: CssExports;
12+
export = cssExports;
913
"
1014
`;

test/verify-invalid-declaration/__snapshots__/verify-invalid-declaration.test.js.snap

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ exports[`Can error on invalid declaration 1`] = `
55
66
// This file is automatically generated.
77
// Please do not change this file!
8-
export const classInBothFiles: string;
9-
- export const classInTypeScriptFile: string;
10-
+ export const classInCssFile: string;
8+
interface CssExports {
9+
'classInBothFiles': string;
10+
- 'classInTypeScriptFile': string;
11+
+ 'classInCssFile': string;
12+
}
13+
declare var cssExports: CssExports;
14+
export = cssExports;
1115
1216
1317
"
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// This file is automatically generated.
22
// Please do not change this file!
3-
export const classInBothFiles: string;
4-
export const classInTypeScriptFile: string;
3+
interface CssExports {
4+
'classInBothFiles': string;
5+
'classInTypeScriptFile': string;
6+
}
7+
declare var cssExports: CssExports;
8+
export = cssExports;

test/verify-valid-declaration/index.css

+5
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@
99
.otherClass {
1010
display: block;
1111
}
12+
13+
.hyphened-classname,
14+
.underscored_classname {
15+
color: papayawhip;
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// This file is automatically generated.
22
// Please do not change this file!
3-
export const otherClass: string;
4-
export const someClass: string;
5-
export const validClass: string;
3+
interface CssExports {
4+
'hyphened-classname': string;
5+
'otherClass': string;
6+
'someClass': string;
7+
'underscored_classname': string;
8+
'validClass': string;
9+
}
10+
declare var cssExports: CssExports;
11+
export = cssExports;

0 commit comments

Comments
 (0)