Skip to content

Commit 33a2c35

Browse files
committed
support prioritizeNamedImports option
1 parent 3225bad commit 33a2c35

6 files changed

Lines changed: 45 additions & 6 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ Determines whether to generate named exports in the d.ts file instead of a defau
165165
}
166166
```
167167

168+
### `cmkOptions.prioritizeNamedImports`
169+
170+
Type: `boolean`, Default: `false`
171+
172+
Whether to prioritize named imports over namespace imports when adding import statements. This option effects only `cmkOptions.namedExports` is `true`.
173+
174+
When this option is `true`, `import { button } from '...'` will be added. When this option is `false`, `import button from '...'` will be added.
175+
176+
```jsonc
177+
{
178+
"compilerOptions": {
179+
// ...
180+
},
181+
"cmkOptions": {
182+
"namedExports": true,
183+
"prioritizeNamedImports": true,
184+
},
185+
}
186+
```
187+
168188
## Limitations
169189

170190
- Sass/Less are not supported to simplify the implementation
@@ -180,3 +200,7 @@ Determines whether to generate named exports in the d.ts file instead of a defau
180200
This project was created as a next-generation tool to replace [happy-css-modules](https://github.com/mizdra/happy-css-modules)
181201

182202
happy-css-modules was also a tool that provided language functionality for `*.module.css`. However, the tool was limited in the type of language features it could provide. To solve this limitation, css-modules-kit was created.
203+
204+
```
205+
206+
```

packages/core/src/config.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ describe('readTsConfigFile', () => {
2929
"cmkOptions": {
3030
"dtsOutDir": "generated/cmk",
3131
"arbitraryExtensions": false,
32-
"namedExports": true
32+
"namedExports": true,
33+
"prioritizeNamedImports": true
3334
}
3435
}
3536
`,
@@ -42,6 +43,7 @@ describe('readTsConfigFile', () => {
4243
dtsOutDir: 'generated/cmk',
4344
arbitraryExtensions: false,
4445
namedExports: true,
46+
prioritizeNamedImports: true,
4547
},
4648
compilerOptions: expect.objectContaining({
4749
module: ts.ModuleKind.ESNext,

packages/core/src/config.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export interface CMKConfig {
1515
excludes: string[];
1616
dtsOutDir: string;
1717
arbitraryExtensions: boolean;
18-
/** Whether to generate named exports in the d.ts file instead of a default export. */
1918
namedExports: boolean;
19+
prioritizeNamedImports: boolean;
2020
/**
2121
* A root directory to resolve relative path entries in the config file to.
2222
* This is an absolute path.
@@ -71,6 +71,7 @@ interface UnnormalizedRawConfig {
7171
dtsOutDir?: string;
7272
arbitraryExtensions?: boolean;
7373
namedExports?: boolean;
74+
prioritizeNamedImports?: boolean;
7475
}
7576

7677
/**
@@ -148,6 +149,17 @@ function parseRawData(raw: unknown, tsConfigSourceFile: ts.TsConfigSourceFile):
148149
});
149150
}
150151
}
152+
if ('prioritizeNamedImports' in raw.cmkOptions) {
153+
if (typeof raw.cmkOptions.prioritizeNamedImports === 'boolean') {
154+
result.config.prioritizeNamedImports = raw.cmkOptions.prioritizeNamedImports;
155+
} else {
156+
result.diagnostics.push({
157+
category: 'error',
158+
text: `\`prioritizeNamedImports\` in ${tsConfigSourceFile.fileName} must be a boolean.`,
159+
// MEMO: Location information can be obtained from `tsConfigSourceFile.statements`, but this is complicated and will be omitted.
160+
});
161+
}
162+
}
151163
}
152164
return result;
153165
}
@@ -242,6 +254,7 @@ export function readConfigFile(project: string): CMKConfig {
242254
dtsOutDir: join(basePath, config.dtsOutDir ?? 'generated'),
243255
arbitraryExtensions: config.arbitraryExtensions ?? false,
244256
namedExports: config.namedExports ?? false,
257+
prioritizeNamedImports: config.prioritizeNamedImports ?? false,
245258
basePath,
246259
configFileName,
247260
compilerOptions,

packages/ts-plugin/src/language-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function createCSSLanguagePlugin(
5959
matchesPattern,
6060
namedExports: config.namedExports,
6161
});
62-
if (config.namedExports) {
62+
if (config.namedExports && !config.prioritizeNamedImports) {
6363
// Export `styles` to appear in code completion suggestions
6464
text += 'declare const styles: {};\nexport default styles;\n';
6565
}

packages/ts-plugin/src/language-service/feature/code-fix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getCodeFixesAtPosition(
2929
),
3030
);
3131

32-
if (config.namedExports) {
32+
if (config.namedExports && !config.prioritizeNamedImports) {
3333
convertDefaultImportsToNamespaceImports(prior, fileName, resolver);
3434
}
3535

packages/ts-plugin/src/language-service/feature/completion.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function getCompletionsAtPosition(
3030
}
3131
}
3232
}
33-
if (config.namedExports) {
33+
if (config.namedExports && !config.prioritizeNamedImports) {
3434
// When `namedExports` is enabled, you can write code as follows:
3535
// ```tsx
3636
// import { button } from './a.module.css';
@@ -106,7 +106,7 @@ export function getCompletionEntryDetails(
106106
);
107107
if (!details) return undefined;
108108

109-
if (config.namedExports && details.codeActions) {
109+
if (config.namedExports && !config.prioritizeNamedImports && details.codeActions) {
110110
convertDefaultImportsToNamespaceImports(details.codeActions, fileName, resolver);
111111
}
112112
return details;

0 commit comments

Comments
 (0)