Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9026,7 +9026,7 @@ export class Watching {
export type WatchOptions = {
aggregateTimeout?: number;
followSymlinks?: boolean;
ignored?: string | RegExp | string[];
ignored?: string | RegExp | (string | RegExp)[];
poll?: number | boolean;
stdin?: boolean;
};
Expand Down
20 changes: 16 additions & 4 deletions packages/rspack/src/NativeWatchFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ const stringToRegexp = (ignored: string) => {

const ignoredToFunction = (ignored: Watchpack.WatchOptions["ignored"]) => {
if (Array.isArray(ignored)) {
const stringRegexps = ignored.map(i => stringToRegexp(i)).filter(Boolean);
if (stringRegexps.length === 0) {
const regexps: RegExp[] = [];
for (const item of ignored) {
if (typeof item === "string") {
const stringRegexp = stringToRegexp(item);
if (stringRegexp) {
regexps.push(new RegExp(stringRegexp));
}
} else if (item instanceof RegExp) {
regexps.push(item);
}
}
if (regexps.length === 0) {
return () => false;
}
const regexp = new RegExp(stringRegexps.join("|"));
return (item: string) => regexp.test(item.replace(/\\/g, "/"));
return (path: string) => {
const normalizedPath = path.replace(/\\/g, "/");
return regexps.some(regexp => regexp.test(normalizedPath));
};
}
if (typeof ignored === "string") {
const stringRegexp = stringToRegexp(ignored);
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2756,7 +2756,7 @@ export type WatchOptions = {
/**
* Ignore some files from being watched.
*/
ignored?: string | RegExp | string[];
ignored?: string | RegExp | (string | RegExp)[];

/**
* Turn on polling by passing true, or specifying a poll interval in milliseconds.
Expand Down
5 changes: 4 additions & 1 deletion packages/rspack/src/schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,10 @@ export const getRspackOptionsSchema = memoize(() => {
.strictObject({
aggregateTimeout: numberOrInfinity,
followSymlinks: z.boolean(),
ignored: z.string().array().or(z.instanceof(RegExp)).or(z.string()),
ignored: z
.string()
.or(z.instanceof(RegExp))
.or(z.string().or(z.instanceof(RegExp)).array()),
poll: numberOrInfinity.or(z.boolean()),
stdin: z.boolean()
})
Expand Down
13 changes: 12 additions & 1 deletion website/docs/en/config/watch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default {

### watchOptions.ignored

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

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

You can also mix strings and regular expressions in an array:

```js title="rspack.config.mjs"
export default {
//...
watchOptions: {
ignored: [/\.git/, '**/node_modules', '**/.cache'],
},
};
```

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.

### watchOptions.poll
Expand Down
13 changes: 12 additions & 1 deletion website/docs/zh/config/watch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default {

### watchOptions.ignored

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

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

你还可以在数组中混合使用字符串和正则表达式:

```js title="rspack.config.mjs"
export default {
//...
watchOptions: {
ignored: [/\.git/, '**/node_modules', '**/.cache'],
},
};
```

当使用 glob 匹配时,Rspack 会将它们转换为正则表达式,因此在使用 glob 匹配之前,请确保你熟悉 [glob-to-regexp](https://github.com/fitzgen/glob-to-regexp)。

### watchOptions.poll
Expand Down
Loading