diff --git a/packages/rspack/src/config/types.ts b/packages/rspack/src/config/types.ts index c3244bc3905b..8c3d21e81520 100644 --- a/packages/rspack/src/config/types.ts +++ b/packages/rspack/src/config/types.ts @@ -3057,7 +3057,18 @@ export type RspackOptions = { */ loader?: Loader; /** - * Warnings to ignore during compilation. + * Tells Rspack to suppress specific compilation warnings by matching their + * message, module, or file, or by using a custom function. + * + * @example + * ```js + * export default { + * ignoreWarnings: [ + * /warning from compiler/, + * { file: /node_modules/ }, + * ], + * }; + * ``` */ ignoreWarnings?: IgnoreWarnings; /** diff --git a/website/docs/en/config/other-options.mdx b/website/docs/en/config/other-options.mdx index e92c575fd1cd..7523ab789292 100644 --- a/website/docs/en/config/other-options.mdx +++ b/website/docs/en/config/other-options.mdx @@ -87,17 +87,52 @@ export default [ ## ignoreWarnings - +- **Type:** + +```ts +type IgnoreWarnings = ( + | RegExp + | { + file?: RegExp; + message?: RegExp; + module?: RegExp; + } + | ((warning: WebpackError, compilation: Compilation) => boolean) +)[]; +``` + +- **Default:** `undefined` -Tells Rspack to ignore specific warnings. +Tells Rspack to suppress specific compilation warnings by matching their message, module, or file, or by using a custom function. + +Use a RegExp to match the warning message: ```js title="rspack.config.mjs" export default { - //... - ignoreWarnings: [/warning from compiler/, warning => true], + ignoreWarnings: [/warning from compiler/, /other warning/], +}; +``` + +Use an object to match the warning via `message`, `file`, or `module`: + +```js title="rspack.config.mjs" +export default { + ignoreWarnings: [ + { message: /warning from compiler/ }, + { file: /node_modules/ }, + ], +}; +``` + +Use a function for full control. Return `true` to ignore the warning: + +```js title="rspack.config.mjs" +export default { + ignoreWarnings: [ + (warning, compilation) => { + return /warning from compiler/.test(warning.message || ''); + }, + ], }; ``` diff --git a/website/docs/zh/config/other-options.mdx b/website/docs/zh/config/other-options.mdx index a08afceb86ac..4d1d2a0e53d8 100644 --- a/website/docs/zh/config/other-options.mdx +++ b/website/docs/zh/config/other-options.mdx @@ -86,17 +86,52 @@ export default [ ## ignoreWarnings - +- **类型:** + +```ts +type IgnoreWarnings = ( + | RegExp + | { + file?: RegExp; + message?: RegExp; + module?: RegExp; + } + | ((warning: WebpackError, compilation: Compilation) => boolean) +)[]; +``` + +- **默认值:** `undefined` -告知 Rspack 忽略特定的警告。 +用于在构建过程中忽略特定的编译警告。你可以通过匹配警告的 `message`、`module` 或 `file`,或者提供一个自定义函数,来决定哪些警告应被忽略。 + +使用正则表达式来匹配警告的 `message` 字段: ```js title="rspack.config.mjs" export default { - //... - ignoreWarnings: [/warning from compiler/, warning => true], + ignoreWarnings: [/warning from compiler/, /other warning/], +}; +``` + +通过对象形式,可以分别对 `message`、`file` 或 `module` 等字段进行匹配: + +```js title="rspack.config.mjs" +export default { + ignoreWarnings: [ + { message: /warning from compiler/ }, + { file: /node_modules/ }, + ], +}; +``` + +当需要更灵活的控制逻辑时,可以使用函数形式。返回 `true` 表示忽略该警告。 + +```js title="rspack.config.mjs" +export default { + ignoreWarnings: [ + (warning, compilation) => { + return /warning from compiler/.test(warning.message || ''); + }, + ], }; ```