Skip to content

Commit 46121f5

Browse files
committed
feat: add option watchIgnore
1 parent e2f51f2 commit 46121f5

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ function resolveConfig(config: Config): Context {
8585

8686
const defaultConfig = {
8787
source: "src",
88+
watchIgnore: [],
8889
dist: ".scaffold/build",
8990

9091
name: "",

src/core/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ export default class Serve extends Base {
6262
* watch source dir and build when file changed
6363
*/
6464
async watch() {
65-
const { source } = this.ctx;
65+
const { source, watchIgnore } = this.ctx;
6666

6767
watch(
6868
source,
69+
watchIgnore,
6970
{
7071
onReady: async () => {
7172
await this.ctx.hooks.callHook("serve:ready", this.ctx);

src/core/tester/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export default class Test extends Base {
7676

7777
watch(
7878
[this.ctx.source, this.ctx.test.entries].flat(),
79+
this.ctx.watchIgnore,
7980
{
8081
onChange: async (path) => {
8182
if (isSource(path)) {

src/types/config.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,33 @@ import type { UpdateJSON } from "./update-json.js";
77

88
export interface Config {
99
/**
10-
* The source code directories.
10+
* Source root directory or directories for the plugin.
1111
*
12-
* Can be multiple directories, and changes to these directories will be watched when `server` is running.
12+
* These paths are:
13+
* - Watched during development to trigger rebuilds on changes.
14+
* - Used to remove the top-level asset directory (e.g. `assets/`) during build,
15+
* so that only its contents are copied directly to the output root.
1316
*
14-
* 源码目录
17+
* 插件的源码根目录(可为字符串或字符串数组)
1518
*
16-
* 可以是多个目录,将在 `server` 运行时监听这些目录的变更。
19+
* 用于以下两个目的:
20+
* - 在开发模式下监听这些目录,以便在文件变更时触发重建;
21+
* - 在构建阶段中,用于识别 assets 所在的顶层目录,以移除该目录本身,仅将其内容复制到输出目录的根部。
1722
*
23+
* @example ["src", "addon"]
1824
* @default "src"
1925
*/
2026
source: string | string[];
27+
28+
/**
29+
* The files to ignore when watching.
30+
*
31+
* 忽略的监听文件。
32+
*
33+
* @default []
34+
*/
35+
watchIgnore: string | string[] | RegExp | RegExp[];
36+
2137
/**
2238
* The build directories.
2339
*

src/utils/watcher.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import type { Stats } from "node:fs";
22
import chokidar from "chokidar";
33
import { debounce } from "es-toolkit";
44
import { logger } from "./logger.js";
5+
import { toArray } from "./string.js";
56

67
export function watch(
78
source: string | string[],
9+
ignore: string | string[] | RegExp | RegExp[],
810
event: {
911
onReady?: () => any;
1012
onChange: (path: string) => any | Promise<any>;
@@ -13,7 +15,14 @@ export function watch(
1315
},
1416
) {
1517
const watcher = chokidar.watch(source, {
16-
ignored: /(^|[/\\])\../, // ignore dotfiles
18+
ignored: [
19+
/(^|[/\\])\../, // ignore dotfiles
20+
/[\\/]\.git[\\/]/,
21+
/[\\/]node_modules[\\/]/,
22+
...toArray(ignore),
23+
],
24+
ignoreInitial: true,
25+
ignorePermissionErrors: true,
1726
persistent: true,
1827
});
1928

0 commit comments

Comments
 (0)