diff --git a/.changeset/fluffy-owls-provide.md b/.changeset/fluffy-owls-provide.md new file mode 100644 index 000000000000..e343e8454c5c --- /dev/null +++ b/.changeset/fluffy-owls-provide.md @@ -0,0 +1,5 @@ +--- +'@modern-js/app-tools': patch +--- + +feat: support dev.watchFiles reload-server option diff --git a/packages/document/main-doc/docs/en/configure/app/dev/watch-files.mdx b/packages/document/main-doc/docs/en/configure/app/dev/watch-files.mdx index 89fd2bd314b5..3510024d6807 100644 --- a/packages/document/main-doc/docs/en/configure/app/dev/watch-files.mdx +++ b/packages/document/main-doc/docs/en/configure/app/dev/watch-files.mdx @@ -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` diff --git a/packages/document/main-doc/docs/zh/configure/app/dev/watch-files.mdx b/packages/document/main-doc/docs/zh/configure/app/dev/watch-files.mdx index 050dcbc96b3d..1e92e01787d8 100644 --- a/packages/document/main-doc/docs/zh/configure/app/dev/watch-files.mdx +++ b/packages/document/main-doc/docs/zh/configure/app/dev/watch-files.mdx @@ -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` diff --git a/packages/solutions/app-tools/src/index.ts b/packages/solutions/app-tools/src/index.ts index 939ce8913a57..136c41db69f0 100644 --- a/packages/solutions/app-tools/src/index.ts +++ b/packages/solutions/app-tools/src/index.ts @@ -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, @@ -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