Skip to content

Commit b5eb961

Browse files
committed
feat(cli): expose compiler options
1 parent c8b67a8 commit b5eb961

File tree

8 files changed

+253
-91
lines changed

8 files changed

+253
-91
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: "CommandKitCompilerOptions"
3+
isDefaultIndex: false
4+
generated: true
5+
---
6+
7+
import MemberInfo from '@site/src/components/MemberInfo';
8+
import GenerationInfo from '@site/src/components/GenerationInfo';
9+
import MemberDescription from '@site/src/components/MemberDescription';
10+
11+
<!-- This file was generated from the CommandKit source. Do not modify. Instead, re-run the "docgen" script -->
12+
13+
14+
## CommandKitCompilerOptions
15+
16+
<GenerationInfo sourceFile="packages/commandkit/src/config/types.ts" sourceLine="5" packageName="commandkit" />
17+
18+
19+
20+
```ts title="Signature"
21+
interface CommandKitCompilerOptions {
22+
macro?: {
23+
/**
24+
* Whether to enable macro function compilation in development mode.
25+
* @default false
26+
*/
27+
development?: boolean;
28+
};
29+
tsdown?: Partial<TsDownOptions>;
30+
disableChunking?: boolean;
31+
}
32+
```
33+
34+
<div className="members-wrapper">
35+
36+
### macro
37+
38+
<MemberInfo kind="property" type={`{ /** * Whether to enable macro function compilation in development mode. * @default false */ development?: boolean; }`} />
39+
40+
The macro compiler options to use with CommandKit.
41+
### tsdown
42+
43+
<MemberInfo kind="property" type={`Partial&#60;TsDownOptions&#62;`} />
44+
45+
The tsdown compiler options to use with CommandKit.
46+
**DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING** as it alters the behavior of the build process.
47+
### disableChunking
48+
49+
<MemberInfo kind="property" type={`boolean`} default={`false`} />
50+
51+
Disables chunking of the output (production only, development never chunks).
52+
53+
54+
</div>

apps/website/docs/api-reference/commandkit/interfaces/command-kit-config.mdx

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,16 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## CommandKitConfig
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/config/types.ts" sourceLine="4" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/config/types.ts" sourceLine="28" packageName="commandkit" />
1717

1818

1919

2020
```ts title="Signature"
2121
interface CommandKitConfig {
2222
plugins?: MaybeArray<CommandKitPlugin>[] | Array<CommandKitPlugin>;
2323
rolldownPlugins?: any[];
24-
compilerOptions?: {
25-
/**
26-
* Macro compiler configuration.
27-
*/
28-
macro?: {
29-
/**
30-
* Whether to enable macro function compilation in development mode.
31-
* @default false
32-
*/
33-
development?: boolean;
34-
};
35-
};
24+
entrypoints?: string[];
25+
compilerOptions?: CommandKitCompilerOptions;
3626
typescript?: {
3727
/**
3828
* Whether to ignore type checking during builds.
@@ -42,6 +32,18 @@ interface CommandKitConfig {
4232
static?: boolean;
4333
env?: Record<string, string>;
4434
distDir?: string;
35+
antiCrashScript?: {
36+
/**
37+
* Whether to enable the anti-crash script in development mode.
38+
* @default true
39+
*/
40+
development?: boolean;
41+
/**
42+
* Whether to enable the anti-crash script in production mode. Usually, you should use other means to handle errors.
43+
* @default false
44+
*/
45+
production?: boolean;
46+
};
4547
sourceMap?: {
4648
/**
4749
* Whether to enable source map generation in development mode.
@@ -71,9 +73,15 @@ Can be a single plugin, an array of plugins, or a nested array of plugins.
7173
<MemberInfo kind="property" type={`any[]`} />
7274

7375
The rolldown plugins to use with CommandKit.
76+
### entrypoints
77+
78+
<MemberInfo kind="property" type={`string[]`} />
79+
80+
The list of additional entrypoints to compile. Eg, `dir` or `dir/index.ts` or `dir/*.ts`, etc.
81+
Similarly, negative patterns can be used to exclude files. Eg, `!dir/index.ts` or `!dir/*.ts`, etc.
7482
### compilerOptions
7583

76-
<MemberInfo kind="property" type={`{ /** * Macro compiler configuration. */ macro?: { /** * Whether to enable macro function compilation in development mode. * @default false */ development?: boolean; }; }`} />
84+
<MemberInfo kind="property" type={`<a href='/docs/next/api-reference/commandkit/interfaces/command-kit-compiler-options#commandkitcompileroptions'>CommandKitCompilerOptions</a>`} />
7785

7886
The compiler options to use with CommandKit.
7987
### typescript
@@ -96,6 +104,11 @@ Statically define the environment variables to use.
96104
<MemberInfo kind="property" type={`string`} default={`\`dist\``} />
97105

