Skip to content

Commit 3ae4c52

Browse files
committed
refactor: extract normalization for ignoreWarnings option
1 parent e58e0ba commit 3ae4c52

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

packages/rspack/src/config/normalization.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import type {
5050
HotUpdateChunkFilename,
5151
HotUpdateGlobal,
5252
HotUpdateMainFilename,
53+
IgnoreWarnings,
5354
Iife,
5455
ImportFunctionName,
5556
ImportMetaName,
@@ -91,33 +92,52 @@ import type {
9192
WorkerPublicPath
9293
} from "./types";
9394

95+
/**
96+
* Normalize the `ignoreWarnings` option into an array of predicate functions.
97+
*/
98+
const normalizeIgnoreWarnings = (ignoreWarnings?: IgnoreWarnings) => {
99+
if (!ignoreWarnings) {
100+
return undefined;
101+
}
102+
103+
return ignoreWarnings.map(ignore => {
104+
if (typeof ignore === "function") {
105+
return ignore;
106+
}
107+
108+
const rule = ignore instanceof RegExp ? { message: ignore } : ignore;
109+
110+
return (warning: WebpackError) => {
111+
if (!rule.message && !rule.module && !rule.file) {
112+
return false;
113+
}
114+
115+
if (rule.message && !rule.message.test(warning.message)) {
116+
return false;
117+
}
118+
119+
if (
120+
rule.module &&
121+
(!warning.module ||
122+
!rule.module.test(warning.module.readableIdentifier()))
123+
) {
124+
return false;
125+
}
126+
127+
if (rule.file && (!warning.file || !rule.file.test(warning.file))) {
128+
return false;
129+
}
130+
131+
return true;
132+
};
133+
});
134+
};
135+
94136
export const getNormalizedRspackOptions = (
95137
config: RspackOptions
96138
): RspackOptionsNormalized => {
97139
return {
98-
ignoreWarnings: config.ignoreWarnings
99-
? config.ignoreWarnings.map(ignore => {
100-
if (typeof ignore === "function") return ignore;
101-
const i = ignore instanceof RegExp ? { message: ignore } : ignore;
102-
return (warning: WebpackError) => {
103-
if (!i.message && !i.module && !i.file) return false;
104-
if (i.message && !i.message.test(warning.message)) {
105-
return false;
106-
}
107-
if (
108-
i.module &&
109-
(!warning.module ||
110-
!i.module.test(warning.module.readableIdentifier()))
111-
) {
112-
return false;
113-
}
114-
if (i.file && (!warning.file || !i.file.test(warning.file))) {
115-
return false;
116-
}
117-
return true;
118-
};
119-
})
120-
: undefined,
140+
ignoreWarnings: normalizeIgnoreWarnings(config.ignoreWarnings),
121141
name: config.name,
122142
dependencies: config.dependencies,
123143
context: config.context,

0 commit comments

Comments
 (0)