Skip to content

Commit a6d1f56

Browse files
committed
feat: JS api
1 parent 2b9e896 commit a6d1f56

File tree

26 files changed

+640
-304
lines changed

26 files changed

+640
-304
lines changed

packages/core/src/cli/build.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/core/src/cli/commands.ts

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
import type { LogLevel, RsbuildMode, RsbuildPlugin } from '@rsbuild/core';
1+
import type { LogLevel, RsbuildMode } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
33
import type { ConfigLoader } from '../loadConfig';
4+
import { watchFilesForRestart } from '../restart';
45
import type { Format, Syntax } from '../types';
56
import { color } from '../utils/color';
67
import { logger } from '../utils/logger';
7-
import { build } from './build';
8-
import { initConfig } from './initConfig';
9-
import { inspect } from './inspect';
10-
import { startMFDevServer } from './mf';
11-
import { watchFilesForRestart } from './restart';
8+
import { init } from './init';
129

1310
export const RSPACK_BUILD_ERROR = 'Rspack build failed.';
1411

1512
export type CommonOptions = {
1613
root?: string;
1714
config?: string;
15+
configLoader?: ConfigLoader;
16+
env?: boolean;
1817
envDir?: string;
1918
envMode?: string;
2019
lib?: string[];
21-
configLoader?: ConfigLoader;
2220
logLevel?: LogLevel;
2321
};
2422

@@ -53,6 +51,13 @@ const applyCommonOptions = (cli: CAC) => {
5351
'-c, --config <config>',
5452
'specify the configuration file, can be a relative or absolute path',
5553
)
54+
.option(
55+
'--config-loader <loader>',
56+
'Set the config file loader (auto | jiti | native)',
57+
{
58+
default: 'auto',
59+
},
60+
)
5661
.option(
5762
'-r, --root <root>',
5863
'specify the project root directory, can be an absolute path or a path relative to cwd',
@@ -61,13 +66,6 @@ const applyCommonOptions = (cli: CAC) => {
6166
'--env-mode <mode>',
6267
'specify the env mode to load the `.env.[mode]` file',
6368
)
64-
.option(
65-
'--config-loader <loader>',
66-
'Set the config file loader (auto | jiti | native)',
67-
{
68-
default: 'auto',
69-
},
70-
)
7169
.option('--env-dir <dir>', 'specify the directory to load `.env` files')
7270
.option(
7371
'--log-level <level>',
@@ -80,7 +78,8 @@ const applyCommonOptions = (cli: CAC) => {
8078
type: [String],
8179
default: [],
8280
},
83-
);
81+
)
82+
.option('--no-env', 'Disable loading of `.env` files');
8483
};
8584

