2022-05-13 11:00:14 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
2022-04-12 10:04:55 +00:00
|
|
|
import { createApp, createError, defineEventHandler, defineLazyEventHandler } from 'h3'
|
2022-03-11 08:41:27 +00:00
|
|
|
import { ViteNodeServer } from 'vite-node/server'
|
|
|
|
import fse from 'fs-extra'
|
|
|
|
import { resolve } from 'pathe'
|
|
|
|
import { addServerMiddleware } from '@nuxt/kit'
|
2022-04-12 10:04:55 +00:00
|
|
|
import type { Plugin as VitePlugin, ViteDevServer } from 'vite'
|
2022-07-26 13:49:17 +00:00
|
|
|
import { ExternalsOptions, isExternal, ExternalsDefaults } from 'externality'
|
|
|
|
import { resolve as resolveModule } from 'mlly'
|
2022-03-11 08:41:27 +00:00
|
|
|
import { distDir } from './dirs'
|
|
|
|
import type { ViteBuildContext } from './vite'
|
2022-04-12 10:04:55 +00:00
|
|
|
import { isCSS } from './utils'
|
2022-03-11 08:41:27 +00:00
|
|
|
|
|
|
|
// TODO: Remove this in favor of registerViteNodeMiddleware
|
|
|
|
// after Nitropack or h3 fixed for adding middlewares after setup
|
|
|
|
export function viteNodePlugin (ctx: ViteBuildContext): VitePlugin {
|
|
|
|
return {
|
|
|
|
name: 'nuxt:vite-node-server',
|
|
|
|
enforce: 'pre',
|
|
|
|
configureServer (server) {
|
|
|
|
server.middlewares.use('/__nuxt_vite_node__', createViteNodeMiddleware(ctx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function registerViteNodeMiddleware (ctx: ViteBuildContext) {
|
|
|
|
addServerMiddleware({
|
|
|
|
route: '/__nuxt_vite_node__/',
|
2022-04-07 11:32:28 +00:00
|
|
|
handler: createViteNodeMiddleware(ctx)
|
2022-03-11 08:41:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
function getManifest (server: ViteDevServer) {
|
|
|
|
const ids = Array.from(server.moduleGraph.urlToModuleMap.keys())
|
|
|
|
.filter(i => isCSS(i))
|
2022-03-11 08:41:27 +00:00
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
const entries = [
|
|
|
|
'@vite/client',
|
|
|
|
'entry.mjs',
|
|
|
|
...ids.map(i => i.slice(1))
|
|
|
|
]
|
|
|
|
|
|
|
|
return {
|
|
|
|
publicPath: '',
|
|
|
|
all: entries,
|
|
|
|
initial: entries,
|
|
|
|
async: [],
|
|
|
|
modules: {}
|
2022-03-11 08:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
function createViteNodeMiddleware (ctx: ViteBuildContext) {
|
|
|
|
const app = createApp()
|
|
|
|
|
2022-06-08 19:49:11 +00:00
|
|
|
app.use('/manifest', defineEventHandler(() => {
|
|
|
|
const manifest = getManifest(ctx.ssrServer)
|
2022-04-12 10:04:55 +00:00
|
|
|
return manifest
|
|
|
|
}))
|
|
|
|
|
|
|
|
app.use('/module', defineLazyEventHandler(() => {
|
2022-07-26 13:49:17 +00:00
|
|
|
const viteServer = ctx.ssrServer
|
|
|
|
const node: ViteNodeServer = new ViteNodeServer(viteServer, {
|
2022-04-12 10:04:55 +00:00
|
|
|
deps: {
|
|
|
|
inline: [
|
2022-04-20 08:52:39 +00:00
|
|
|
/\/(nuxt|nuxt3)\//,
|
2022-04-12 10:04:55 +00:00
|
|
|
/^#/,
|
|
|
|
...ctx.nuxt.options.build.transpile as string[]
|
|
|
|
]
|
2022-07-14 14:00:29 +00:00
|
|
|
},
|
|
|
|
transformMode: {
|
|
|
|
ssr: [/.*/],
|
|
|
|
web: []
|
2022-04-12 10:04:55 +00:00
|
|
|
}
|
|
|
|
})
|
2022-07-26 13:49:17 +00:00
|
|
|
const externalOpts: ExternalsOptions = {
|
|
|
|
inline: [
|
|
|
|
/virtual:/,
|
|
|
|
/\.ts$/,
|
|
|
|
...ExternalsDefaults.inline,
|
|
|
|
...viteServer.config.ssr.noExternal as string[]
|
|
|
|
],
|
|
|
|
external: [
|
|
|
|
...viteServer.config.ssr.external,
|
|
|
|
/node_modules/
|
|
|
|
],
|
|
|
|
resolve: {
|
|
|
|
type: 'module',
|
|
|
|
extensions: ['.ts', '.js', '.json', '.vue', '.mjs', '.jsx', '.tsx', '.wasm']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const rootDir = ctx.nuxt.options.rootDir
|
|
|
|
node.shouldExternalize = async (id: string) => {
|
|
|
|
const result = await isExternal(id, rootDir, externalOpts)
|
|
|
|
if (result?.external) {
|
|
|
|
return resolveModule(result.id, { url: rootDir })
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
return async (event) => {
|
|
|
|
const moduleId = decodeURI(event.req.url).substring(1)
|
|
|
|
if (moduleId === '/') {
|
|
|
|
throw createError({ statusCode: 400 })
|
|
|
|
}
|
|
|
|
const module = await node.fetchModule(moduleId) as any
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
return app.nodeHandler
|
|
|
|
}
|
|
|
|
|
2022-03-11 08:41:27 +00:00
|
|
|
export async function prepareDevServerEntry (ctx: ViteBuildContext) {
|
2022-03-24 12:35:09 +00:00
|
|
|
let entryPath = resolve(ctx.nuxt.options.appDir, 'entry.async.mjs')
|
|
|
|
if (!fse.existsSync(entryPath)) {
|
|
|
|
entryPath = resolve(ctx.nuxt.options.appDir, 'entry.async')
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
// TODO: Update me
|
2022-03-11 08:41:27 +00:00
|
|
|
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'
|
2022-03-30 11:34:23 +00:00
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
// Serialize and pass vite-node runtime options
|
|
|
|
const viteNodeServerOptions = {
|
|
|
|
baseURL: `${protocol}://${host}:${port}/__nuxt_vite_node__`,
|
|
|
|
rootDir: ctx.nuxt.options.rootDir,
|
|
|
|
entryPath,
|
|
|
|
base: ctx.ssrServer.config.base || '/_nuxt/'
|
|
|
|
}
|
|
|
|
process.env.NUXT_VITE_NODE_OPTIONS = JSON.stringify(viteNodeServerOptions)
|
2022-03-30 11:34:23 +00:00
|
|
|
|
2022-05-13 11:00:14 +00:00
|
|
|
const serverResolvedPath = resolve(distDir, 'runtime/vite-node.mjs')
|
|
|
|
const manifestResolvedPath = resolve(distDir, 'runtime/client.manifest.mjs')
|
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
await fse.writeFile(
|
|
|
|
resolve(ctx.nuxt.options.buildDir, 'dist/server/server.mjs'),
|
2022-05-13 11:00:14 +00:00
|
|
|
`export { default } from ${JSON.stringify(pathToFileURL(serverResolvedPath).href)}`
|
2022-04-12 10:04:55 +00:00
|
|
|
)
|
|
|
|
await fse.writeFile(
|
|
|
|
resolve(ctx.nuxt.options.buildDir, 'dist/server/client.manifest.mjs'),
|
2022-05-13 11:00:14 +00:00
|
|
|
`export { default } from ${JSON.stringify(pathToFileURL(manifestResolvedPath).href)}`
|
2022-03-11 08:41:27 +00:00
|
|
|
)
|
|
|
|
}
|