Skip to content

Commit 0d4318d

Browse files
committed
support prioritizeNamedImports option
1 parent bcd0f57 commit 0d4318d

6 files changed

Lines changed: 56 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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('normalizeConfig', () => {
2323
dtsOutDir: undefined,
2424
arbitraryExtensions: undefined,
2525
namedExports: undefined,
26+
prioritizeNamedImports: undefined,
2627
};
2728
test('resolves options', () => {
2829
expect(
@@ -46,6 +47,7 @@ describe('normalizeConfig', () => {
4647
"/app/src",
4748
],
4849
"namedExports": false,
50+
"prioritizeNamedImports": false,
4951
}
5052
`);
5153
});
@@ -64,7 +66,8 @@ describe('readTsConfigFile', () => {
6466
"cmkOptions": {
6567
"dtsOutDir": "generated/cmk",
6668
"arbitraryExtensions": false,
67-
"namedExports": true
69+
"namedExports": true,
70+
"prioritizeNamedImports": true
6871
}
6972
}
7073
`,
@@ -77,6 +80,7 @@ describe('readTsConfigFile', () => {
7780
dtsOutDir: 'generated/cmk',
7881
arbitraryExtensions: false,
7982
namedExports: true,
83+
prioritizeNamedImports: true,
8084
},
8185
compilerOptions: expect.objectContaining({
8286
module: ts.ModuleKind.ESNext,
@@ -106,6 +110,7 @@ describe('readTsConfigFile', () => {
106110
dtsOutDir: 'generated/cmk',
107111
arbitraryExtensions: true,
108112
namedExports: undefined,
113+
prioritizeNamedImports: undefined,
109114
},
110115
compilerOptions: expect.any(Object),
111116
diagnostics: [],
@@ -137,6 +142,7 @@ describe('readTsConfigFile', () => {
137142
dtsOutDir: undefined,
138143
arbitraryExtensions: undefined,
139144
namedExports: undefined,
145+
prioritizeNamedImports: undefined,
140146
},
141147
compilerOptions: expect.objectContaining({
142148
module: undefined,
@@ -178,6 +184,7 @@ describe('readTsConfigFile', () => {
178184
dtsOutDir: 'generated/cmk',
179185
arbitraryExtensions: true,
180186
namedExports: undefined,
187+
prioritizeNamedImports: undefined,
181188
});
182189
});
183190
test('does not merge arrays and objects, but overwrites them', async () => {
@@ -202,6 +209,7 @@ describe('readTsConfigFile', () => {
202209
dtsOutDir: undefined,
203210
arbitraryExtensions: undefined,
204211
namedExports: undefined,
212+
prioritizeNamedImports: undefined,
205213
});
206214
});
207215
test('inherits from a file recursively', async () => {
@@ -229,6 +237,7 @@ describe('readTsConfigFile', () => {
229237
dtsOutDir: 'generated/cmk',
230238
arbitraryExtensions: true,
231239
namedExports: undefined,
240+
prioritizeNamedImports: undefined,
232241
});
233242
});
234243
test('inherits from a package', async () => {
@@ -250,6 +259,7 @@ describe('readTsConfigFile', () => {
250259
dtsOutDir: 'generated/cmk',
251260
arbitraryExtensions: undefined,
252261
namedExports: undefined,
262+
prioritizeNamedImports: undefined,
253263
});
254264
});
255265
test('inherits from multiple files', async () => {
@@ -276,6 +286,7 @@ describe('readTsConfigFile', () => {
276286
dtsOutDir: 'generated/cmk',
277287
arbitraryExtensions: true,
278288
namedExports: undefined,
289+
prioritizeNamedImports: undefined,
279290
});
280291
});
281292
test('ignores un-existing files', async () => {
@@ -299,6 +310,7 @@ describe('readTsConfigFile', () => {
299310
dtsOutDir: 'generated/cmk',
300311
arbitraryExtensions: true,
301312
namedExports: undefined,
313+
prioritizeNamedImports: undefined,
302314
});
303315
});
304316
});

packages/core/src/config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export interface CMKConfig {
1919
excludes: string[];
2020
dtsOutDir: string;
2121
arbitraryExtensions: boolean;
22-
/** Whether to generate named exports in the d.ts file instead of a default export. */
2322
namedExports: boolean;
23+
prioritizeNamedImports: boolean;
2424
/**
2525
* A root directory to resolve relative path entries in the config file to.
2626
* This is an absolute path.
@@ -75,6 +75,7 @@ interface UnnormalizedRawConfig {
7575
dtsOutDir: string | undefined;
7676
arbitraryExtensions: boolean | undefined;
7777
namedExports: boolean | undefined;
78+
prioritizeNamedImports: boolean | undefined;
7879
}
7980

8081
/**
@@ -102,6 +103,7 @@ function parseRawData(raw: unknown, tsConfigSourceFile: ts.TsConfigSourceFile):
102103
dtsOutDir: undefined,
103104
arbitraryExtensions: undefined,
104105
namedExports: undefined,
106+
prioritizeNamedImports: undefined,
105107
},
106108
diagnostics: [],
107109
};
@@ -158,6 +160,17 @@ function parseRawData(raw: unknown, tsConfigSourceFile: ts.TsConfigSourceFile):
158160
});
159161
}
160162
}
163+
if ('prioritizeNamedImports' in raw.cmkOptions) {
164+
if (typeof raw.cmkOptions.prioritizeNamedImports === 'boolean') {
165+
result.config.prioritizeNamedImports = raw.cmkOptions.prioritizeNamedImports;
166+
} else {
167+
result.diagnostics.push({
168+
category: 'error',
169+
text: `\`prioritizeNamedImports\` in ${tsConfigSourceFile.fileName} must be a boolean.`,
170+
// MEMO: Location information can be obtained from `tsConfigSourceFile.statements`, but this is complicated and will be omitted.
171+
});
172+
}
173+
}
161174
}
162175
return result;
163176
}
@@ -249,6 +262,7 @@ export function normalizeConfig(
249262
dtsOutDir: join(basePath, config.dtsOutDir ?? 'generated'),
250263
arbitraryExtensions: config.arbitraryExtensions ?? false,
251264
namedExports: config.namedExports ?? false,
265+
prioritizeNamedImports: config.prioritizeNamedImports ?? false,
252266
};
253267
}
254268

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
@@ -21,7 +21,7 @@ export function getCodeFixesAtPosition(
2121
languageService.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences) ?? [],
2222
);
2323

24-
if (config.namedExports) {
24+
if (config.namedExports && !config.prioritizeNamedImports) {
2525
convertDefaultImportsToNamespaceImports(prior, fileName, resolver);
2626
}
2727

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function getCompletionsAtPosition(
2424
}
2525
}
2626
}
27-
if (config.namedExports) {
27+
if (config.namedExports && !config.prioritizeNamedImports) {
2828
// When `namedExports` is enabled, you can write code as follows:
2929
// ```tsx
3030
// import { button } from './a.module.css';
@@ -100,7 +100,7 @@ export function getCompletionEntryDetails(
100100
);
101101
if (!details) return undefined;
102102

103-
if (config.namedExports && details.codeActions) {
103+
if (config.namedExports && !config.prioritizeNamedImports && details.codeActions) {
104104
convertDefaultImportsToNamespaceImports(details.codeActions, fileName, resolver);
105105
}
106106
return details;

0 commit comments

Comments
 (0)