From 37a98e0f99de3ace81618bff1a3873dc85035d19 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Tue, 8 Oct 2024 14:31:44 +0800 Subject: [PATCH 1/2] feat: support dev.watchFiles reload-server option --- .changeset/fluffy-owls-provide.md | 5 +++++ packages/solutions/app-tools/src/index.ts | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 .changeset/fluffy-owls-provide.md 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/solutions/app-tools/src/index.ts b/packages/solutions/app-tools/src/index.ts index 939ce8913a57..f75e8139487a 100644 --- a/packages/solutions/app-tools/src/index.ts +++ b/packages/solutions/app-tools/src/index.ts @@ -152,7 +152,21 @@ 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 = config.dev.watchFiles; + if (watchFiles?.type === 'reload-server') { + files.push( + ...(Array.isArray(watchFiles.paths) + ? watchFiles.paths + : [watchFiles.paths]), + ); + } + + return files; }, // 这里会被 core/initWatcher 监听的文件变动触发,如果是 src 目录下的文件变动,则不做 restart From a99ac598ddb6e61aa82596f5fe03e1885bbc5e49 Mon Sep 17 00:00:00 2001 From: "gaoyuan.1226" Date: Wed, 9 Oct 2024 17:44:11 +0800 Subject: [PATCH 2/2] fix: support watchFiles array --- .../docs/en/configure/app/dev/watch-files.mdx | 19 +++++++++++++------ .../docs/zh/configure/app/dev/watch-files.mdx | 19 +++++++++++++------ packages/solutions/app-tools/src/index.ts | 15 +++++++-------- 3 files changed, 33 insertions(+), 20 deletions(-) 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 f75e8139487a..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, @@ -157,14 +158,12 @@ export const appTools = ( config.source.configDir, ); - const watchFiles = config.dev.watchFiles; - if (watchFiles?.type === 'reload-server') { - files.push( - ...(Array.isArray(watchFiles.paths) - ? watchFiles.paths - : [watchFiles.paths]), - ); - } + const watchFiles = castArray(config.dev.watchFiles); + watchFiles.forEach(({ type, paths }) => { + if (type === 'reload-server') { + files.push(...(Array.isArray(paths) ? paths : [paths])); + } + }); return files; },