import { Compiler , Plugin , Stats } from 'webpack' ; import { AllowedFilter , ExcludeOption , WebpackFilterWarningsPluginOptions , WebpackLogWarning } from './interfaces' ; export class FilterWarningsPlugin implements Plugin { private static convertToAllowedValues ( exclude ) { return exclude . map ( ( excludeEntry ) => { if ( typeof excludeEntry === 'string' ) { excludeEntry = new RegExp ( excludeEntry , 'i' ) ; } return excludeEntry ; } ) ; } private static filterWarning ( exclude , warning ) { return ! exclude . some ( ( filter ) => { const message = ( warning as { message : string ; } ) . message ? ( warning as { message : string ; } ) . message : ( warning as string || '' ) ; if ( filter instanceof RegExp ) { return filter . test ( message ) ; } else if ( typeof filter === 'function' ) { return filter . call ( undefined , message ) ; } return false ; } ) ; } /**
     * Filters warnings array.
     * Mutates the data!
     */ private static filterWarnings ( exclude , result ) { result . compilation . warnings = result . compilation . warnings . filter ( ( warning ) => FilterWarningsPlugin . filterWarning ( exclude , warning ) ) ; return result . compilation . warnings ; } /**
     * Checks if user-provided exclude is compatible with plugin
     */ private static isSupportedOption ( exclude ) { if ( Array . isArray ( exclude ) ) { return exclude . reduce ( ( memo , excludeEntry ) => memo && FilterWarningsPlugin . isSupportedSimpleType ( excludeEntry ) , true ) ; } else { return FilterWarningsPlugin . isSupportedSimpleType ( exclude ) ; } } /**
     * Detects whether single exclude option is of supported type
     */ private static isSupportedSimpleType ( exclude ) { return exclude instanceof RegExp || typeof exclude === 'string' || typeof exclude === 'function' ; } private readonly exclude : AllowedFilter [ ] ; public constructor ( { exclude } ) { if ( ! FilterWarningsPlugin . isSupportedOption ( exclude ) ) { throw new Error ( 'Exclude can only be string, RegExp or Array of these' ) ; } if ( ! Array . isArray ( exclude ) ) { exclude = [ exclude ] ; } this . exclude = FilterWarningsPlugin . convertToAllowedValues ( exclude ) ; } public apply ( compiler ) { compiler . hooks . done . tap ( 'filter-warnings-plugin' , ( result ) => FilterWarningsPlugin . filterWarnings ( this . exclude , result ) ) ; } }