Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/vite/src/runtime/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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 = {
Expand All @@ -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
}
18 changes: 9 additions & 9 deletions packages/vite/src/vite-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are not depending on template anymore, can we avoid copy and directly use runtime/server.mjs as entrypoint?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to, but how could we do that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm actually we can update via nitro options but seems little hacky with legacy support of webpack.

What about generating something minimal like this inside entry:

export { default } from `path/runtime/server.mjs`

Suggestion: would be nice to also rename server.mjs to vite-node.mjs for clarity.

Copy link
Copy Markdown
Member Author

@antfu antfu Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh since it will write client.manifest.mjs, maybe it's better to do that after #3969? Otherwise, it writes the file to the runtime folder

Did it in 0ee2d81 (#3968)

resolve(distDir, 'runtime/server.mjs'),
resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs')
)
}

Expand Down