98106
The custom build directory name to use.
107+
### antiCrashScript
108+
109+
<MemberInfo kind="property" type={`{ /** * Whether to enable the anti-crash script in development mode. * @default true */ development?: boolean; /** * Whether to enable the anti-crash script in production mode. Usually, you should use other means to handle errors. * @default false */ production?: boolean; }`} />
110+
111+
The anti-crash script configuration.
99112
### sourceMap
100113

101114
<MemberInfo kind="property" type={`{ /** * Whether to enable source map generation in development mode. */ development?: boolean; /** * Whether to enable source map generation in production mode. */ production?: boolean; }`} />

apps/website/docs/api-reference/commandkit/types/resolved-command-kit-config.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import MemberDescription from '@site/src/components/MemberDescription';
1313

1414
## ResolvedCommandKitConfig
1515

16-
<GenerationInfo sourceFile="packages/commandkit/src/config/utils.ts" sourceLine="45" packageName="commandkit" />
16+
<GenerationInfo sourceFile="packages/commandkit/src/config/utils.ts" sourceLine="62" packageName="commandkit" />
1717

1818

1919

2020
```ts title="Signature"
21-
type ResolvedCommandKitConfig = DeepRequired<CommandKitConfig>
21+
type ResolvedCommandKitConfig = DeepRequired<
22+
CommandKitConfig,
23+
'compilerOptions'
24+
>
2225
```

packages/commandkit/src/cli/build.ts

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { build } from 'tsdown';
1+
import { build, Options } from 'tsdown';
22
import { CompilerPlugin, CompilerPluginRuntime } from '../plugins';
33
import { loadConfigFile } from '../config/loader';
44
import { writeFile } from 'node:fs/promises';
@@ -9,6 +9,8 @@ import { performTypeCheck } from './type-checker';
99
import { copyLocaleFiles } from './common';
1010
import { MaybeArray } from '../components';
1111
import { COMMANDKIT_CWD } from '../utils/constants';
12+
import { mergeDeep } from '../config/utils';
13+
import { existsSync } from 'node:fs';
1214

1315
/**
1416
* @private
@@ -84,60 +86,70 @@ export async function buildApplication({
8486

8587
await pluginRuntime.init();
8688

87-
await build({
88-
watch: false,
89-
dts: false,
90-
clean: true,
91-
format: ['esm'],
92-
shims: true,
93-
minify: false,
94-
silent: !!isDev,
95-
inputOptions: {
96-
transform: {
97-
jsx: {
98-
runtime: 'automatic',
99-
importSource: 'commandkit',
89+
await build(
90+
mergeDeep(
91+
{
92+
watch: false,
93+
dts: false,
94+
clean: true,
95+
format: ['esm'],
96+
shims: true,
97+
minify: false,
98+
silent: !!isDev,
99+
inputOptions: {
100+
transform: {
101+
jsx: {
102+
runtime: 'automatic',
103+
importSource: 'commandkit',
104+
},
105+
},
106+
checks: {
107+
circularDependency: true,
108+
},
109+
onwarn: (warning, defaultWarn) => {
110+
if (warning?.message?.includes('compilerOptions.jsx')) return;
111+
112+
return defaultWarn(warning);
113+
},
114+
onLog: (level, log, defaultLog) => {
115+
if (isDev) return;
116+
117+
return defaultLog(level, log);
118+
},
119+
moduleTypes: {
120+
'.json': 'js',
121+
'.node': 'binary',
122+
},
100123
},
101-
},
102-
checks: {
103-
circularDependency: true,
104-
},
105-
onwarn: (warning, defaultWarn) => {
106-
if (warning?.message?.includes('compilerOptions.jsx')) return;
107-
108-
return defaultWarn(warning);
109-
},
110-
onLog: (level, log, defaultLog) => {
111-
if (isDev) return;
112-
113-
return defaultLog(level, log);
114-
},
115-
moduleTypes: {
116-
'.json': 'js',
117-
'.node': 'binary',
118-
},
119-
},
120-
plugins: rolldownPlugins,
121-
platform: 'node',
122-
skipNodeModulesBundle: true,
123-
sourcemap: true,
124-
target: 'node16',
125-
outDir: dest,
126-
env: mergeDefinitionsIfNeeded(config.env || {}, !!isDev),
127-
entry: [
128-
'src',
129-
`!${config.distDir}`,
130-
'!.commandkit',
131-
'!**/*.test.*',
132-
'!**/*.spec.*',
133-
],
134-
unbundle: !!isDev,
135-
});
124+
plugins: rolldownPlugins,
125+
platform: 'node',
126+
skipNodeModulesBundle: true,
127+
sourcemap:
128+
config.sourceMap?.[isDev ? 'development' : 'production'] ?? true,
129+
target: 'node16',
130+
outDir: dest,
131+
env: mergeDefinitionsIfNeeded(config.env || {}, !!isDev),
132+
entry: [
133+
'src',
134+
`!${config.distDir}`,
135+
'!.commandkit',
136+
'!**/*.test.*',
137+
'!**/*.spec.*',
138+
],
139+
unbundle: !!isDev,
140+
} satisfies Options,
141+
config.compilerOptions?.tsdown,
142+
),
143+
);
136144

