Skip to content

Commit 20dc8f1

Browse files
feat: tsdown plugin (#1109)
1 parent 0c3d054 commit 20dc8f1

File tree

19 files changed

+118
-0
lines changed

19 files changed

+118
-0
lines changed

packages/knip/fixtures/plugins/tsdown/entry-1.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/tsdown/entry-2.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/tsdown/entry-3.ts

Whitespace-only changes.

packages/knip/fixtures/plugins/tsdown/entry-4.ts

Whitespace-only changes.

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

Whitespace-only changes.

packages/knip/fixtures/plugins/tsdown/node_modules/tsdown/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/knip/fixtures/plugins/tsdown/node_modules/tsdown/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@plugins/tsdown",
3+
"devDependencies": {
4+
"tsdown": "*"
5+
},
6+
"scripts": {
7+
"build": "tsdown",
8+
"build-3": "tsdown --config tsdown.config-3.ts",
9+
"build-4": "tsdown -c tsdown.config-4.ts"
10+
},
11+
"tsdown": {
12+
"entry": ["entry-5.ts"]
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from 'tsdown';
2+
3+
export default defineConfig({
4+
entry: ['entry-3.ts'],
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from 'tsdown';
2+
3+
export default defineConfig({
4+
entry: ['entry-4.ts'],
5+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"entry": ["entry-1.ts"]
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { defineConfig } from 'tsdown';
2+
3+
export default defineConfig({
4+
entry: ['entry-2.ts'],
5+
});

packages/knip/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,10 @@
651651
"title": "ts-node plugin configuration (https://knip.dev/reference/plugins/ts-node)",
652652
"$ref": "#/definitions/plugin"
653653
},
654+
"tsdown": {
655+
"title": "tsdown plugin configuration (https://knip.dev/reference/plugins/tsdown)",
656+
"$ref": "#/definitions/plugin"
657+
},
654658
"tsup": {
655659
"title": "tsup plugin configuration (https://knip.dev/reference/plugins/tsup)",
656660
"$ref": "#/definitions/plugin"

packages/knip/src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import { default as syncpack } from './syncpack/index.js';
8888
import { default as tailwind } from './tailwind/index.js';
8989
import { default as travis } from './travis/index.js';
9090
import { default as tsNode } from './ts-node/index.js';
91+
import { default as tsdown } from './tsdown/index.js';
9192
import { default as tsup } from './tsup/index.js';
9293
import { default as tsx } from './tsx/index.js';
9394
import { default as typedoc } from './typedoc/index.js';
@@ -197,6 +198,7 @@ export const Plugins = {
197198
tailwind,
198199
travis,
199200
'ts-node': tsNode,
201+
tsdown,
200202
tsup,
201203
tsx,
202204
typedoc,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { IsPluginEnabled, Plugin, ResolveConfig } from '../../types/config.js';
2+
import { toProductionEntry } from '../../util/input.js';
3+
import { hasDependency } from '../../util/plugin.js';
4+
import type { TsdownConfig } from './types.js';
5+
6+
// https://github.com/rolldown/tsdown/blob/main/src/options/index.ts
7+
8+
const title = 'tsdown';
9+
10+
const enablers = ['tsdown'];
11+
12+
const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
13+
14+
const config = ['tsdown.config.{ts,mts,cts,js,mjs,cjs,json}', 'package.json'];
15+
16+
const resolveConfig: ResolveConfig<TsdownConfig> = async config => {
17+
if (typeof config === 'function') config = await config({});
18+
19+
const entryPatterns = [config]
20+
.flat()
21+
.flatMap(config => {
22+
if (!config.entry) return [];
23+
if (Array.isArray(config.entry)) return config.entry;
24+
return Object.values(config.entry);
25+
})
26+
.map(id => toProductionEntry(id, { allowIncludeExports: true }));
27+
28+
return entryPatterns;
29+
};
30+
31+
const args = {
32+
config: true,
33+
};
34+
35+
export default {
36+
title,
37+
enablers,
38+
isEnabled,
39+
config,
40+
resolveConfig,
41+
args,
42+
} satisfies Plugin;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Entry = string[] | Record<string, string>;
2+
3+
type Options = {
4+
entry?: Entry;
5+
};
6+
7+
type MaybePromise<T> = T | Promise<T>;
8+
9+
export type TsdownConfig = Options | Options[] | ((overrideOptions: Options) => MaybePromise<Options | Options[]>);

packages/knip/src/schema/plugins.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export const pluginsSchema = z.object({
102102
tailwind: pluginSchema,
103103
travis: pluginSchema,
104104
'ts-node': pluginSchema,
105+
tsdown: pluginSchema,
105106
tsup: pluginSchema,
106107
tsx: pluginSchema,
107108
typedoc: pluginSchema,

packages/knip/src/types/PluginNames.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export type PluginName =
8989
| 'tailwind'
9090
| 'travis'
9191
| 'ts-node'
92+
| 'tsdown'
9293
| 'tsup'
9394
| 'tsx'
9495
| 'typedoc'
@@ -198,6 +199,7 @@ export const pluginNames = [
198199
'tailwind',
199200
'travis',
200201
'ts-node',
202+
'tsdown',
201203
'tsup',
202204
'tsx',
203205
'typedoc',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from 'bun:test';
2+
import assert from 'node:assert/strict';
3+
import { main } from '../../src/index.js';
4+
import { resolve } from '../../src/util/path.js';
5+
import baseArguments from '../helpers/baseArguments.js';
6+
import baseCounters from '../helpers/baseCounters.js';
7+
8+
const cwd = resolve('fixtures/plugins/tsdown');
9+
10+
test('Find dependencies with the tsdown plugin', async () => {
11+
const { counters } = await main({
12+
...baseArguments,
13+
cwd,
14+
});
15+
16+
assert.deepEqual(counters, {
17+
...baseCounters,
18+
processed: 8,
19+
total: 8,
20+
});
21+
});

0 commit comments

Comments
 (0)