Skip to content

Commit

Permalink
fix: watchFiles options not work for reloading server (#3077)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jul 30, 2024
1 parent 1e3117a commit 3f492d0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
18 changes: 9 additions & 9 deletions packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { loadConfig, watchFiles } from '../config';
import { isDev } from '../helpers';
import { castArray, isDev } from '../helpers';
import { loadEnv } from '../loadEnv';
import { logger } from '../logger';
import { onBeforeRestartServer } from '../server/restart';
Expand Down Expand Up @@ -47,18 +47,18 @@ export async function init({
const command = process.argv[2];
if (command === 'dev') {
const files = [...envs.filePaths];

if (configFilePath) {
files.push(configFilePath);
if (config.dev?.watchFiles?.type === 'reload-server') {
const extraConfigFiles =
typeof config.dev.watchFiles.paths === 'string'
? [config.dev.watchFiles.paths]
: config.dev.watchFiles.paths;
files.push(...extraConfigFiles);
}
}

watchFiles(files);
const { watchFiles: watchFilesConfig } = config.dev || {};
if (watchFilesConfig?.type === 'reload-server') {
files.push(...castArray(watchFilesConfig.paths));
watchFiles(files, watchFilesConfig.options);
} else {
watchFiles(files);
}
}

const { createRsbuild } = await import('../createRsbuild');
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'node:fs';
import { isAbsolute, join } from 'node:path';
import type { WatchOptions } from 'chokidar';
import color from 'picocolors';
import RspackChain from 'rspack-chain';
import {
Expand Down Expand Up @@ -296,7 +297,10 @@ const resolveConfigPath = (root: string, customConfig?: string) => {
return null;
};

export async function watchFiles(files: string[]): Promise<void> {
export async function watchFiles(
files: string[],
watchOptions?: WatchOptions,
): Promise<void> {
if (!files.length) {
return;
}
Expand All @@ -307,6 +311,7 @@ export async function watchFiles(files: string[]): Promise<void> {
ignoreInitial: true,
// If watching fails due to read permissions, the errors will be suppressed silently.
ignorePermissionErrors: true,
...watchOptions,
});

const callback = debounce(
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/server/watchFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,17 @@ async function startWatchFiles(
{ paths, options, type }: ReturnType<typeof prepareWatchOptions>,
compileMiddlewareAPI: CompileMiddlewareAPI,
) {
// If `type` is not 'reload-server', treat it as 'reload-page'.
if (type !== 'reload-server') {
const chokidar = await import('chokidar');
const watcher = chokidar.watch(paths, options);
// If `type` is 'reload-server', skip it.
if (type === 'reload-server') {
return;
}

watcher.on('change', () => {
compileMiddlewareAPI.sockWrite('static-changed');
});
const chokidar = await import('chokidar');
const watcher = chokidar.watch(paths, options);

return watcher;
}
watcher.on('change', () => {
compileMiddlewareAPI.sockWrite('static-changed');
});

return watcher;
}

0 comments on commit 3f492d0

Please sign in to comment.