@@ -1509,3 +1509,48 @@ describe('vite inspect batched writes', () => {
15091509 ] )
15101510 } )
15111511} )
1512+
1513+ describe ( 'vite inspect modules list' , ( ) => {
1514+ it ( 'reuses the list until a recorded change and then rebuilds it' , async ( ) => {
1515+ const { ctx, envCtx, vite } = await createFixture ( )
1516+ const plugin = vite . config . plugins [ 1 ]
1517+ const record = ( id : string ) => {
1518+ envCtx . recordTransform ( id , { name : 'plugin-a' , result : `export const id = '${ id } '` , start : 0 , end : 1 } , `const id = '${ id } '` , plugin )
1519+ }
1520+
1521+ record ( '/src/a.ts' )
1522+ const first = await envCtx . getModulesList ( )
1523+ expect ( first . map ( module => module . id ) ) . toEqual ( [ '/src/a.ts' ] )
1524+ expect ( await envCtx . getModulesList ( ) ) . toBe ( first )
1525+
1526+ // Finishing a transform request doesn't change the recorded modules.
1527+ ctx . store . clearPendingLoadMetrics ( envCtx . scope , { } )
1528+ expect ( await envCtx . getModulesList ( ) ) . toBe ( first )
1529+
1530+ record ( '/src/b.ts' )
1531+ const second = await envCtx . getModulesList ( )
1532+ expect ( second . map ( module => module . id ) ) . toEqual ( [ '/src/a.ts' , '/src/b.ts' ] )
1533+
1534+ envCtx . invalidate ( '/src/a.ts' )
1535+ expect ( ( await envCtx . getModulesList ( ) ) . map ( module => module . id ) ) . toEqual ( [ '/src/b.ts' ] )
1536+ } )
1537+
1538+ it ( 'builds the full sorted list when it takes several slices' , async ( ) => {
1539+ const { envCtx, vite } = await createFixture ( )
1540+ const plugin = vite . config . plugins [ 1 ]
1541+ const ids = Array . from ( { length : 3000 } , ( _ , index ) => `/src/m${ String ( index ) . padStart ( 4 , '0' ) } .ts` )
1542+ for ( const id of ids )
1543+ envCtx . recordTransform ( id , { name : 'plugin-a' , result : 'export {}' , start : 0 , end : 1 } , 'const a = 1' , plugin )
1544+
1545+ // Make every module take longer than a slice, so the build yields between modules.
1546+ const now = vi . spyOn ( performance , 'now' )
1547+ let clock = 0
1548+ now . mockImplementation ( ( ) => ( clock += 10 ) )
1549+ try {
1550+ expect ( ( await envCtx . getModulesList ( ) ) . map ( module => module . id ) ) . toEqual ( ids )
1551+ }
1552+ finally {
1553+ now . mockRestore ( )
1554+ }
1555+ } )
1556+ } )
0 commit comments