rspack icon indicating copy to clipboard operation
rspack copied to clipboard

[Feature]: `watchOptions.ignored` support `(string | RegExp)[]` type

Open 9aoy opened this issue 6 months ago • 5 comments

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)[];

9aoy avatar Jun 06 '25 07:06 9aoy

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

GiveMe-A-Name avatar Jul 08 '25 09:07 GiveMe-A-Name

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.

9aoy avatar Jul 08 '25 10:07 9aoy

Why not using ignoredFunction? It's more flexible then Array.

GiveMe-A-Name avatar Jul 09 '25 02:07 GiveMe-A-Name

Why not using ignoredFunction? It's more flexible then Array.

Function type sounds OK. BTW, this configuration type is not very framework friendly.

9aoy avatar Jul 09 '25 06:07 9aoy

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:

  1. Type Definitions (packages/rspack/src/config/types.ts):

    • Updated WatchOptions.ignored type from string | RegExp | string[] to string | RegExp | (string | RegExp)[]
  2. Schema Validation (packages/rspack/src/schema/config.ts):

    • Updated the Zod schema to accept mixed arrays
  3. Runtime Logic (packages/rspack/src/NativeWatchFileSystem.ts):

    • Enhanced the ignoredToFunction to properly handle mixed arrays by type-checking each item and converting strings to RegExp while keeping RegExp objects as-is
  4. 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 WatchOptions type: (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

  1. Wait for upstream: Contribute this feature to the watchpack repository first
  2. Remove dependencies: Plan to remove @types/watchpack and watchpack dependencies
  3. 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.

Image Image Image Image

Perfecto23 avatar Jul 16 '25 15:07 Perfecto23