Skip to content

Commit f5a7ee7

Browse files
committed
feat: support mixed array types for watchOptions.ignored
- Support (string | RegExp)[] for watchOptions.ignored configuration - Update type definitions to allow mixed arrays in WatchOptions - Enhance ignoredToFunction to handle both string and RegExp items - Update schema validation with Zod to accept mixed array types - Add documentation examples for mixed array usage - Update API documentation to reflect new type signature This allows users to combine string patterns and RegExp objects in watchOptions.ignored for more flexible file exclusion patterns. Closes #10596
1 parent fcc1674 commit f5a7ee7

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

packages/rspack/etc/core.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9026,7 +9026,7 @@ export class Watching {
90269026
export type WatchOptions = {
90279027
aggregateTimeout?: number;
90289028
followSymlinks?: boolean;
9029-
ignored?: string | RegExp | string[];
9029+
ignored?: string | RegExp | (string | RegExp)[];
90309030
poll?: number | boolean;
90319031
stdin?: boolean;
90329032
};

packages/rspack/src/NativeWatchFileSystem.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ const stringToRegexp = (ignored: string) => {
2222

2323
const ignoredToFunction = (ignored: Watchpack.WatchOptions["ignored"]) => {
2424
if (Array.isArray(ignored)) {
25-
const stringRegexps = ignored.map(i => stringToRegexp(i)).filter(Boolean);
26-
if (stringRegexps.length === 0) {
25+
const regexps: RegExp[] = [];
26+
for (const item of ignored) {
27+
if (typeof item === "string") {
28+
const stringRegexp = stringToRegexp(item);
29+
if (stringRegexp) {
30+
regexps.push(new RegExp(stringRegexp));
31+
}
32+
} else if (item instanceof RegExp) {
33+
regexps.push(item);
34+
}
35+
}
36+
if (regexps.length === 0) {
2737
return () => false;
2838
}
29-
const regexp = new RegExp(stringRegexps.join("|"));
30-
return (item: string) => regexp.test(item.replace(/\\/g, "/"));
39+
return (path: string) => {
40+
const normalizedPath = path.replace(/\\/g, "/");
41+
return regexps.some(regexp => regexp.test(normalizedPath));
42+
};
3143
}
3244
if (typeof ignored === "string") {
3345
const stringRegexp = stringToRegexp(ignored);

packages/rspack/src/config/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,7 @@ export type WatchOptions = {
27562756
/**
27572757
* Ignore some files from being watched.
27582758
*/
2759-
ignored?: string | RegExp | string[];
2759+
ignored?: string | RegExp | (string | RegExp)[];
27602760

27612761
/**
27622762
* Turn on polling by passing true, or specifying a poll interval in milliseconds.

packages/rspack/src/schema/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,10 @@ export const getRspackOptionsSchema = memoize(() => {
14401440
.strictObject({
14411441
aggregateTimeout: numberOrInfinity,
14421442
followSymlinks: z.boolean(),
1443-
ignored: z.string().array().or(z.instanceof(RegExp)).or(z.string()),
1443+
ignored: z
1444+
.string()
1445+
.or(z.instanceof(RegExp))
1446+
.or(z.string().or(z.instanceof(RegExp)).array()),
14441447
poll: numberOrInfinity.or(z.boolean()),
14451448
stdin: z.boolean()
14461449
})

website/docs/en/config/watch.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default {
5858

5959
### watchOptions.ignored
6060

61-
- **Type:** `RegExp | string | string[]`
61+
- **Type:** `RegExp | string | (string | RegExp)[]`
6262
- **Default:** `/[\\/](?:\.git|node_modules)[\\/]/`
6363

6464
The path that matches is excluded while watching. Watching many files can result in a lot of CPU or memory usage.
@@ -111,6 +111,17 @@ export default {
111111
};
112112
```
113113

114+
You can also mix strings and regular expressions in an array:
115+
116+
```js title="rspack.config.mjs"
117+
export default {
118+
//...
119+
watchOptions: {
120+
ignored: [/\.git/, '**/node_modules', '**/.cache'],
121+
},
122+
};
123+
```
124+
114125
When using glob patterns, Rspack convert them to regular expressions with [glob-to-regexp](https://github.com/fitzgen/glob-to-regexp), so make sure to get yourself familiar with it before you use glob patterns for watchOptions.ignored.
115126

116127
### watchOptions.poll

website/docs/zh/config/watch.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default {
5858

5959
### watchOptions.ignored
6060

61-
- **类型:** `RegExp | string | string[]`
61+
- **类型:** `RegExp | string | (string | RegExp)[]`
6262
- **默认值:** `/[\\/](?:\.git|node_modules)[\\/]/`
6363

6464
监听时排除匹配到的路径。监听大量文件可能会导致 CPU 或内存使用率过高。
@@ -111,6 +111,17 @@ export default {
111111
};
112112
```
113113

114+
你还可以在数组中混合使用字符串和正则表达式:
115+
116+
```js title="rspack.config.mjs"
117+
export default {
118+
//...
119+
watchOptions: {
120+
ignored: [/\.git/, '**/node_modules', '**/.cache'],
121+
},
122+
};
123+
```
124+
114125
当使用 glob 匹配时,Rspack 会将它们转换为正则表达式,因此在使用 glob 匹配之前,请确保你熟悉 [glob-to-regexp](https://github.com/fitzgen/glob-to-regexp)
115126

116127
### watchOptions.poll

0 commit comments

Comments
 (0)