Skip to content

Commit 9b40191

Browse files
authored
Support namedExports option (#182)
* update example * update README * add E2E test for named exports * support `namedExports` options * add changelog
1 parent 3ec5b22 commit 9b40191

16 files changed

Lines changed: 614 additions & 51 deletions

File tree

.changeset/true-kiwis-kneel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@css-modules-kit/ts-plugin': minor
3+
'@css-modules-kit/codegen': minor
4+
'@css-modules-kit/core': minor
5+
---
6+
7+
feat: support `namedExports` option

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ In TypeScript, the `include`/`exclude` properties specify which `*.ts` files to
116116

117117
### `cmkOptions.dtsOutDir`
118118

119-
Specifies the directory where `*.d.ts` files are output. The default is `"generated"`.
119+
Type: `string`, Default: `"generated"`
120+
121+
Specifies the directory where `*.d.ts` files are output.
120122

121123
```jsonc
122124
{
@@ -131,7 +133,9 @@ Specifies the directory where `*.d.ts` files are output. The default is `"genera
131133

132134
### `cmkOptions.arbitraryExtensions`
133135

134-
Determines whether to generate `*.module.d.css.ts` instead of `*.module.css.d.ts`. The default is `false`.
136+
Type: `boolean`, Default: `false`
137+
138+
Determines whether to generate `*.module.d.css.ts` instead of `*.module.css.d.ts`.
135139

136140
```jsonc
137141
{
@@ -144,6 +148,23 @@ Determines whether to generate `*.module.d.css.ts` instead of `*.module.css.d.ts
144148
}
145149
```
146150

151+
### `cmkOptions.namedExports`
152+
153+
Type: `boolean`, Default: `false`
154+
155+
Determines whether to generate named exports in the d.ts file instead of a default export.
156+
157+
```jsonc
158+
{
159+
"compilerOptions": {
160+
// ...
161+
},
162+
"cmkOptions": {
163+
"namedExports": true,
164+
},
165+
}
166+
```
167+
147168
## Limitations
148169

149170
- Sass/Less are not supported to simplify the implementation
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// @ts-nocheck
2-
declare const styles = {
3-
a_1: '' as readonly string,
4-
a_2: '' as readonly string,
5-
a_2: '' as readonly string,
6-
a_3: '' as readonly string,
7-
...(await import('./b.module.css')).default,
8-
c_1: (await import('./c.module.css')).default.c_1,
9-
c_alias: (await import('./c.module.css')).default.c_2,
10-
};
11-
export default styles;
2+
export var a_1: string;
3+
export var a_2: string;
4+
export var a_2: string;
5+
export var a_3: string;
6+
export * from './b.module.css';
7+
export {
8+
c_1,
9+
c_2 as c_alias,
10+
} from './c.module.css';
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
// @ts-nocheck
2-
declare const styles = {
3-
b_1: '' as readonly string,
4-
b_2: '' as readonly string,
5-
};
6-
export default styles;
2+
export var b_1: string;
3+
export var b_2: string;
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
// @ts-nocheck
2-
declare const styles = {
3-
c_1: '' as readonly string,
4-
c_2: '' as readonly string,
5-
};
6-
export default styles;
2+
export var c_1: string;
3+
export var c_2: string;

example/generated/src/d.module.css.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

example/src/index.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
import styles from './a.module.css';
1+
import * as styles from "./a.module.css";
22

33
styles.a_1;
4-
styles.a_2;
5-
styles.a_3;
6-
styles.b_1;
7-
styles.b_2;
8-
styles.c_1;
9-
styles.c_alias;

example/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
"paths": { "@/*": ["./*"] },
1313
"rootDirs": [".", "generated"],
1414
"types": [] // Simplify tsserver.log
15+
},
16+
"cmkOptions": {
17+
"namedExports": true
1518
}
1619
}

packages/codegen/src/runner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ async function parseCSSModuleByFileName(fileName: string): Promise<ParseCSSModul
4141
*/
4242
async function writeDtsByCSSModule(
4343
cssModule: CSSModule,
44-
{ dtsOutDir, basePath, arbitraryExtensions }: CMKConfig,
44+
{ dtsOutDir, basePath, arbitraryExtensions, namedExports }: CMKConfig,
4545
resolver: Resolver,
4646
matchesPattern: MatchesPattern,
4747
): Promise<void> {
48-
const dts = createDts(cssModule, { resolver, matchesPattern });
48+
const dts = createDts(cssModule, { resolver, matchesPattern, namedExports });
4949
await writeDtsFile(dts.text, cssModule.fileName, {
5050
outDir: dtsOutDir,
5151
basePath,

packages/core/src/config.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('readTsConfigFile', () => {
2727
"module": "esnext"
2828
},
2929
"cmkOptions": {
30-
"dtsOutDir": "generated/cmk"
30+
"dtsOutDir": "generated/cmk",
31+
"arbitraryExtensions": false,
32+
"namedExports": true
3133
}
3234
}
3335
`,
@@ -38,6 +40,8 @@ describe('readTsConfigFile', () => {
3840
includes: ['src'],
3941
excludes: ['src/test'],
4042
dtsOutDir: 'generated/cmk',
43+
arbitraryExtensions: false,
44+
namedExports: true,
4145
},
4246
compilerOptions: expect.objectContaining({
4347
module: ts.ModuleKind.ESNext,

0 commit comments

Comments
 (0)