Skip to content

Commit

Permalink
feat: support dev.watchFiles reload-server option (#6336)
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy authored Oct 9, 2024
1 parent afa4880 commit 007fe86
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-owls-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-js/app-tools': patch
---

feat: support dev.watchFiles reload-server option
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ configName: dev.watchFiles
- **Type:**

```ts
type WatchFiles = {
paths: string | string[];
type?: 'reload-page' | 'reload-server';
// watch options for chokidar
options?: WatchOptions;
};
type WatchFiles =
| {
paths: string | string[];
type?: 'reload-page';
// watch options for chokidar
options?: WatchOptions;
}
| {
paths: string | string[];
type?: 'reload-server';
};

type WatchFilesConfig = WatchFiles | WatchFiles[];
```

- **Default:** `undefined`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ configName: dev.watchFiles
- **类型:**

```ts
type WatchFiles = {
paths: string | string[];
type?: 'reload-page' | 'reload-server';
// chokidar 选项
options?: WatchOptions;
};
type WatchFiles =
| {
paths: string | string[];
type?: 'reload-page';
// chokidar 选项
options?: WatchOptions;
}
| {
paths: string | string[];
type?: 'reload-server';
};

type WatchFilesConfig = WatchFiles | WatchFiles[];
```

- **默认值:** `undefined`
Expand Down
15 changes: 14 additions & 1 deletion packages/solutions/app-tools/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import type { CliPlugin } from '@modern-js/core';
import { getLocaleLanguage } from '@modern-js/plugin-i18n/language-detector';
import { castArray } from '@modern-js/uni-builder';
import {
cleanRequireCache,
deprecatedCommands,
Expand Down Expand Up @@ -152,7 +153,19 @@ export const appTools = (
async watchFiles() {
const appContext = api.useAppContext();
const config = api.useResolvedConfigContext();
return await generateWatchFiles(appContext, config.source.configDir);
const files = await generateWatchFiles(
appContext,
config.source.configDir,
);

const watchFiles = castArray(config.dev.watchFiles);
watchFiles.forEach(({ type, paths }) => {
if (type === 'reload-server') {
files.push(...(Array.isArray(paths) ? paths : [paths]));
}
});

return files;
},

// 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart
Expand Down

0 comments on commit 007fe86

Please sign in to comment.