@@ -48,13 +48,43 @@ const fileEndingRegex = /(ts|js)x(\?.*)?$/;
4848
4949const lazy = ( ) : PluginOption => {
5050 const cwd = process . cwd ( ) . replaceAll ( osSep , sep ) ;
51+
52+ /**
53+ * Maps module ids to their client-specific shared chunk names.
54+ * Modules in shared chunks need to find their assets via the chunk name, instead of their module id.
55+ *
56+ * Vite includes assets of such modules in the manifest via the chunk name:
57+ * https://github.com/vitejs/vite/blob/4be37a8389c67873880f826b01fe40137e1c29a7/packages/vite/src/node/plugins/manifest.ts#L179
58+ * https://github.com/vitejs/vite/blob/4be37a8389c67873880f826b01fe40137e1c29a7/packages/vite/src/node/plugins/manifest.ts#L319
59+ *
60+ * Rollup occassionally creates shared chunks automatically,
61+ * but they can also be manually created by the user via:
62+ * https://rollupjs.org/configuration-options/#output-manualchunks
63+ *
64+ * More infos on Rollup's logic:
65+ * https://github.com/rollup/rollup/issues/3772#issuecomment-689955168
66+ */
67+ const sharedChunkNames : Record < string , string > = { } ;
68+
5169 return {
5270 name : "solid-lazy-css" ,
5371 enforce : "pre" ,
54- applyToEnvironment ( env ) {
55- return env . name === VITE_ENVIRONMENTS . server ;
72+ generateBundle ( _ , bundle ) {
73+ if ( this . environment . name !== VITE_ENVIRONMENTS . client ) return ;
74+
75+ for ( const chunk of Object . values ( bundle ) ) {
76+ if ( chunk . type !== "chunk" || ! chunk . isDynamicEntry || chunk . facadeModuleId ) continue ;
77+
78+ // Has to follow Vites implementation:
79+ // https://github.com/vitejs/vite/blob/4be37a8389c67873880f826b01fe40137e1c29a7/packages/vite/src/node/plugins/manifest.ts#L179
80+ const chunkName = `_${ basename ( chunk . fileName ) } ` ;
81+ for ( const id of chunk . moduleIds ) {
82+ sharedChunkNames [ id ] = chunkName ;
83+ }
84+ }
5685 } ,
5786 async transform ( src , id ) {
87+ if ( this . environment . name !== VITE_ENVIRONMENTS . server ) return ;
5888 if ( ! id . match ( fileEndingRegex ) ) return ;
5989
6090 // The transformed files either import "lazy" or css files
@@ -66,7 +96,8 @@ const lazy = (): PluginOption => {
6696 const hasDefaultExport = src . indexOf ( "export default" ) !== - 1 ;
6797 if ( hasDefaultExport ) {
6898 const localId = relative ( cwd , id ) ;
69- plugins . push ( idTransform ( localId ) ) ;
99+ const chunkName = sharedChunkNames [ id ] ;
100+ plugins . push ( idTransform ( chunkName ?? localId ) ) ;
70101 }
71102
72103 const hasLazy = src . indexOf ( "lazy(" ) !== - 1 ;
0 commit comments