diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 95101ed7b5c7..945b5b491814 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -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; }; diff --git a/packages/rspack/src/NativeWatchFileSystem.ts b/packages/rspack/src/NativeWatchFileSystem.ts index 63ff28c905bb..e937e18ab744 100644 --- a/packages/rspack/src/NativeWatchFileSystem.ts +++ b/packages/rspack/src/NativeWatchFileSystem.ts @@ -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); diff --git a/packages/rspack/src/config/types.ts b/packages/rspack/src/config/types.ts index 7fe5d792cc66..a8c6e1947c1e 100644 --- a/packages/rspack/src/config/types.ts +++ b/packages/rspack/src/config/types.ts @@ -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. diff --git a/packages/rspack/src/schema/config.ts b/packages/rspack/src/schema/config.ts index 13e1775f591c..d3fa128f8af5 100644 --- a/packages/rspack/src/schema/config.ts +++ b/packages/rspack/src/schema/config.ts @@ -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() }) diff --git a/website/docs/en/config/watch.mdx b/website/docs/en/config/watch.mdx index 6cdfa2a84e5c..12f57487214f 100644 --- a/website/docs/en/config/watch.mdx +++ b/website/docs/en/config/watch.mdx @@ -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. @@ -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 diff --git a/website/docs/zh/config/watch.mdx b/website/docs/zh/config/watch.mdx index 391a6cfd6952..c934bbcd005f 100644 --- a/website/docs/zh/config/watch.mdx +++ b/website/docs/zh/config/watch.mdx @@ -58,7 +58,7 @@ export default { ### watchOptions.ignored -- **类型:** `RegExp | string | string[]` +- **类型:** `RegExp | string | (string | RegExp)[]` - **默认值:** `/[\\/](?:\.git|node_modules)[\\/]/` 监听时排除匹配到的路径。监听大量文件可能会导致 CPU 或内存使用率过高。 @@ -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