mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 15:50:32 +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 { 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
|
||||||
}
|
}
|
||||||
|
@ -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')
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user