8685
export function setupCommands(): void {
@@ -141,26 +140,15 @@ export function setupCommands(): void {
141140
.action(async (options: BuildOptions) => {
142141
try {
143142
const cliBuild = async () => {
144-
const { config, watchFiles } = await initConfig(options);
143+
const rslib = await init(options);
145144

146145
if (options.watch) {
147-
config.plugins = config.plugins || [];
148-
config.plugins.push({
149-
name: 'rslib:on-after-build',
150-
setup(api) {
151-
api.onAfterBuild(({ isFirstCompile }) => {
152-
if (isFirstCompile) {
153-
logger.success('build complete, watching for changes...');
154-
}
155-
});
156-
},
157-
} satisfies RsbuildPlugin);
158-
159-
watchFilesForRestart(watchFiles, async () => {
146+
watchFilesForRestart(rslib.context.watchFiles, async () => {
160147
await cliBuild();
161148
});
162149
}
163-
await build(config, options);
150+
151+
await rslib.build(options);
164152
};
165153

166154
await cliBuild();
@@ -189,12 +177,13 @@ export function setupCommands(): void {
189177
.action(async (options: InspectOptions) => {
190178
try {
191179
// TODO: inspect should output Rslib's config
192-
const { config } = await initConfig(options);
193-
await inspect(config, {
180+
const rslib = await init(options);
181+
await rslib.inspectConfig({
194182
lib: options.lib,
195183
mode: options.mode,
196-
output: options.output,
184+
outputPath: options.output,
197185
verbose: options.verbose,
186+
writeToDisk: true,
198187
});
199188
} catch (err) {
200189
logger.error('Failed to inspect config.');
@@ -206,12 +195,12 @@ export function setupCommands(): void {
206195
mfDevCommand.action(async (options: MfDevOptions) => {
207196
try {
208197
const cliMfDev = async () => {
209-
const { config, watchFiles } = await initConfig(options);
210-
await startMFDevServer(config, {
198+
const rslib = await init(options);
199+
await rslib.startMFDevServer({
211200
lib: options.lib,
212201
});
213202

214-
watchFilesForRestart(watchFiles, async () => {
203+
watchFilesForRestart(rslib.context.watchFiles, async () => {
215204
await cliMfDev();
216205
});
217206
};
Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import path from 'node:path';
2-
import util from 'node:util';
3-
import { loadEnv, type RsbuildEntry } from '@rsbuild/core';
4-
import { loadConfig } from '../loadConfig';
2+
import type { RsbuildEntry } from '@rsbuild/core';
3+
import { createRslib } from '../createRslib';
4+
import { loadConfig as baseLoadConfig } from '../loadConfig';
55
import type {
66
LibConfig,
77
RsbuildConfigOutputTarget,
88
RslibConfig,
9+
RslibInstance,
910
} from '../types';
10-
import { getAbsolutePath } from '../utils/helper';
11-
import { isDebugKey, logger } from '../utils/logger';
11+
import { ensureAbsolutePath } from '../utils/helper';
12+
import { logger } from '../utils/logger';
1213
import type { BuildOptions, CommonOptions } from './commands';
13-
import { onBeforeRestart } from './restart';
1414

1515
const getEnvDir = (cwd: string, envDir?: string) => {
1616
if (envDir) {
@@ -67,8 +67,13 @@ export const applyCliOptions = (
6767
options: BuildOptions,
6868
root: string,
6969
): void => {
70-
if (options.root) config.root = root;
71-
if (options.logLevel) config.logLevel = options.logLevel;
70+
if (options.root) {
71+
config.root = root;
72+
}
73+
74+
if (options.logLevel) {
75+
config.logLevel = options.logLevel;
76+
}
7277

7378
for (const lib of config.lib) {
7479
if (options.format !== undefined) lib.format = options.format;
@@ -105,21 +110,8 @@ export const applyCliOptions = (
105110
}
106111
};
107112

108-
export async function initConfig(options: CommonOptions): Promise<{
109-
config: RslibConfig;
110-
configFilePath: string | null;
111-
watchFiles: string[];
112-
}> {
113-
const cwd = process.cwd();
114-
const root = options.root ? getAbsolutePath(cwd, options.root) : cwd;
115-
const envs = loadEnv({
116-
cwd: getEnvDir(root, options.envDir),
117-
mode: options.envMode,
118-
});
119-
120-
onBeforeRestart(envs.cleanup);
121-
122-
const { content: config, filePath: configFilePath } = await loadConfig({
113+
const loadConfig = async (options: CommonOptions, root: string) => {
114+
const { content: config, filePath: configFilePath } = await baseLoadConfig({
123115
cwd: root,
124116
path: options.config,
125117
envMode: options.envMode,
@@ -128,28 +120,29 @@ export async function initConfig(options: CommonOptions): Promise<{
128120

129121
if (configFilePath === null) {
130122
config.lib = [{} satisfies LibConfig];
131-
logger.debug(
132-
'No config file found. Falling back to CLI options for the default library.',
133-
);
123+
logger.debug('Falling back to CLI options for the default library.');
134124
}
135125

136-
config.source ||= {};
137-
config.source.define = {
138-
...envs.publicVars,
139-
...config.source.define,
140-
};
141-
142126
applyCliOptions(config, options, root);
143127

144-
// only debug serialized rslib config when DEBUG=rslib
145-
if (isDebugKey(['rslib'])) {
146-
logger.debug('Rslib config used to generate Rsbuild environments:');
147-
logger.debug(`\n${util.inspect(config, { depth: null, colors: true })}`);
148-
}
128+
return config;
129+
};
130+
131+
export async function init(options: CommonOptions): Promise<RslibInstance> {
132+
const cwd = process.cwd();
133+
const root = options.root ? ensureAbsolutePath(cwd, options.root) : cwd;
134+
135+
const rslib = await createRslib({
136+
cwd: root,
137+
config: () => loadConfig(options, root),
138+
loadEnv:
139+
options.env === false
140+
? false
141+
: {
142+
cwd: getEnvDir(root, options.envDir),
143+
mode: options.envMode,
144+
},
145+
});
149146

150-
return {
151-
config,
152-
configFilePath,
153-
watchFiles: [configFilePath, ...envs.filePaths].filter(Boolean) as string[],
154-
};
147+
return rslib;
155148
}

packages/core/src/cli/inspect.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/core/src/cli/mf.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)