mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +00:00
fix(vite-node): improve server.mjs (#3967)
This commit is contained in:
parent
9181734912
commit
43007c6d13
@ -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
|
||||
}
|
||||
|
@ -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')
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user