Skip to content

Commit

Permalink
feat(pluginContainer): implement watchChange hook (#14822)
Browse files Browse the repository at this point in the history
Co-authored-by: patak <matias.capeletto@gmail.com>
  • Loading branch information
developit and patak-dev committed Nov 1, 2023
1 parent 275907b commit 9369d8d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,14 @@ export async function _createServer(

const onFileAddUnlink = async (file: string, isUnlink: boolean) => {
file = normalizePath(file)
await container.watchChange(file, { event: isUnlink ? 'delete' : 'create' })
await handleFileAddUnlink(file, server, isUnlink)
await onHMRUpdate(file, true)
}

watcher.on('change', async (file) => {
file = normalizePath(file)
await container.watchChange(file, { event: 'update' })
// invalidate module graph cache on file change
moduleGraph.onFileChange(file)

Expand Down
13 changes: 13 additions & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export interface PluginContainer {
ssr?: boolean
},
): Promise<LoadResult | null>
watchChange(
id: string,
change: { event: 'create' | 'update' | 'delete' },
): Promise<void>
close(): Promise<void>
}

Expand Down Expand Up @@ -778,6 +782,15 @@ export async function createPluginContainer(
}
},

async watchChange(id, change) {
const ctx = new Context()
await hookParallel(
'watchChange',
() => ctx,
() => [id, change],
)
},

async close() {
if (closed) return
closed = true
Expand Down

0 comments on commit 9369d8d

Please sign in to comment.