@@ -27,6 +27,15 @@ export default function hikkaku(init: HikkakuViteInit): PluginOption {
2727 let additionalAssets = new Map < string , Uint8Array > ( )
2828 const assetCache = new Map < string , Uint8Array | false > ( )
2929
30+ const loadProjectFromRunner = async ( ) : Promise < Project > => {
31+ if ( ! runner ) {
32+ throw new Error ( 'Module runner is not initialized.' )
33+ }
34+ const project : Project = ( await runner . import ( init . entry ) ) . default
35+ additionalAssets = project . getAdditionalAssets ( )
36+ return project
37+ }
38+
3039 // Helper function to set Content-Type based on file extension
3140 const setContentType = ( res : ServerResponse , assetId : string ) => {
3241 const assetExt = path . extname ( assetId ) . toLowerCase ( )
@@ -175,10 +184,7 @@ export default function hikkaku(init: HikkakuViteInit): PluginOption {
175184 if ( id === VIRTUAL_MODULE_IDS . project ) {
176185 if ( this . environment . mode === 'dev' ) {
177186 // in dev mode, use entry file
178- if ( ! runner ) {
179- throw new Error ( 'Module runner is not initialized.' )
180- }
181- const project : Project = ( await runner . import ( init . entry ) ) . default
187+ const project = await loadProjectFromRunner ( )
182188
183189 return `
184190 export default ${ JSON . stringify ( project . toScratch ( ) ) }
@@ -204,11 +210,7 @@ export default function hikkaku(init: HikkakuViteInit): PluginOption {
204210 } ,
205211 async hotUpdate ( options ) {
206212 if ( this . environment . name !== 'hikkaku' ) return
207- if ( ! runner ) {
208- throw new Error ( 'Module runner is not initialized.' )
209- }
210- const project : Project = ( await runner . import ( init . entry ) ) . default
211- additionalAssets = project . getAdditionalAssets ( )
213+ const project = await loadProjectFromRunner ( )
212214 options . server . environments . client . hot . send (
213215 'hikkaku:project' ,
214216 project . toScratch ( ) ,
@@ -223,11 +225,7 @@ export default function hikkaku(init: HikkakuViteInit): PluginOption {
223225 //server.watcher.add(init.entry)
224226 runner = createServerModuleRunner ( hikkakuEnv )
225227 server . environments . client . hot . on ( 'vite:client:connect' , async ( ) => {
226- if ( ! runner ) {
227- throw new Error ( 'Module runner is not initialized.' )
228- }
229- const project : Project = ( await runner . import ( init . entry ) ) . default
230- additionalAssets = project . getAdditionalAssets ( )
228+ const project = await loadProjectFromRunner ( )
231229 server . environments . client . hot . send (
232230 'hikkaku:project' ,
233231 project . toScratch ( ) ,
@@ -244,7 +242,11 @@ export default function hikkaku(init: HikkakuViteInit): PluginOption {
244242 res . end ( 'Asset ID is required' )
245243 return
246244 }
247- const assetData = additionalAssets . get ( assetId )
245+ let assetData = additionalAssets . get ( assetId )
246+ if ( ! assetData && runner ) {
247+ await loadProjectFromRunner ( )
248+ assetData = additionalAssets . get ( assetId )
249+ }
248250 if ( ! assetData ) {
249251 // fallback to network fetch
250252 if ( assetCache . has ( assetId ) ) {
0 commit comments