fix(vite-node): improve server.mjs (#3967)

This commit is contained in:
Anthony Fu 2022-03-30 19:34:23 +08:00 committed by GitHub
parent 9181734912
commit 43007c6d13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 19 deletions

View File

@ -3,12 +3,13 @@ import { fileURLToPath } from 'url'
import { ViteNodeRunner } from 'vite-node/client'
import { dirname, join } from 'pathe'
const entry = '__NUXT_SERVER_ENTRY__'
const url = '__NUXT_SERVER_FETCH_URL__'
const base = '__NUXT_SERVER_BASE__'
const url = process.env.NUXT_VITE_SERVER_FETCH
const entry = process.env.NUXT_VITE_SERVER_ENTRY
const base = process.env.NUXT_VITE_SERVER_BASE
const root = process.env.NUXT_VITE_SERVER_ROOT
const runner = new ViteNodeRunner({
root: process.cwd(),
root,
base,
async fetchModule (id) {
return await $fetch(url, {
@ -23,13 +24,15 @@ function isCSS (file) {
return IS_CSS_RE.test(file)
}
async function writeManifest (extraEntries) {
async function writeManifest () {
const dir = dirname(fileURLToPath(import.meta.url))
const entries = [
'@vite/client',
'entry.mjs',
...extraEntries
...Array.from(runner.moduleCache.keys())
.filter(i => runner.moduleCache.get(i).exports && isCSS(i))
.map(i => i.slice(1))
]
const clientManifest = {
@ -44,11 +47,11 @@ async function writeManifest (extraEntries) {
await fs.writeFile(join(dir, 'client.manifest.mjs'), 'export default ' + JSON.stringify(clientManifest, null, 2), 'utf8')
}
let render
export default async (ssrContext) => {
const { default: render } = await runner.executeFile(entry)
render = render || (await runner.executeFile(entry)).default
const result = await render(ssrContext)
const modules = Array.from(runner.moduleCache.keys())
// Write CSS modules intro manifest to prevent FOUC
await writeManifest(modules.filter(i => isCSS(i)).map(i => i.slice(1)))
await writeManifest()
return result
}

View File

@ -62,18 +62,18 @@ export async function prepareDevServerEntry (ctx: ViteBuildContext) {
entryPath = resolve(ctx.nuxt.options.appDir, 'entry.async')
}
const raw = await fse.readFile(resolve(distDir, 'runtime/server.mjs'), 'utf-8')
const host = ctx.nuxt.options.server.host || 'localhost'
const port = ctx.nuxt.options.server.port || '3000'
const protocol = ctx.nuxt.options.server.https ? 'https' : 'http'
const code = raw
.replace('__NUXT_SERVER_FETCH_URL__', `${protocol}://${host}:${port}/__nuxt_vite_node__/`)
.replace('__NUXT_SERVER_ENTRY__', entryPath)
.replace('__NUXT_SERVER_BASE__', ctx.ssrServer.config.base || '/_nuxt/')
await fse.writeFile(
resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'),
code,
'utf-8'
process.env.NUXT_VITE_SERVER_FETCH = `${protocol}://${host}:${port}/__nuxt_vite_node__/`
process.env.NUXT_VITE_SERVER_ENTRY = entryPath
process.env.NUXT_VITE_SERVER_BASE = ctx.ssrServer.config.base || '/_nuxt/'
process.env.NUXT_VITE_SERVER_ROOT = ctx.nuxt.options.rootDir
await fse.copyFile(
resolve(distDir, 'runtime/server.mjs'),
resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs')
)
}