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

View File

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