[Feature]: `watchOptions.ignored` support `(string | RegExp)[]` type
What problem does this feature solve?
As a framework developer, I would like to add some extra watchOptions.ignored configuration. If the original watchOptions.ignored is a RegExp, I cannot make the user configuration and my configuration work at the same time, because rspack does not support the (string | RegExp)[] type
https://rspack.rs/config/watch#watchoptionsignored
User's configuration:
export default {
//...
watchOptions: {
ignored: /\.git/,
},
};
After merged configuration:
export default {
//...
watchOptions: {
ignored: [/\.git/, '**/dist'],
},
};
What does the proposed API of configuration look like?
watchOptions.ignored support (string | RegExp)[] type.
- type ignored = RegExp | string | string[];
+ type ignored = RegExp | string | (string | RegExp)[];
Why not using string[]. You can pass ignored using [".git", "**/dist"]
watchpack will transform string to RegExp.
https://github.com/webpack/watchpack/blob/main/lib/watchpack.js#L35-L40
This is configured by the user. My question is how can I append other external configurations if the user has configured RegExp, if the (string | RegExp)[] type is not supported.
Why not using ignoredFunction? It's more flexible then Array.
Why not using ignoredFunction? It's more flexible then Array.
Function type sounds OK. BTW, this configuration type is not very framework friendly.
After investigating this issue thoroughly, I found that supporting mixed array types (string | RegExp)[] for watchOptions.ignored is little hard in Rspack's current architecture.
Root Cause Analysis
The main blocker is that Rspack's watchOptions depends on the watchpack library maintained by the Webpack team. The current type declaration in watchpack is:
ignored?: string[] | string | RegExp | ((path: string) => boolean) | undefined;
This means watchpack doesn't support the mixed array syntax (string | RegExp)[] - it only supports homogeneous string[] arrays, not heterogeneous arrays containing both strings and RegExp objects.
What I've Attempted
I successfully implemented the functionality by modifying the following files:
-
Type Definitions (
packages/rspack/src/config/types.ts):- Updated
WatchOptions.ignoredtype fromstring | RegExp | string[]tostring | RegExp | (string | RegExp)[]
- Updated
-
Schema Validation (
packages/rspack/src/schema/config.ts):- Updated the Zod schema to accept mixed arrays
-
Runtime Logic (
packages/rspack/src/NativeWatchFileSystem.ts):- Enhanced the
ignoredToFunctionto properly handle mixed arrays by type-checking each item and converting strings to RegExp while keeping RegExp objects as-is
- Enhanced the
-
API Documentation:
- Updated the API extractor output to reflect the new type signature
The Implementation Works But...
The actual functionality works perfectly - the mixed array configuration is processed correctly at runtime and files are ignored as expected. However, TypeScript compilation fails due to type incompatibility between:
- Rspack's updated
WatchOptionstype:(string | RegExp)[] - Watchpack's original type:
string[]
Let's see https://github1s.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/watchpack/index.d.ts:144
This creates a type mismatch in the NativeWatchFileSystem.watch() method signature that implements the WatchFileSystem interface.
Recommendation for Issue Status
From my understanding, the web-infra team's dependency on watchpack is primarily limited to type declarations. If the team has no plans to remove @types/watchpack and watchpack dependencies, then this issue should be considered "not a problem" rather than "PR welcome".
However, if the team is considering removing these dependencies in a planned manner, then this issue should remain open as it would become feasible to implement without external constraints.
Current Options
- Wait for upstream: Contribute this feature to the
watchpackrepository first - Remove dependencies: Plan to remove
@types/watchpackandwatchpackdependencies - Close as won't fix: Accept the current limitation due to external constraints
The current workaround for users is to use separate string patterns that achieve the same result as RegExp patterns.