@@ -123,7 +123,9 @@ interface PluginConfig {
123123 markerSpecifiers : { serverOnly : Set < string > ; clientOnly : Set < string > }
124124 envTypeMap : Map < string , 'client' | 'server' >
125125
126- onViolation ?: ( info : ViolationInfo ) => boolean | void
126+ onViolation ?: (
127+ info : ViolationInfo ,
128+ ) => boolean | void | Promise < boolean | void >
127129}
128130
129131/**
@@ -787,7 +789,7 @@ export function importProtectionPlugin(
787789 }
788790
789791 if ( config . onViolation ) {
790- const result = config . onViolation ( pv . info )
792+ const result = await config . onViolation ( pv . info )
791793 if ( result === false ) continue
792794 }
793795 warnFn ( formatViolation ( pv . info , config . root ) )
@@ -830,12 +832,17 @@ export function importProtectionPlugin(
830832 /** Counter for generating unique per-violation mock module IDs in build mode. */
831833 let buildViolationCounter = 0
832834
833- function handleViolation (
835+ type HandleViolationResult =
836+ | { id : string ; syntheticNamedExports : boolean }
837+ | string
838+ | undefined
839+
840+ async function handleViolation (
834841 ctx : ViolationReporter ,
835842 env : EnvState ,
836843 info : ViolationInfo ,
837844 violationOpts ?: { silent ?: boolean } ,
838- ) : { id : string ; syntheticNamedExports : boolean } | string | undefined {
845+ ) : Promise < HandleViolationResult > {
839846 const key = dedupeKey (
840847 info . type ,
841848 info . importer ,
@@ -845,7 +852,7 @@ export function importProtectionPlugin(
845852
846853 if ( ! violationOpts ?. silent ) {
847854 if ( config . onViolation ) {
848- const result = config . onViolation ( info )
855+ const result = await config . onViolation ( info )
849856 if ( result === false ) {
850857 return undefined
851858 }
@@ -924,9 +931,9 @@ export function importProtectionPlugin(
924931 info : ViolationInfo ,
925932 shouldDefer : boolean ,
926933 isPreTransformResolve : boolean ,
927- ) : Promise < ReturnType < typeof handleViolation > > {
934+ ) : Promise < HandleViolationResult > {
928935 if ( shouldDefer ) {
929- const result = handleViolation ( ctx , env , info , { silent : true } )
936+ const result = await handleViolation ( ctx , env , info , { silent : true } )
930937
931938 if ( config . command === 'build' ) {
932939 // Build mode: store for generateBundle tree-shaking check.
@@ -941,7 +948,7 @@ export function importProtectionPlugin(
941948
942949 return result
943950 }
944- return handleViolation ( ctx , env , info , {
951+ return await handleViolation ( ctx , env , info , {
945952 silent : isPreTransformResolve ,
946953 } )
947954 }
@@ -1444,7 +1451,7 @@ export function importProtectionPlugin(
14441451 } ,
14451452 } ,
14461453
1447- generateBundle ( _options , bundle ) {
1454+ async generateBundle ( _options , bundle ) {
14481455 const envName = this . environment . name
14491456 const env = envStates . get ( envName )
14501457 if ( ! env || env . deferredBuildViolations . length === 0 ) return
@@ -1463,9 +1470,14 @@ export function importProtectionPlugin(
14631470 // in the bundle, the import was NOT tree-shaken — real leak.
14641471 const realViolations : Array < ViolationInfo > = [ ]
14651472 for ( const { info, mockModuleId } of env . deferredBuildViolations ) {
1466- if ( survivingModules . has ( mockModuleId ) ) {
1467- realViolations . push ( info )
1473+ if ( ! survivingModules . has ( mockModuleId ) ) continue
1474+
1475+ if ( config . onViolation ) {
1476+ const result = await config . onViolation ( info )
1477+ if ( result === false ) continue
14681478 }
1479+
1480+ realViolations . push ( info )
14691481 }
14701482
14711483 if ( realViolations . length === 0 ) return
0 commit comments