Skip to content

Allow any class names to be exported #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ There are currently a lot of [solutions to this problem](https://www.npmjs.com/s

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

- 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.

## Usage

Place `css-modules-typescript-loader` directly after `css-loader` in your webpack config.
Expand Down
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const LineDiff = require('line-diff');
const bannerMessage =
'// This file is automatically generated.\n// Please do not change this file!';

const cssModuleExport = 'declare var cssExports: CssExports;\nexport = cssExports;\n';

const getNoDeclarationFileError = ({ filename }) =>
new Error(
`Generated type declaration does not exist. Run webpack and commit the type declaration for '${filename}'`
Expand All @@ -19,12 +21,13 @@ const getTypeMismatchError = ({ filename, expected, actual }) => {
);
};

const cssModuleToNamedExports = cssModuleKeys => {
return cssModuleKeys
const cssModuleToInterface = (cssModuleKeys) => {
const interfaceFields = cssModuleKeys
.sort()
.map(key => `export const ${key}: string;`)
.join('\n')
.concat('\n');
.map(key => ` '${key}': string;`)
.join('\n');

return `interface CssExports {\n${interfaceFields}\n}`;
};

const filenameToTypingsFilename = filename => {
Expand Down Expand Up @@ -70,9 +73,7 @@ module.exports = function(content, ...rest) {
}
}

const cssModuleDefinition = `${bannerMessage}\n${cssModuleToNamedExports(
cssModuleKeys
)}`;
const cssModuleDefinition = `${bannerMessage}\n${cssModuleToInterface(cssModuleKeys)}\n${cssModuleExport}`;

if (mode === 'verify') {
read((err, fileContents) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
exports[`Can emit valid declaration 1`] = `
"// This file is automatically generated.
// Please do not change this file!
export const otherClass: string;
export const someClass: string;
export const validClass: string;
interface CssExports {
'otherClass': string;
'someClass': string;
'validClass': string;
}
declare var cssExports: CssExports;
export = cssExports;
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ exports[`Can error on invalid declaration 1`] = `

// This file is automatically generated.
// Please do not change this file!
export const classInBothFiles: string;
- export const classInTypeScriptFile: string;
+ export const classInCssFile: string;
interface CssExports {
'classInBothFiles': string;
- 'classInTypeScriptFile': string;
+ 'classInCssFile': string;
}
declare var cssExports: CssExports;
export = cssExports;


"
Expand Down
8 changes: 6 additions & 2 deletions test/verify-invalid-declaration/index.css.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// This file is automatically generated.
// Please do not change this file!
export const classInBothFiles: string;
export const classInTypeScriptFile: string;
interface CssExports {
'classInBothFiles': string;
'classInTypeScriptFile': string;
}
declare var cssExports: CssExports;
export = cssExports;
5 changes: 5 additions & 0 deletions test/verify-valid-declaration/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
.otherClass {
display: block;
}

.hyphened-classname,
.underscored_classname {
color: papayawhip;
}
12 changes: 9 additions & 3 deletions test/verify-valid-declaration/index.css.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// This file is automatically generated.
// Please do not change this file!
export const otherClass: string;
export const someClass: string;
export const validClass: string;
interface CssExports {
'hyphened-classname': string;
'otherClass': string;
'someClass': string;
'underscored_classname': string;
'validClass': string;
}
declare var cssExports: CssExports;
export = cssExports;