11import type { DevToolsLogLevel } from '@vitejs/devtools-kit'
2- import { getDevToolsRpcClient } from '@vitejs/devtools-kit/client'
2+ import type { DockClientScriptContext } from '@vitejs/devtools-kit/client'
33import axe from 'axe-core'
44
55const SOURCE = 'a11y-checker'
6+ const SUMMARY_LOG_ID = 'a11y-checker-summary'
67
78function impactToLevel ( impact : string | undefined | null ) : DevToolsLogLevel {
89 switch ( impact ) {
@@ -17,8 +18,18 @@ function impactToLevel(impact: string | undefined | null): DevToolsLogLevel {
1718 }
1819}
1920
20- async function runA11yCheck ( ) : Promise < void > {
21- const rpc = await getDevToolsRpcClient ( )
21+ export default async function runA11yCheck ( context : DockClientScriptContext ) : Promise < void > {
22+ const { rpc } = context
23+
24+ // Show loading state
25+ await rpc . call ( 'devtoolskit:internal:logs:add' , {
26+ id : SUMMARY_LOG_ID ,
27+ message : 'Running accessibility audit...' ,
28+ level : 'info' as DevToolsLogLevel ,
29+ category : 'a11y' ,
30+ status : 'loading' ,
31+ notify : true ,
32+ } , SOURCE )
2233
2334 try {
2435 const results = await axe . run ( document )
@@ -28,6 +39,7 @@ async function runA11yCheck(): Promise<void> {
2839 const firstNode = violation . nodes [ 0 ]
2940
3041 await rpc . call ( 'devtoolskit:internal:logs:add' , {
42+ id : `a11y-violation-${ violation . id } ` ,
3143 message : violation . description ,
3244 level,
3345 description : `${ violation . help } \n\n${ violation . helpUrl } ` ,
@@ -45,31 +57,29 @@ async function runA11yCheck(): Promise<void> {
4557 const violationCount = results . violations . length
4658 const passCount = results . passes . length
4759
60+ // Update the summary log (dedup by id)
4861 await rpc . call ( 'devtoolskit:internal:logs:add' , {
62+ id : SUMMARY_LOG_ID ,
4963 message : violationCount > 0
5064 ? `Found ${ violationCount } violation${ violationCount > 1 ? 's' : '' } , ${ passCount } passed`
5165 : `All ${ passCount } checks passed` ,
5266 level : ( violationCount > 0 ? 'warn' : 'success' ) as DevToolsLogLevel ,
5367 category : 'a11y' ,
68+ status : 'idle' ,
5469 notify : true ,
5570 autoDismiss : 4000 ,
5671 } , SOURCE )
5772 }
5873 catch ( err ) {
74+ // Update the summary log with error
5975 await rpc . call ( 'devtoolskit:internal:logs:add' , {
76+ id : SUMMARY_LOG_ID ,
6077 message : 'A11y audit failed' ,
6178 level : 'error' as DevToolsLogLevel ,
6279 description : String ( err ) ,
6380 category : 'a11y' ,
81+ status : 'idle' ,
6482 notify : true ,
6583 } , SOURCE )
6684 }
6785}
68-
69- // Auto-execute after page load
70- if ( document . readyState === 'complete' ) {
71- runA11yCheck ( )
72- }
73- else {
74- window . addEventListener ( 'load' , ( ) => runA11yCheck ( ) )
75- }
0 commit comments