Skip to content

Commit

Permalink
fix: watchFiles.type not work as expected (#3963)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Nov 13, 2024
1 parent 94c608c commit 05c6c65
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
56 changes: 37 additions & 19 deletions e2e/cases/cli/watch-files/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ import path from 'node:path';
import { awaitFileExists, getRandomPort } from '@e2e/helper';
import { expect, test } from '@playwright/test';

const dist = path.join(__dirname, 'dist');
const tempConfigPath = './test-temp-config.ts';
const tempOutputFile = path.join(__dirname, 'test-temp-file.txt');
const extraConfigFile = path.join(__dirname, tempConfigPath);

test('should restart dev server when extra config file changed', async () => {
const dist = path.join(__dirname, 'dist');
const extraConfigFile = path.join(__dirname, tempConfigPath);

fs.rmSync(extraConfigFile, { force: true });
test.beforeEach(() => {
fs.rmSync(dist, { recursive: true, force: true });
fs.writeFileSync(extraConfigFile, 'export default { foo: 1 };');
fs.rmSync(tempOutputFile, { force: true });
fs.rmSync(extraConfigFile, { force: true });
fs.writeFileSync(extraConfigFile, 'export default 1;');
});

test('should restart dev server when extra config file changed', async () => {
const childProcess = exec('npx rsbuild dev', {
cwd: __dirname,
env: {
Expand All @@ -23,44 +26,59 @@ test('should restart dev server when extra config file changed', async () => {
},
});

// the first build
await awaitFileExists(dist);

fs.rmSync(dist, { recursive: true });
// temp config changed
fs.writeFileSync(extraConfigFile, 'export default { foo: 2 };');
fs.rmSync(tempOutputFile, { force: true });
// temp config changed and trigger rebuild
fs.writeFileSync(extraConfigFile, 'export default 2;');

// rebuild and generate dist files
await awaitFileExists(dist);
expect(fs.existsSync(path.join(dist, 'temp.txt')));
await awaitFileExists(tempOutputFile);
expect(fs.readFileSync(tempOutputFile, 'utf-8')).toEqual('2');

childProcess.kill();
});

test('should not restart dev server if `watchFiles.type` is `reload-page`', async () => {
const dist = path.join(__dirname, 'dist');
const extraConfigFile = path.join(__dirname, tempConfigPath);
const childProcess = exec('npx rsbuild dev', {
cwd: __dirname,
env: {
...process.env,
PORT: String(await getRandomPort()),
WATCH_FILES_TYPE: 'reload-page',
},
});

fs.rmSync(extraConfigFile, { force: true });
fs.rmSync(dist, { recursive: true, force: true });
fs.writeFileSync(extraConfigFile, 'export default { foo: 1 };');
await awaitFileExists(dist);

fs.rmSync(dist, { recursive: true });
// temp config changed
fs.writeFileSync(extraConfigFile, 'export default 2;');

await new Promise((resolve) => setTimeout(resolve, 300));
expect(fs.readFileSync(tempOutputFile, 'utf-8')).toEqual('1');

childProcess.kill();
});

test('should not restart dev server if `watchFiles.type` is not set', async () => {
const childProcess = exec('npx rsbuild dev', {
cwd: __dirname,
env: {
...process.env,
PORT: String(await getRandomPort()),
WATCH_FILES_TYPE: 'reload-page',
},
});

await awaitFileExists(dist);

fs.rmSync(dist, { recursive: true });
// temp config changed
fs.writeFileSync(extraConfigFile, 'export default { foo: 2 };');
fs.writeFileSync(extraConfigFile, 'export default 2;');

await new Promise((resolve) => setTimeout(resolve, 300));
expect(fs.existsSync(path.join(dist, 'temp.txt'))).toBeFalsy();
expect(fs.readFileSync(tempOutputFile, 'utf-8')).toEqual('1');

childProcess.kill();
});
4 changes: 2 additions & 2 deletions e2e/cases/cli/watch-files/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import content from './test-temp-config';
const testPlugin: RsbuildPlugin = {
name: 'test-plugin',
setup(api) {
api.onBeforeBuild(() => {
api.onBeforeCreateCompiler(() => {
fse.outputFileSync(
path.join(api.context.distPath, 'temp.txt'),
path.join(__dirname, 'test-temp-file.txt'),
JSON.stringify(content),
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function init({

if (config.dev?.watchFiles) {
for (const watchFilesConfig of castArray(config.dev.watchFiles)) {
if (watchFilesConfig.type === 'reload-page') {
if (watchFilesConfig.type !== 'reload-server') {
continue;
}

Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/server/watchFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ function prepareWatchOptions(
}

async function startWatchFiles(
{ paths, options, type }: ReturnType<typeof prepareWatchOptions>,
{
paths,
options,
type = 'reload-page',
}: ReturnType<typeof prepareWatchOptions>,
compileMiddlewareAPI: CompileMiddlewareAPI,
) {
// If `type` is 'reload-server', skip it.
if (type === 'reload-server') {
if (type !== 'reload-page') {
return;
}

Expand Down

0 comments on commit 05c6c65

Please sign in to comment.