11import { expect , test } from 'vitest'
2- import { browserLogs , getColor , page } from '~utils'
2+ import type { OutputAsset , OutputChunk , RolldownOutput } from 'rolldown'
3+ import { build } from 'vite'
4+ import { browserLogs , getColor , isBuild , page , testDir } from '~utils'
35
46test ( 'should have no 404s' , ( ) => {
57 browserLogs . forEach ( ( msg ) => {
@@ -35,6 +37,10 @@ test('dynamic css', async () => {
3537 await expect . poll ( ( ) => getColor ( '.dynamic' ) ) . toBe ( 'red' )
3638} )
3739
40+ test ( 'direct dynamic css' , async ( ) => {
41+ await expect . poll ( ( ) => getColor ( '.direct-dynamic' ) ) . toBe ( 'red' )
42+ } )
43+
3844// a CSS-only module shared by multiple chunks becomes a pure CSS chunk that is
3945// removed from the output. The import map must not keep referencing its removed
4046// JS file, otherwise chunks importing it 404 and fail to execute
@@ -47,3 +53,75 @@ test('shared pure css chunk', async () => {
4753test ( 'worker' , async ( ) => {
4854 await expect . poll ( ( ) => page . textContent ( '.worker' ) ) . toBe ( 'worker: pong' )
4955} )
56+
57+ for ( const cssFileName of [ 'dynamic.css' , 'direct-dynamic.css' ] ) {
58+ // This is a correctness requirement, not only a cache optimization: hashed
59+ // chunk filenames are cached as immutable. If the filename stayed the same
60+ // while its code changed to reference a new CSS filename, cached JS could
61+ // load stale CSS alongside newly mapped JS, breaking CSS module selectors.
62+ test . runIf ( isBuild ) (
63+ `${ cssFileName } uses a stable import map specifier` ,
64+ async ( ) => {
65+ const buildWithColor = async ( color : string ) =>
66+ ( await build ( {
67+ root : testDir ,
68+ logLevel : 'silent' ,
69+ build : { write : false } ,
70+ plugins : [
71+ {
72+ name : 'change-dynamic-css' ,
73+ enforce : 'pre' ,
74+ transform ( code , id ) {
75+ if ( id . endsWith ( `/${ cssFileName } ` ) ) {
76+ return code . replace ( 'red' , color )
77+ }
78+ } ,
79+ } ,
80+ ] ,
81+ } ) ) as RolldownOutput
82+
83+ const getIndexChunk = ( output : RolldownOutput ) =>
84+ output . output . find (
85+ ( file ) : file is OutputChunk => file . type === 'chunk' && file . isEntry ,
86+ ) !
87+ const getImportMap = ( output : RolldownOutput ) =>
88+ JSON . parse (
89+ output . output
90+ . find (
91+ ( file ) : file is OutputAsset => file . fileName === 'importmap.json' ,
92+ ) !
93+ . source . toString ( ) ,
94+ ) . imports as Record < string , string >
95+ const getCssFileName = ( output : RolldownOutput ) =>
96+ output . output . find (
97+ ( file ) : file is OutputAsset =>
98+ file . type === 'asset' && file . names . includes ( cssFileName ) ,
99+ ) ! . fileName
100+
101+ const redBuild = await buildWithColor ( 'red' )
102+ const blueBuild = await buildWithColor ( 'blue' )
103+ const redIndex = getIndexChunk ( redBuild )
104+ const blueIndex = getIndexChunk ( blueBuild )
105+
106+ // The importer chunk does not change
107+ // because the CSS reference uses the hash in the import map
108+ expect ( redIndex . fileName ) . toBe ( blueIndex . fileName )
109+ expect ( redIndex . code ) . toBe ( blueIndex . code )
110+
111+ const redImportMap = getImportMap ( redBuild )
112+ const blueImportMap = getImportMap ( blueBuild )
113+ const cssName = cssFileName . slice ( 0 , - '.css' . length )
114+ const cssSpecifier = Object . keys ( redImportMap ) . find (
115+ ( specifier ) =>
116+ specifier . includes ( `/${ cssName } -` ) && specifier . endsWith ( '.css' ) ,
117+ ) !
118+
119+ // The hash in the import map specifier is stable across builds,
120+ // but the mapped filename changes properly
121+ expect ( cssSpecifier ) . toBeDefined ( )
122+ expect ( redImportMap [ cssSpecifier ] ) . toBe ( `/${ getCssFileName ( redBuild ) } ` )
123+ expect ( blueImportMap [ cssSpecifier ] ) . toBe ( `/${ getCssFileName ( blueBuild ) } ` )
124+ expect ( redImportMap [ cssSpecifier ] ) . not . toBe ( blueImportMap [ cssSpecifier ] )
125+ } ,
126+ )
127+ }
0 commit comments