Skip to content

Commit 833bf22

Browse files
committed
feat: optionally allow any additional classes
1 parent fb79442 commit 833bf22

File tree

4 files changed

+91
-9
lines changed

4 files changed

+91
-9
lines changed

src/helpers/__tests__/__snapshots__/getDtsSnapshot.test.ts.snap

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,56 @@ exports[`utils / cssSnapshots with a custom renderer should process a file and l
99
}
1010
`;
1111

12+
exports[`utils / cssSnapshots with allowAdditionalClasses enabled should return a dts file that allows any string value 1`] = `
13+
"declare let classes: {
14+
'localClassInsideGlobal': string;
15+
'localClass': string;
16+
'localClass2': string;
17+
'localClassInsideLocal': string;
18+
'reservedWords': string;
19+
'default': string;
20+
'const': string;
21+
'nestedClassParent': string;
22+
'childClass': string;
23+
'nestedClassParentExtended': string;
24+
'section1': string;
25+
'section2': string;
26+
'section3': string;
27+
'section4': string;
28+
'section5': string;
29+
'section6': string;
30+
'section7': string;
31+
'section8': string;
32+
'section9': string;
33+
'classWithMixin': string;
34+
'appLogo': string;
35+
'appLogo': string;
36+
[key: string]: string;
37+
};
38+
export default classes;
39+
export let localClassInsideGlobal: string;
40+
export let localClass: string;
41+
export let localClass2: string;
42+
export let localClassInsideLocal: string;
43+
export let reservedWords: string;
44+
export let nestedClassParent: string;
45+
export let childClass: string;
46+
export let nestedClassParentExtended: string;
47+
export let section1: string;
48+
export let section2: string;
49+
export let section3: string;
50+
export let section4: string;
51+
export let section5: string;
52+
export let section6: string;
53+
export let section7: string;
54+
export let section8: string;
55+
export let section9: string;
56+
export let classWithMixin: string;
57+
export let appLogo: string;
58+
export let appLogo: string;
59+
"
60+
`;
61+
1262
exports[`utils / cssSnapshots with baseUrl and paths in compilerOptions sass should find the files 1`] = `
1363
{
1464
"big-font": "tsconfig-paths-module__big-font---3FOK9",
@@ -19,7 +69,7 @@ exports[`utils / cssSnapshots with baseUrl and paths in compilerOptions sass sho
1969

2070
exports[`utils / cssSnapshots with file 'empty.module.less' createExports should create an exports file 1`] = `
2171
"declare let classes: {
22-
72+
2373
};
2474
export default classes;
2575
"
@@ -30,7 +80,7 @@ exports[`utils / cssSnapshots with file 'empty.module.less' getCssExports should
3080
exports[`utils / cssSnapshots with file 'empty.module.less' with a custom template should transform the generated dts 1`] = `
3181
"/* eslint-disable */
3282
declare let classes: {
33-
83+
3484
};
3585
export default classes;
3686
@@ -40,7 +90,7 @@ export type AllClassNames = '';"
4090

4191
exports[`utils / cssSnapshots with file 'empty.module.sass' createExports should create an exports file 1`] = `
4292
"declare let classes: {
43-
93+
4494
};
4595
export default classes;
4696
"
@@ -51,7 +101,7 @@ exports[`utils / cssSnapshots with file 'empty.module.sass' getCssExports should
51101
exports[`utils / cssSnapshots with file 'empty.module.sass' with a custom template should transform the generated dts 1`] = `
52102
"/* eslint-disable */
53103
declare let classes: {
54-
104+
55105
};
56106
export default classes;
57107
@@ -61,7 +111,7 @@ export type AllClassNames = '';"
61111

62112
exports[`utils / cssSnapshots with file 'empty.module.scss' createExports should create an exports file 1`] = `
63113
"declare let classes: {
64-
114+
65115
};
66116
export default classes;
67117
"
@@ -72,7 +122,7 @@ exports[`utils / cssSnapshots with file 'empty.module.scss' getCssExports should
72122
exports[`utils / cssSnapshots with file 'empty.module.scss' with a custom template should transform the generated dts 1`] = `
73123
"/* eslint-disable */
74124
declare let classes: {
75-
125+
76126
};
77127
export default classes;
78128
@@ -82,7 +132,7 @@ export type AllClassNames = '';"
82132

83133
exports[`utils / cssSnapshots with file 'empty.module.styl' createExports should create an exports file 1`] = `
84134
"declare let classes: {
85-
135+
86136
};
87137
export default classes;
88138
"
@@ -93,7 +143,7 @@ exports[`utils / cssSnapshots with file 'empty.module.styl' getCssExports should
93143
exports[`utils / cssSnapshots with file 'empty.module.styl' with a custom template should transform the generated dts 1`] = `
94144
"/* eslint-disable */
95145
declare let classes: {
96-
146+
97147
};
98148
export default classes;
99149

src/helpers/__tests__/getDtsSnapshot.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,33 @@ describe('utils / cssSnapshots', () => {
290290
expect(dts).toMatchSnapshot();
291291
});
292292
});
293+
294+
describe('with allowAdditionalClasses enabled', () => {
295+
const fileName = join(__dirname, 'fixtures', 'test.module.scss');
296+
const css = readFileSync(fileName, 'utf8');
297+
const options: Options = {
298+
classnameTransform: 'camelCaseOnly',
299+
allowAdditionalClasses: true,
300+
};
301+
302+
const cssExports = getCssExports({
303+
css,
304+
fileName,
305+
logger,
306+
options,
307+
processor,
308+
compilerOptions,
309+
directory: __dirname,
310+
});
311+
312+
it('should return a dts file that allows any string value', () => {
313+
const dts = createDtsExports({
314+
cssExports,
315+
fileName,
316+
logger,
317+
options,
318+
});
319+
expect(dts).toMatchSnapshot();
320+
});
321+
});
293322
});

src/helpers/createDtsExports.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export const createDtsExports = ({
4242

4343
let dts = `\
4444
declare let classes: {
45-
${processedClasses.map(classNameToProperty).join('\n ')}
45+
${processedClasses.map(classNameToProperty).join('\n ')}${
46+
options.allowAdditionalClasses ? '\n [key: string]: string;' : ''
47+
}
4648
};
4749
export default classes;
4850
`;

src/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface RendererOptions {
2020
}
2121

2222
export interface Options {
23+
allowAdditionalClasses?: boolean;
2324
classnameTransform?: ClassnameTransformOptions;
2425
customMatcher?: string;
2526
customRenderer?: string;

0 commit comments

Comments
 (0)