@@ -43,6 +43,7 @@ interface ViteConfig {
4343
4444interface ViteTestPlugin {
4545 name : string
46+ sharedDuringBuild : true
4647 enforce : 'pre'
4748 apply : ( ) => boolean
4849 config : (
@@ -66,10 +67,38 @@ interface ViteTestPlugin {
6667 timestamp : number
6768 } ) => Promise < unknown [ ] | undefined >
6869 load : ( id : string ) => string | undefined
69- transform : ( code : string , id : string ) => Promise < { code : string } | undefined >
70+ transform : (
71+ this : {
72+ environment ?: {
73+ name : string
74+ config : {
75+ consumer : 'client' | 'server'
76+ build ?: { write ?: boolean }
77+ }
78+ }
79+ } ,
80+ code : string ,
81+ id : string ,
82+ ) => Promise < { code : string } | undefined >
7083 generateBundle : (
84+ this : {
85+ environment ?: {
86+ name : string
87+ config : {
88+ consumer : 'client' | 'server'
89+ build ?: { write ?: boolean }
90+ }
91+ }
92+ } ,
7193 options : object ,
72- bundle : Record < string , { source : string ; name : string } > ,
94+ bundle : Record <
95+ string ,
96+ {
97+ source ?: string
98+ name : string
99+ viteMetadata ?: { importedCss ?: Set < string > }
100+ }
101+ > ,
73102 ) => Promise < void >
74103 resolveId : ( source : string , importer ?: string ) => string | undefined
75104}
@@ -174,6 +203,7 @@ describe('devupUIVitePlugin', () => {
174203 const plugin = createPlugin ( { } )
175204 expect ( plugin ) . toEqual ( {
176205 name : 'devup-ui' ,
206+ sharedDuringBuild : true ,
177207 config : expect . any ( Function ) ,
178208 load : expect . any ( Function ) ,
179209 watchChange : expect . any ( Function ) ,
@@ -572,6 +602,117 @@ describe('devupUIVitePlugin', () => {
572602 expect ( bundle [ 'base.css' ] . source ) . toEqual ( 'final complete sheet' )
573603 } )
574604
605+ it ( 'does not forward server css that the client already emits' , async ( ) => {
606+ const plugin = createPlugin ( { } )
607+ getCssSpy . mockImplementation ( ( fileNum : number | null ) =>
608+ fileNum === null ? 'base sheet' : 'file sheet' ,
609+ )
610+ const serverBundle = {
611+ 'base.css' : { source : 'stale' , name : 'devup-ui.css' } ,
612+ 'file.css' : { source : 'stale' , name : 'devup-ui-3.css' } ,
613+ 'entry.js' : {
614+ name : 'entry' ,
615+ viteMetadata : {
616+ importedCss : new Set ( [ 'base.css' , 'file.css' , 'server-only.css' ] ) ,
617+ } ,
618+ } ,
619+ }
620+ const clientBundle = {
621+ 'base.css' : { source : 'stale' , name : 'devup-ui.css' } ,
622+ 'file.css' : { source : 'stale' , name : 'devup-ui-3.css' } ,
623+ }
624+
625+ await plugin . generateBundle . call (
626+ { environment : { name : 'rsc' , config : { consumer : 'server' } } } ,
627+ { } ,
628+ serverBundle ,
629+ )
630+ await plugin . generateBundle . call (
631+ { environment : { name : 'client' , config : { consumer : 'client' } } } ,
632+ { } ,
633+ clientBundle ,
634+ )
635+
636+ expect ( serverBundle [ 'base.css' ] . source ) . toEqual ( 'base sheet' )
637+ expect ( serverBundle [ 'file.css' ] . source ) . toEqual ( 'file sheet' )
638+ expect ( clientBundle [ 'base.css' ] . source ) . toEqual ( 'base sheet' )
639+ expect ( clientBundle [ 'file.css' ] . source ) . toEqual ( 'file sheet' )
640+ expect ( serverBundle [ 'entry.js' ] . viteMetadata . importedCss ) . toEqual (
641+ new Set ( [ 'server-only.css' ] ) ,
642+ )
643+ } )
644+
645+ it ( 'ignores no-write analysis bundles when tracking server css' , async ( ) => {
646+ const plugin = createPlugin ( { } )
647+ const serverBundle = {
648+ 'file.css' : { source : 'stale' , name : 'devup-ui-3.css' } ,
649+ 'entry.js' : {
650+ name : 'entry' ,
651+ viteMetadata : { importedCss : new Set ( [ 'file.css' ] ) } ,
652+ } ,
653+ }
654+ await plugin . generateBundle . call (
655+ {
656+ environment : {
657+ name : 'rsc' ,
658+ config : { consumer : 'server' , build : { write : false } } ,
659+ } ,
660+ } ,
661+ { } ,
662+ serverBundle ,
663+ )
664+ const clientBundle = {
665+ 'file.css' : { source : 'stale' , name : 'devup-ui-3.css' } ,
666+ }
667+
668+ await plugin . generateBundle . call (
669+ { environment : { name : 'client' , config : { consumer : 'client' } } } ,
670+ { } ,
671+ clientBundle ,
672+ )
673+
674+ expect ( serverBundle [ 'entry.js' ] . viteMetadata . importedCss ) . toEqual (
675+ new Set ( [ 'file.css' ] ) ,
676+ )
677+ } )
678+
679+ it ( 'keeps server forwarding for a different output file name' , async ( ) => {
680+ const plugin = createPlugin ( { } )
681+ const serverBundle = {
682+ 'devup-ui-3.server.css' : {
683+ source : 'stale' ,
684+ name : 'devup-ui-3.css' ,
685+ } ,
686+ 'entry.js' : {
687+ name : 'entry' ,
688+ viteMetadata : {
689+ importedCss : new Set ( [ 'devup-ui-3.server.css' ] ) ,
690+ } ,
691+ } ,
692+ }
693+ await plugin . generateBundle . call (
694+ { environment : { name : 'rsc' , config : { consumer : 'server' } } } ,
695+ { } ,
696+ serverBundle ,
697+ )
698+ const clientBundle = {
699+ 'devup-ui-3.client.css' : {
700+ source : 'stale' ,
701+ name : 'devup-ui-3.css' ,
702+ } ,
703+ }
704+
705+ await plugin . generateBundle . call (
706+ { environment : { name : 'client' , config : { consumer : 'client' } } } ,
707+ { } ,
708+ clientBundle ,
709+ )
710+
711+ expect ( serverBundle [ 'entry.js' ] . viteMetadata . importedCss ) . toEqual (
712+ new Set ( [ 'devup-ui-3.server.css' ] ) ,
713+ )
714+ } )
715+
575716 it ( 'resolves a stable id during build' , async ( ) => {
576717 const plugin = createPlugin ( { } )
577718 await plugin . configResolved ( { command : 'build' } )
0 commit comments