Skip to content

Commit a3c0e63

Browse files
authored
Merge pull request #205 from underctrl-io/rolldown
feat!: use rolldown based bundler
2 parents 186b3fd + 1a6884c commit a3c0e63

File tree

16 files changed

+539
-311
lines changed

16 files changed

+539
-311
lines changed

packages/commandkit/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"scripts": {
6565
"lint": "tsc --noEmit",
6666
"dev": "pnpm run --filter=test-bot dev",
67-
"build": "tsup",
67+
"build": "tsdown",
6868
"publish-package": "npm publish",
6969
"test": "vitest"
7070
},
@@ -80,6 +80,7 @@
8080
"event handler"
8181
],
8282
"dependencies": {
83+
"@rollup/plugin-json": "^6.1.0",
8384
"chokidar": "^4.0.3",
8485
"commander": "^13.0.0",
8586
"directive-to-hof": "^0.0.2",
@@ -89,7 +90,7 @@
8990
"picocolors": "^1.1.1",
9091
"rfdc": "^1.3.1",
9192
"rimraf": "^6.0.0",
92-
"tsup": "^8.4.0",
93+
"tsdown": "^0.12.4",
9394
"use-macro": "^1.1.0"
9495
},
9596
"devDependencies": {

packages/commandkit/src/CommandKit.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export function onApplicationBootstrap<F extends BootstrapFunction>(
7474

7575
export class CommandKit extends EventEmitter {
7676
#started = false;
77+
private options: CommandKitOptions;
7778
public eventInterceptor!: EventInterceptor;
7879

7980
public static readonly createElement = createElement;
@@ -89,10 +90,10 @@ export class CommandKit extends EventEmitter {
8990

9091
public commandsRouter!: CommandsRouter;
9192
public eventsRouter!: EventsRouter;
92-
public readonly commandHandler = new AppCommandHandler(this);
93-
public readonly eventHandler = new AppEventsHandler(this);
94-
public readonly plugins: CommandKitPluginRuntime;
95-
public readonly events = new CommandKitEventsChannel(this);
93+
public commandHandler!: AppCommandHandler;
94+
public eventHandler!: AppEventsHandler;
95+
public plugins!: CommandKitPluginRuntime;
96+
public events!: CommandKitEventsChannel;
9697

9798
static instance: CommandKit | undefined = undefined;
9899

@@ -102,7 +103,7 @@ export class CommandKit extends EventEmitter {
102103
* @param options - The default CommandKit configuration.
103104
* @see {@link https://commandkit.js.org/docs/guide/commandkit-setup}
104105
*/
105-
constructor(private readonly options: CommandKitOptions) {
106+
constructor(options: CommandKitOptions) {
106107
if (CommandKit.instance) {
107108
process.emitWarning(
108109
'CommandKit instance already exists. Having multiple instance in same project is discouraged and it may lead to unexpected behavior.',
@@ -120,12 +121,14 @@ export class CommandKit extends EventEmitter {
120121

121122
super();
122123

123-
this.plugins = new CommandKitPluginRuntime(this);
124+
this.options = options;
124125

125126
if (!CommandKit.instance) {
126127
CommandKit.instance = this;
127128
}
128129

130+
this.plugins = new CommandKitPluginRuntime(this);
131+
129132
// @ts-ignore
130133
commandkit = CommandKit.instance;
131134

@@ -280,6 +283,10 @@ export class CommandKit extends EventEmitter {
280283
const commandsPath = this.getPath('commands')!;
281284
const events = this.getPath('events')!;
282285

286+
this.commandHandler = new AppCommandHandler(this);
287+
this.eventHandler = new AppEventsHandler(this);
288+
this.events = new CommandKitEventsChannel(this);
289+
283290
this.commandsRouter = new CommandsRouter({
284291
entrypoint: commandsPath,
285292
});

packages/commandkit/src/cli/build.ts

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { build } from 'tsup';
2-
import {
3-
CompilerPlugin,
4-
CompilerPluginRuntime,
5-
fromEsbuildPlugin,
6-
} from '../plugins';
1+
import { build } from 'tsdown';
2+
import { CompilerPlugin, CompilerPluginRuntime } from '../plugins';
73
import { loadConfigFile } from '../config/loader';
84
import { writeFile } from 'node:fs/promises';
95
import { join } from 'node:path';
@@ -15,14 +11,14 @@ import { MaybeArray } from '../components';
1511

1612
export interface ApplicationBuildOptions {
1713
plugins?: MaybeArray<CompilerPlugin>[] | Array<CompilerPlugin>;
18-
esbuildPlugins?: any[];
14+
rolldownPlugins?: any[];
1915
isDev?: boolean;
2016
configPath?: string;
2117
}
2218

2319
export async function buildApplication({
2420
plugins,
25-
esbuildPlugins,
21+
rolldownPlugins,
2622
isDev,
2723
configPath,
2824
}: ApplicationBuildOptions) {
@@ -35,45 +31,40 @@ export async function buildApplication({
3531
const pluginRuntime = new CompilerPluginRuntime(
3632
(plugins || []) as CompilerPlugin[],
3733
);
38-
const esbuildPluginList: any[] = pluginRuntime.isEmpty()
39-
? []
40-
: [pluginRuntime.toEsbuildPlugin()];
4134

42-
if (esbuildPlugins?.length) {
43-
esbuildPluginList.push(...esbuildPlugins.map(fromEsbuildPlugin));
44-
}
35+
rolldownPlugins ??= [];
36+
37+
rolldownPlugins.push(pluginRuntime.toJSON());
4538

4639
try {
4740
const dest = isDev ? '.commandkit' : config.distDir;
4841

4942
// Clean the destination directory
5043
await rimraf(dest);
5144

45+
await pluginRuntime.init();
46+
5247
await build({
53-
esbuildPlugins: esbuildPluginList,
5448
watch: false,
55-
banner: {
56-
js: !isDev
57-
? '/* Optimized production build generated by commandkit */'
58-
: '',
59-
},
60-
cjsInterop: true,
6149
dts: false,
6250
clean: true,
6351
format: ['esm'],
6452
shims: true,
65-
keepNames: true,
6653
minify: false,
67-
esbuildOptions: (options) => {
68-
options.jsx = 'automatic';
69-
options.jsxImportSource = 'commandkit';
70-
71-
return options;
72-
},
73-
minifyIdentifiers: false,
74-
minifySyntax: false,
7554
silent: !!isDev,
76-
splitting: true,
55+
inputOptions: {
56+
transform: {
57+
jsx: {
58+
runtime: 'automatic',
59+
importSource: 'commandkit',
60+
},
61+
},
62+
moduleTypes: {
63+
'.json': 'js',
64+
},
65+
},
66+
plugins: rolldownPlugins,
67+
platform: 'node',
7768
skipNodeModulesBundle: true,
7869
name: 'CommandKit',
7970
sourcemap: true,
@@ -87,6 +78,7 @@ export async function buildApplication({
8778
'!**/*.test.*',
8879
'!**/*.spec.*',
8980
],
81+
unbundle: false,
9082
});
9183

9284
await copyLocaleFiles('src', dest);
@@ -99,7 +91,7 @@ export async function buildApplication({
9991
process.exit(1); // Force exit on error
10092
} finally {
10193
// Ensure plugins are cleaned up
102-
await pluginRuntime.cleanup();
94+
await pluginRuntime.destroy();
10395
}
10496
}
10597

@@ -113,23 +105,6 @@ for (const file of $env) {
113105
}
114106
`;
115107

116-
const requireScript = [
117-
'// --- CommandKit require() polyfill ---',
118-
' if (typeof require === "undefined") {',
119-
' const { createRequire } = await import("node:module");',
120-
' const __require = createRequire(import.meta.url);',
121-
' Object.defineProperty(globalThis, "require", {',
122-
' value: (id) => {',
123-
' return __require(id);',
124-
' },',
125-
' configurable: true,',
126-
' enumerable: false,',
127-
' writable: true,',
128-
' });',
129-
' }',
130-
'// --- CommandKit require() polyfill ---',
131-
].join('\n');
132-
133108
const antiCrashScript = [
134109
'// --- CommandKit Anti-Crash Monitor ---',
135110
" // 'uncaughtException' event is supposed to be used to perform synchronous cleanup before shutting down the process",
@@ -156,7 +131,7 @@ async function injectEntryFile(
156131
distDir?: string,
157132
) {
158133
const code = `/* Entrypoint File Generated By CommandKit */
159-
${isDev ? `\n\n// Injected for development\n${wrapInAsyncIIFE([envScript(isDev), antiCrashScript, requireScript])}\n\n` : wrapInAsyncIIFE([envScript(isDev), requireScript])}
134+
${isDev ? `\n\n// Injected for development\n${wrapInAsyncIIFE([envScript(isDev), antiCrashScript])}\n\n` : wrapInAsyncIIFE([envScript(isDev)])}
160135
161136
import { CommandKit } from 'commandkit';
162137

packages/commandkit/src/cli/development.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function buildAndStart(configPath: string, skipStart = false) {
2020
configPath,
2121
isDev: true,
2222
plugins: config.plugins.flat(2).filter((p) => isCompilerPlugin(p)),
23-
esbuildPlugins: config.esbuildPlugins,
23+
rolldownPlugins: config.rolldownPlugins,
2424
});
2525

2626
if (skipStart) return null as never;

packages/commandkit/src/cli/production.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function createProductionBuild(configPath?: string) {
3939
plugins: config.plugins.filter((p) =>
4040
isCompilerPlugin(p),
4141
) as CompilerPlugin[],
42-
esbuildPlugins: config.esbuildPlugins,
42+
rolldownPlugins: config.rolldownPlugins,
4343
});
4444

4545
spinner.succeed('Production build completed!');

packages/commandkit/src/config/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export function defineConfig(
3737
...defaultConfig.env,
3838
...config.env,
3939
},
40-
esbuildPlugins: [
41-
...(config.esbuildPlugins ?? []),
42-
...(defaultConfig.esbuildPlugins ?? []),
40+
rolldownPlugins: [
41+
...(config.rolldownPlugins ?? []),
42+
...(defaultConfig.rolldownPlugins ?? []),
4343
],
4444
plugins: [...(config.plugins ?? []), ...(defaultConfig.plugins ?? [])].flat(
4545
Infinity,

packages/commandkit/src/config/default.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { MacroPlugin } from '../plugins/plugin-runtime/builtin/MacroPlugin';
22
import { ResolvedCommandKitConfig } from './utils';
3+
import json from '@rollup/plugin-json';
34

45
export const defaultConfig: ResolvedCommandKitConfig = {
56
plugins: [new MacroPlugin({ enabled: true })],
6-
esbuildPlugins: [],
7+
rolldownPlugins: [json() as any],
78
compilerOptions: {
89
macro: {
910
development: false,

packages/commandkit/src/config/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export interface CommandKitConfig {
88
*/
99
plugins?: MaybeArray<CommandKitPlugin>[] | Array<CommandKitPlugin>;
1010
/**
11-
* The esbuild plugins to use with CommandKit.
11+
* The rolldown plugins to use with CommandKit.
1212
*/
13-
esbuildPlugins?: any[];
13+
rolldownPlugins?: any[];
1414
/**
1515
* The compiler options to use with CommandKit.
1616
*/

packages/commandkit/src/flags/feature-flags.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import {
88
Client,
99
ClientEvents,
1010
ContextMenuCommandInteraction,
11-
Events,
1211
Guild,
1312
Message,
1413
TextBasedChannel,
1514
} from 'discord.js';
1615
import { LoadedCommand } from '../app';
17-
import { FlagStore } from './store';
1816

1917
export type MaybePromise<T> = T | Promise<T>;
2018

packages/commandkit/src/plugins/CompilerPlugin.ts

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
Loader,
3-
OnLoadArgs,
4-
OnResolveArgs,
5-
OnResolveResult,
6-
} from './plugin-runtime/types';
1+
import { OnResolveArgs } from './plugin-runtime/types';
72
import {
83
isPlugin,
94
PluginCommon,
@@ -14,20 +9,13 @@ import { MaybeFalsey } from './types';
149
import { CompilerPluginRuntime } from './plugin-runtime/CompilerPluginRuntime';
1510

1611
export interface PluginTransformParameters {
17-
args: OnLoadArgs;
18-
path: string;
19-
contents: string | Uint8Array;
20-
loader?: Loader;
12+
code: string;
13+
id: string;
2114
}
2215

2316
export interface TransformedResult {
24-
contents?: string | Uint8Array;
25-
loader?: Loader;
26-
}
27-
28-
export interface ResolveResult {
29-
path?: string;
30-
external?: boolean;
17+
code?: string;
18+
map?: string | null;
3119
}
3220

3321
export abstract class CompilerPlugin<
@@ -45,27 +33,6 @@ export abstract class CompilerPlugin<
4533
): Promise<MaybeFalsey<TransformedResult>> {
4634
return null;
4735
}
48-
49-
/**
50-
* Called when a resolve is requested to this plugin
51-
* @param args The arguments for the resolve
52-
* @returns The resolved result
53-
*/
54-
public async resolve(
55-
args: OnResolveArgs,
56-
): Promise<MaybeFalsey<ResolveResult>> {
57-
return null;
58-
}
59-
60-
/**
61-
* Called when a build is started
62-
*/
63-
public async onBuildStart(): Promise<void> {}
64-
65-
/**
66-
* Called when a build is ended
67-
*/
68-
public async onBuildEnd(): Promise<void> {}
6936
}
7037

7138
export function isCompilerPlugin(plugin: unknown): plugin is CompilerPlugin {

0 commit comments

Comments
 (0)