137145
await copyLocaleFiles('src', dest);
138146
await injectEntryFile(
139147
configPath || COMMANDKIT_CWD,
140148
!!isDev,
149+
!!(
150+
config.antiCrashScript?.[isDev ? 'development' : 'production'] ??
151+
(isDev ? true : false)
152+
),
141153
config.distDir,
142154
);
143155
} catch (error) {
@@ -185,10 +197,17 @@ const wrapInAsyncIIFE = (code: string[]) =>
185197
async function injectEntryFile(
186198
configPath: string,
187199
isDev: boolean,
200+
emitAntiCrashScript: boolean,
188201
distDir?: string,
189202
) {
203+
const dist = isDev ? '.commandkit' : distDir || 'dist';
204+
const entryFilePath = join(configPath, dist, 'index.js');
205+
206+
// skip if the entry file already exists
207+
if (existsSync(entryFilePath)) return;
208+
190209
const code = `/* Entrypoint File Generated By CommandKit */
191-
${isDev ? `\n\n// Injected for development\n${wrapInAsyncIIFE([envScript(isDev), antiCrashScript])}\n\n` : wrapInAsyncIIFE([envScript(isDev)])}
210+
${isDev ? `\n\n// Injected for development\n${wrapInAsyncIIFE([envScript(isDev), emitAntiCrashScript ? antiCrashScript : ''])}\n\n` : wrapInAsyncIIFE([envScript(isDev)])}
192211
193212
import { commandkit } from 'commandkit';
194213
import { Client } from 'discord.js';
@@ -210,7 +229,5 @@ await bootstrap().catch((e) => {
210229
})
211230
`;
212231

213-
const dist = isDev ? '.commandkit' : distDir || 'dist';
214-
215-
await writeFile(join(configPath, dist, 'index.js'), code);
232+
await writeFile(entryFilePath, code);
216233
}

packages/commandkit/src/config/config.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { MaybeArray } from '../components';
22
import { CommandKitPlugin } from '../plugins';
33
import { defaultConfig } from './default';
44
import { CommandKitConfig } from './types';
5-
import { ResolvedCommandKitConfig } from './utils';
5+
import { mergeDeep, ResolvedCommandKitConfig } from './utils';
66

77
let defined: ResolvedCommandKitConfig = defaultConfig;
88

@@ -20,18 +20,29 @@ export function getConfig(): ResolvedCommandKitConfig {
2020
export function defineConfig(
2121
config: Partial<CommandKitConfig> = {},
2222
): ResolvedCommandKitConfig {
23-
// defined = mergeDeep(
24-
// config as ResolvedCommandKitConfig,
25-
// mergeDeep({} as ResolvedCommandKitConfig, defaultConfig),
26-
// );
23+
defined = mergeDeep(
24+
config as ResolvedCommandKitConfig,
25+
mergeDeep({} as ResolvedCommandKitConfig, defaultConfig),
26+
);
2727

2828
defined = {
2929
compilerOptions: {
3030
macro: {
3131
...defaultConfig.compilerOptions.macro,
3232
...config.compilerOptions?.macro,
3333
},
34+
tsdown: {
35+
...defaultConfig.compilerOptions.tsdown,
36+
...config.compilerOptions?.tsdown,
37+
},
38+
disableChunking:
39+
config.compilerOptions?.disableChunking ??
40+
defaultConfig.compilerOptions.disableChunking,
3441
},
42+
entrypoints: [
43+
...(config.entrypoints ?? []),
44+
...(defaultConfig.entrypoints ?? []),
45+
],
3546
distDir: config.distDir ?? defaultConfig.distDir,
3647
env: {
3748
...defaultConfig.env,
@@ -59,6 +70,10 @@ export function defineConfig(
5970
showUnknownPrefixCommandsWarning:
6071
config.showUnknownPrefixCommandsWarning ??
6172
defaultConfig.showUnknownPrefixCommandsWarning,
73+
antiCrashScript: {
74+
...defaultConfig.antiCrashScript,
75+
...config.antiCrashScript,
76+
},
6277
};
6378

6479
return defined;

packages/commandkit/src/config/default.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export const defaultConfig: ResolvedCommandKitConfig = {
1212
macro: {
1313
development: false,
1414
},
15+
tsdown: {},
16+
disableChunking: false,
1517
},
18+
entrypoints: [],
1619
static: true,
1720
typescript: {
1821
ignoreBuildErrors: false,
@@ -26,4 +29,8 @@ export const defaultConfig: ResolvedCommandKitConfig = {
2629
typedCommands: true,
2730
disablePrefixCommands: false,
2831
showUnknownPrefixCommandsWarning: true,
32+
antiCrashScript: {
33+
development: true,
34+
production: false,
35+
},
2936
};

0 commit comments

Comments
 (0)