11/* eslint-disable no-console */
22let warned : Record < string , boolean > = { } ;
33
4+ export type preMessageFn = (
5+ message : string ,
6+ type : 'warning' | 'note' ,
7+ ) => string | null | undefined | number ;
8+
9+ const preWarningFns : preMessageFn [ ] = [ ] ;
10+
11+ /**
12+ * Pre warning enable you to parse content before console.error.
13+ * Modify to null will prevent warning.
14+ */
15+ export const preMessage = ( fn : preMessageFn ) => {
16+ preWarningFns . push ( fn ) ;
17+ } ;
18+
419export function warning ( valid : boolean , message : string ) {
520 // Support uglify
6- if ( process . env . NODE_ENV !== 'production' && ! valid && console !== undefined ) {
7- console . error ( `Warning: ${ message } ` ) ;
21+ if (
22+ process . env . NODE_ENV !== 'production' &&
23+ ! valid &&
24+ console !== undefined
25+ ) {
26+ const finalMessage = preWarningFns . reduce (
27+ ( msg , preMessageFn ) => preMessageFn ( msg ?? '' , 'warning' ) ,
28+ message ,
29+ ) ;
30+
31+ if ( finalMessage ) {
32+ console . error ( `Warning: ${ finalMessage } ` ) ;
33+ }
834 }
935}
1036
1137export function note ( valid : boolean , message : string ) {
1238 // Support uglify
13- if ( process . env . NODE_ENV !== 'production' && ! valid && console !== undefined ) {
14- console . warn ( `Note: ${ message } ` ) ;
39+ if (
40+ process . env . NODE_ENV !== 'production' &&
41+ ! valid &&
42+ console !== undefined
43+ ) {
44+ const finalMessage = preWarningFns . reduce (
45+ ( msg , preMessageFn ) => preMessageFn ( msg ?? '' , 'note' ) ,
46+ message ,
47+ ) ;
48+
49+ if ( finalMessage ) {
50+ console . warn ( `Note: ${ finalMessage } ` ) ;
51+ }
1552 }
1653}
1754
@@ -38,5 +75,9 @@ export function noteOnce(valid: boolean, message: string) {
3875 call ( note , valid , message ) ;
3976}
4077
78+ warningOnce . preMessage = preMessage ;
79+ warningOnce . resetWarned = resetWarned ;
80+ warningOnce . noteOnce = noteOnce ;
81+
4182export default warningOnce ;
4283/* eslint-enable */
0 commit comments