Skip to content

Commit c7366b5

Browse files
authored
Update rsbuild Plugin to Check Environments (#1246)
1 parent 8b338a2 commit c7366b5

File tree

8 files changed

+36
-11
lines changed

8 files changed

+36
-11
lines changed

packages/knip/fixtures/plugins/rsbuild/entry-5.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/rsbuild/entry-6.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/rsbuild/entry-7.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/rsbuild/entry-8.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/rsbuild/rsbuild.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,16 @@ export default defineConfig({
1111
entry4: { import: ['entry-4.ts'] },
1212
},
1313
},
14+
environments: {
15+
test: {
16+
source: {
17+
entry: {
18+
entry5: 'entry-5.ts',
19+
entry6: ['entry-6.ts'],
20+
entry7: { import: 'entry-7.ts' },
21+
entry8: { import: ['entry-8.ts'] },
22+
},
23+
},
24+
},
25+
},
1426
});

packages/knip/src/plugins/rsbuild/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@ const config = ['rsbuild*.config.{mjs,ts,js,cjs,mts,cts}'];
1515

1616
const resolveConfig: ResolveConfig<RsbuildConfig> = async config => {
1717
const inputs = new Set<Input>();
18-
if (config.source?.entry) {
19-
for (const entry of Object.values(config.source.entry)) {
20-
if (typeof entry === 'string') inputs.add(toEntry(entry));
21-
else if (Array.isArray(entry)) for (const e of entry) inputs.add(toEntry(e));
22-
else {
23-
if (typeof entry.import === 'string') inputs.add(toEntry(entry.import));
24-
else if (Array.isArray(entry.import)) for (const e of entry.import) inputs.add(toEntry(e));
18+
19+
const checkSource = (source: RsbuildConfig['source']) => {
20+
if (source?.entry) {
21+
for (const entry of Object.values(source.entry)) {
22+
if (typeof entry === 'string') inputs.add(toEntry(entry));
23+
else if (Array.isArray(entry)) for (const e of entry) inputs.add(toEntry(e));
24+
else {
25+
if (typeof entry.import === 'string') inputs.add(toEntry(entry.import));
26+
else if (Array.isArray(entry.import)) for (const e of entry.import) inputs.add(toEntry(e));
27+
}
2528
}
2629
}
30+
};
31+
32+
checkSource(config.source);
33+
34+
if (config.environments) {
35+
for (const environment of Object.values(config.environments)) {
36+
checkSource(environment.source);
37+
}
2738
}
39+
2840
return Array.from(inputs);
2941
};
3042

packages/knip/src/plugins/rsbuild/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ type Entry = Record<string, string | string[] | (EntryDescription & { html?: boo
44

55
export type RsbuildConfig = {
66
plugins?: unknown[];
7-
source?: {
8-
entry?: Entry;
7+
source?: { entry?: Entry };
8+
environments?: {
9+
[k: string]: Pick<RsbuildConfig, 'plugins' | 'source'>;
910
};
1011
};

packages/knip/test/plugins/rsbuild.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('Find dependencies with the rsbuild plugin', async () => {
1414
assert.deepEqual(counters, {
1515
...baseCounters,
1616
binaries: 1,
17-
processed: 5,
18-
total: 5,
17+
processed: 9,
18+
total: 9,
1919
});
2020
});

0 commit comments

Comments
 (0)