2022-08-04 11:13:12 +00:00
|
|
|
import { performance } from 'node:perf_hooks'
|
2022-09-03 10:02:14 +00:00
|
|
|
import { createError } from 'h3'
|
2022-04-12 10:04:55 +00:00
|
|
|
import { ViteNodeRunner } from 'vite-node/client'
|
2022-08-04 11:13:12 +00:00
|
|
|
import consola from 'consola'
|
2022-09-15 11:01:52 +00:00
|
|
|
import { viteNodeOptions, viteNodeFetch } from './vite-node-shared.mjs'
|
2022-04-12 10:04:55 +00:00
|
|
|
|
2022-09-03 10:02:14 +00:00
|
|
|
const runner = createRunner()
|
2022-04-12 10:04:55 +00:00
|
|
|
let render
|
|
|
|
|
|
|
|
export default async (ssrContext) => {
|
|
|
|
// Workaround for stub mode
|
|
|
|
// https://github.com/nuxt/framework/pull/3983
|
|
|
|
process.server = true
|
2022-08-04 10:03:46 +00:00
|
|
|
|
|
|
|
// Invalidate cache for files changed since last rendering
|
2022-09-15 11:01:52 +00:00
|
|
|
const invalidates = await viteNodeFetch('/invalidates')
|
2022-08-18 08:05:37 +00:00
|
|
|
const updates = runner.moduleCache.invalidateDepTree(invalidates)
|
2022-08-04 10:03:46 +00:00
|
|
|
|
|
|
|
// Execute SSR bundle on demand
|
2022-08-04 11:13:12 +00:00
|
|
|
const start = performance.now()
|
2022-09-08 14:17:22 +00:00
|
|
|
render = (updates.has(viteNodeOptions.entryPath) || !render) ? (await runner.executeFile(viteNodeOptions.entryPath)).default : render
|
2022-08-04 11:13:12 +00:00
|
|
|
if (updates.size) {
|
|
|
|
const time = Math.round((performance.now() - start) * 1000) / 1000
|
|
|
|
consola.success(`Vite server hmr ${updates.size} files`, time ? `in ${time}ms` : '')
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:04:55 +00:00
|
|
|
const result = await render(ssrContext)
|
|
|
|
return result
|
|
|
|
}
|
2022-09-03 10:02:14 +00:00
|
|
|
|
|
|
|
function createRunner () {
|
2022-09-19 09:28:38 +00:00
|
|
|
const _importers = new Map()
|
2022-09-03 10:02:14 +00:00
|
|
|
return new ViteNodeRunner({
|
|
|
|
root: viteNodeOptions.root, // Equals to Nuxt `srcDir`
|
|
|
|
base: viteNodeOptions.base,
|
2022-09-19 09:28:38 +00:00
|
|
|
resolveId (id, importer) { _importers.set(id, importer) },
|
2022-09-03 10:02:14 +00:00
|
|
|
async fetchModule (id) {
|
2022-09-19 09:28:38 +00:00
|
|
|
const importer = _importers.get(id)
|
|
|
|
_importers.delete(id)
|
|
|
|
id = id.replace(/\/\//g, '/') // TODO: fix in vite-node
|
2022-09-15 11:01:52 +00:00
|
|
|
return await viteNodeFetch('/module/' + encodeURI(id)).catch((err) => {
|
2022-09-03 10:02:14 +00:00
|
|
|
const errorData = err?.data?.data
|
|
|
|
if (!errorData) {
|
|
|
|
throw err
|
|
|
|
}
|
2022-09-14 15:58:28 +00:00
|
|
|
let _err
|
2022-09-03 10:02:14 +00:00
|
|
|
try {
|
2022-09-19 09:28:38 +00:00
|
|
|
const { message, stack } = formatViteError(errorData, id, importer)
|
2022-09-14 15:58:28 +00:00
|
|
|
_err = createError({
|
2022-09-03 10:02:14 +00:00
|
|
|
statusMessage: 'Vite Error',
|
|
|
|
message,
|
|
|
|
stack
|
|
|
|
})
|
2022-09-16 18:12:52 +00:00
|
|
|
} catch (formatError) {
|
|
|
|
consola.warn('Internal nuxt error while formatting vite-node error. Please report this!', formatError)
|
|
|
|
const message = `[vite-node] [TransformError] ${errorData?.message || '-'}`
|
|
|
|
consola.error(message, errorData)
|
2022-09-03 10:02:14 +00:00
|
|
|
throw createError({
|
|
|
|
statusMessage: 'Vite Error',
|
2022-09-16 18:12:52 +00:00
|
|
|
message,
|
|
|
|
stack: `${message}\nat ${id}\n` + (errorData?.stack || '')
|
2022-09-03 10:02:14 +00:00
|
|
|
})
|
|
|
|
}
|
2022-09-14 15:58:28 +00:00
|
|
|
throw _err
|
2022-09-03 10:02:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-19 09:28:38 +00:00
|
|
|
function formatViteError (errorData, id, importer) {
|
2022-09-03 10:02:14 +00:00
|
|
|
const errorCode = errorData.name || errorData.reasonCode || errorData.code
|
|
|
|
const frame = errorData.frame || errorData.source || errorData.pluginCode
|
|
|
|
|
2022-09-19 09:28:38 +00:00
|
|
|
const getLocId = (locObj = {}) => locObj.file || locObj.id || locObj.url || id || ''
|
2022-09-03 10:02:14 +00:00
|
|
|
const getLocPos = (locObj = {}) => locObj.line ? `${locObj.line}:${locObj.column || 0}` : ''
|
|
|
|
const locId = getLocId(errorData.loc) || getLocId(errorData.location) || getLocId(errorData.input) || getLocId(errorData)
|
|
|
|
const locPos = getLocPos(errorData.loc) || getLocPos(errorData.location) || getLocPos(errorData.input) || getLocPos(errorData)
|
|
|
|
const loc = locId.replace(process.cwd(), '.') + (locPos ? `:${locPos}` : '')
|
|
|
|
|
|
|
|
const message = [
|
|
|
|
'[vite-node]',
|
|
|
|
errorData.plugin && `[plugin:${errorData.plugin}]`,
|
|
|
|
errorCode && `[${errorCode}]`,
|
|
|
|
loc,
|
|
|
|
errorData.reason && `: ${errorData.reason}`,
|
|
|
|
frame && `<br><pre>${frame.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')}</pre><br>`
|
|
|
|
].filter(Boolean).join(' ')
|
|
|
|
|
|
|
|
const stack = [
|
|
|
|
message,
|
2022-09-19 09:28:38 +00:00
|
|
|
`at ${loc} ${importer ? `(imported from ${importer})` : ''}`,
|
2022-09-03 10:02:14 +00:00
|
|
|
errorData.stack
|
|
|
|
].filter(Boolean).join('\n')
|
|
|
|
|
|
|
|
return {
|
|
|
|
message,
|
|
|
|
stack
|
|
|
|
}
|
|
|
|
}
|