This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy pathvite-node.mjs
More file actions
47 lines (40 loc) · 1.32 KB
/
vite-node.mjs
File metadata and controls
47 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { performance } from 'node:perf_hooks'
import { ViteNodeRunner } from 'vite-node/client'
import { $fetch } from 'ohmyfetch'
import consola from 'consola'
import { getViteNodeOptions } from './vite-node-shared.mjs'
const viteNodeOptions = getViteNodeOptions()
const runner = new ViteNodeRunner({
root: viteNodeOptions.root,
base: viteNodeOptions.base,
async fetchModule (id) {
return await $fetch('/module/' + encodeURI(id), {
baseURL: viteNodeOptions.baseURL
})
}
})
let render
export default async (ssrContext) => {
// Workaround for stub mode
// https://github.com/nuxt/framework/pull/3983
process.server = true
// Invalidate cache for files changed since last rendering
const invalidates = await $fetch('/invalidates', {
baseURL: viteNodeOptions.baseURL
})
const updates = new Set()
for (const key of invalidates) {
if (runner.moduleCache.delete(key)) {
updates.add(key)
}
}
// Execute SSR bundle on demand
const start = performance.now()
render = render || (await runner.executeFile(viteNodeOptions.entryPath)).default
if (updates.size) {
const time = Math.round((performance.now() - start) * 1000) / 1000
consola.success(`Vite server hmr ${updates.size} files`, time ? `in ${time}ms` : '')
}
const result = await render(ssrContext)
return result
}