@ -58,15 +58,46 @@ export const staticDataPlugin: Plugin = {
load : {
load : {
filter : { id : loaderMatch } ,
filter : { id : loaderMatch } ,
async handler ( id ) {
handler ( id ) {
let _resolve : ( ( res : any ) = > void ) | undefined
if ( isBuild ) return ( idToPendingPromiseMap [ id ] ? ? = loadData ( id ) )
if ( isBuild ) {
return loadData ( id )
if ( idToPendingPromiseMap [ id ] ) return idToPendingPromiseMap [ id ]
}
idToPendingPromiseMap [ id ] = new Promise ( ( r ) = > {
} ,
_resolve = r
} )
hotUpdate ( { file , module s : existingMods } ) {
if ( this . environment . name !== 'client' ) return
const module s : EnvironmentModuleNode [ ] = [ ]
const normalizedFile = normalizePath ( file )
// Trigger update if a dependency (including transitive ones) changed.
if ( normalizedFile in depToLoaderModuleIdsMap ) {
for ( const id of Array . from (
depToLoaderModuleIdsMap [ normalizedFile ] || [ ]
) ) {
delete idToLoaderModulesMap [ id ]
const mod = this . environment . module Graph.getModuleById ( id )
if ( mod ) module s.push ( mod )
}
}
// Also check if the file matches any custom watch patterns.
for ( const id in idToLoaderModulesMap ) {
const loader = idToLoaderModulesMap [ id ]
if (
loader ? . watch ? . length &&
pm ( loader . watch , loader . options . globOptions ) ( normalizedFile )
) {
const mod = this . environment . module Graph.getModuleById ( id )
if ( mod ) module s.push ( mod )
}
}
return module s.length ? [ . . . existingMods , . . . module s ] : undefined
}
}
}
async function loadData ( id : string ) : Promise < string > {
const base = path . dirname ( id )
const base = path . dirname ( id )
let watch : LoaderModule [ 'watch' ]
let watch : LoaderModule [ 'watch' ]
let load : LoaderModule [ 'load' ]
let load : LoaderModule [ 'load' ]
@ -107,42 +138,5 @@ export const staticDataPlugin: Plugin = {
// record loader module for HMR
// record loader module for HMR
if ( server ) idToLoaderModulesMap [ id ] = { watch , load , options }
if ( server ) idToLoaderModulesMap [ id ] = { watch , load , options }
const result = ` export const data = JSON.parse( ${ JSON . stringify ( JSON . stringify ( data ) ) } ) `
return ` export const data = JSON.parse( ${ JSON . stringify ( JSON . stringify ( data ) ) } ) `
if ( _resolve ) _resolve ( result )
return result
}
} ,
hotUpdate ( { file , module s : existingMods } ) {
if ( this . environment . name !== 'client' ) return
const module s : EnvironmentModuleNode [ ] = [ ]
const normalizedFile = normalizePath ( file )
// Trigger update if a dependency (including transitive ones) changed.
if ( normalizedFile in depToLoaderModuleIdsMap ) {
for ( const id of Array . from (
depToLoaderModuleIdsMap [ normalizedFile ] || [ ]
) ) {
delete idToLoaderModulesMap [ id ]
const mod = this . environment . module Graph.getModuleById ( id )
if ( mod ) module s.push ( mod )
}
}
// Also check if the file matches any custom watch patterns.
for ( const id in idToLoaderModulesMap ) {
const loader = idToLoaderModulesMap [ id ]
if (
loader ? . watch ? . length &&
pm ( loader . watch , loader . options . globOptions ) ( normalizedFile )
) {
const mod = this . environment . module Graph.getModuleById ( id )
if ( mod ) module s.push ( mod )
}
}
return module s.length ? [ . . . existingMods , . . . module s ] : undefined
}
}
}