Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/cli/src/config/loadConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const findConfig = (basePath: string): string | undefined => {

export async function loadConfigFile(
customConfigFile?: string,
): Promise<UserConfig> {
): Promise<{ config: UserConfig; configFilePath: string }> {
const baseDir = process.cwd();
let configFilePath = '';
if (customConfigFile) {
Expand All @@ -29,7 +29,7 @@ export async function loadConfigFile(
}
if (!configFilePath) {
logger.info(`No config file found in ${baseDir}`);
return {};
return { config: {}, configFilePath: '' };
}

const { loadConfig } = await import('@rsbuild/core');
Expand All @@ -38,7 +38,10 @@ export async function loadConfigFile(
path: configFilePath,
});

return content as UserConfig;
return {
config: content as UserConfig,
configFilePath,
};
}

export function resolveDocRoot(
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ cli
let devServer: Awaited<ReturnType<typeof dev>>;
const startDevServer = async () => {
const { port, host } = options || {};
const config = await loadConfigFile(options?.config);
const { config, configFilePath } = await loadConfigFile(
options?.config,
);

config.root = resolveDocRoot(cwd, root, config.root);

Expand All @@ -50,6 +52,7 @@ cli
appDirectory: cwd,
docDirectory,
config,
configFilePath,
extraBuilderConfig: { server: { port, host } },
});

Expand Down Expand Up @@ -104,7 +107,7 @@ cli
cli.command('build [root]').action(async (root, options) => {
setNodeEnv('production');
const cwd = process.cwd();
const config = await loadConfigFile(options.config);
const { config, configFilePath } = await loadConfigFile(options.config);

config.root = resolveDocRoot(cwd, root, config.root);
const docDirectory = config.root;
Expand All @@ -113,6 +116,7 @@ cli.command('build [root]').action(async (root, options) => {
await build({
docDirectory,
config,
configFilePath,
});
} catch (err) {
logger.error(err);
Expand All @@ -133,14 +137,15 @@ cli
setNodeEnv('production');
const cwd = process.cwd();
const { port, host } = options || {};
const config = await loadConfigFile(options?.config);
const { config, configFilePath } = await loadConfigFile(options?.config);

config.root = resolveDocRoot(cwd, root, config.root);

await serve({
config,
host,
port,
configFilePath,
});
},
);
Expand Down
20 changes: 12 additions & 8 deletions packages/cli/tests/config/config-basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const TEST_TITLE = 'my-title';
describe('Should load config file', () => {
test('Load config.cjs', async () => {
const fixtureDir = path.join(__dirname, 'cjs');
const config = await loadConfigFile(
const { config } = await loadConfigFile(
path.join(fixtureDir, 'rspress.config.cjs'),
);

Expand All @@ -20,7 +20,7 @@ describe('Should load config file', () => {

test('Load config.mjs', async () => {
const fixtureDir = path.join(__dirname, 'esm');
const config = await loadConfigFile(
const { config } = await loadConfigFile(
path.join(fixtureDir, 'rspress.config.mjs'),
);

Expand All @@ -33,7 +33,7 @@ describe('Should load config file', () => {

test('Load config.js/config.ts in cjs project', async () => {
const fixtureDir = path.join(__dirname, 'cjs');
let config = await loadConfigFile(
const { config } = await loadConfigFile(
path.join(fixtureDir, 'rspress.config.js'),
);

Expand All @@ -42,16 +42,18 @@ describe('Should load config file', () => {
title: TEST_TITLE,
});

config = await loadConfigFile(path.join(fixtureDir, 'rspress.config.ts'));
expect(config).toMatchObject({
const { config: config2 } = await loadConfigFile(
path.join(fixtureDir, 'rspress.config.ts'),
);
expect(config2).toMatchObject({
root: normalizePath(fixtureDir),
title: TEST_TITLE,
});
});

test('Load config.js/config.ts in esm project', async () => {
const fixtureDir = path.join(__dirname, 'esm');
let config = await loadConfigFile(
const { config } = await loadConfigFile(
path.join(fixtureDir, 'rspress.config.js'),
);

Expand All @@ -61,7 +63,9 @@ describe('Should load config file', () => {
};
expect(config).toMatchObject(expectConfig);

config = await loadConfigFile(path.join(fixtureDir, 'rspress.config.ts'));
expect(config).toMatchObject(expectConfig);
const { config: config2 } = await loadConfigFile(
path.join(fixtureDir, 'rspress.config.ts'),
);
expect(config2).toMatchObject(expectConfig);
});
});
8 changes: 7 additions & 1 deletion packages/core/src/node/PluginDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ type RspressPluginHookKeys =

export class PluginDriver {
#config: UserConfig;
#configFilePath: string;

#plugins: RspressPlugin[];

#isProd: boolean;

constructor(config: UserConfig, isProd: boolean) {
constructor(config: UserConfig, configFilePath: string, isProd: boolean) {
this.#config = config;
this.#configFilePath = configFilePath;
this.#isProd = isProd;
this.#plugins = [];
}

getConfigFilePath() {
return this.#configFilePath;
}

// The init function is used to initialize the doc plugins and will execute before the build process.
async init() {
// Clear RspressPlugins first, for the watch mode
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { checkLanguageParity } from './utils/checkLanguageParity';
interface BuildOptions {
docDirectory: string;
config: UserConfig;
configFilePath: string;
}

export async function bundle(
Expand Down Expand Up @@ -42,8 +43,8 @@ function emptyDir(path: string): Promise<void> {
}

export async function build(options: BuildOptions) {
const { docDirectory, config } = options;
const pluginDriver = new PluginDriver(config, true);
const { docDirectory, config, configFilePath } = options;
const pluginDriver = new PluginDriver(config, configFilePath, true);
await pluginDriver.init();
const modifiedConfig = await pluginDriver.modifyConfig();

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/node/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ interface DevOptions {
appDirectory: string;
docDirectory: string;
config: UserConfig;
configFilePath: string;
extraBuilderConfig?: RsbuildConfig;
}

export async function dev(options: DevOptions): Promise<ServerInstance> {
const { docDirectory, config, extraBuilderConfig } = options;
const { docDirectory, config, extraBuilderConfig, configFilePath } = options;
const isProd = false;
const pluginDriver = new PluginDriver(config, isProd);
const pluginDriver = new PluginDriver(config, configFilePath, isProd);
await pluginDriver.init();
const modifiedConfig = await pluginDriver.modifyConfig();

Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/node/initRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ async function createInternalBuildConfig(
},
},
performance: {
buildCache: {
buildDependencies: [pluginDriver.getConfigFilePath()],
},
chunkSplit: {
override: {
cacheGroups: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/node/route/RouteService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function initRouteService(
) {
const routeService = await RouteService.create({
config,
pluginDriver: new PluginDriver(config, false),
pluginDriver: new PluginDriver(config, '', false),
runtimeTempDir: '.rsbuild',
scanDir: fixtureDir,
});
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/node/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { initRsbuild } from './initRsbuild';

interface ServeOptions {
config: UserConfig;
configFilePath: string;
port?: number;
host?: string;
}
Expand All @@ -13,7 +14,7 @@ interface ServeOptions {
export async function serve(
options: ServeOptions,
): Promise<ReturnType<RsbuildInstance['preview']>> {
const { config, port: userPort, host: userHost } = options;
const { config, port: userPort, host: userHost, configFilePath } = options;
const envPort = process.env.PORT;
const envHost = process.env.HOST;
const { builderConfig } = config;
Expand All @@ -30,7 +31,7 @@ export async function serve(
},
});

const pluginDriver = new PluginDriver(config, true);
const pluginDriver = new PluginDriver(config, configFilePath, true);
await pluginDriver.init();

const modifiedConfig = await pluginDriver.modifyConfig();
Expand Down