fix(vite-node): include importer in error stack (#7607)

This commit is contained in:
pooya parsa 2022-09-19 11:28:38 +02:00 committed by GitHub
parent c4fe8525ef
commit 57a8a8694a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,12 +29,15 @@ export default async (ssrContext) => {
} }
function createRunner () { function createRunner () {
const _importers = new Map()
return new ViteNodeRunner({ return new ViteNodeRunner({
root: viteNodeOptions.root, // Equals to Nuxt `srcDir` root: viteNodeOptions.root, // Equals to Nuxt `srcDir`
base: viteNodeOptions.base, base: viteNodeOptions.base,
resolveId (id, importer) { _importers.set(id, importer) },
async fetchModule (id) { async fetchModule (id) {
// TODO: fix in vite-node const importer = _importers.get(id)
id = id.replace(/\/\//g, '/') _importers.delete(id)
id = id.replace(/\/\//g, '/') // TODO: fix in vite-node
return await viteNodeFetch('/module/' + encodeURI(id)).catch((err) => { return await viteNodeFetch('/module/' + encodeURI(id)).catch((err) => {
const errorData = err?.data?.data const errorData = err?.data?.data
if (!errorData) { if (!errorData) {
@ -42,7 +45,7 @@ function createRunner () {
} }
let _err let _err
try { try {
const { message, stack } = formatViteError(errorData) const { message, stack } = formatViteError(errorData, id, importer)
_err = createError({ _err = createError({
statusMessage: 'Vite Error', statusMessage: 'Vite Error',
message, message,
@ -64,11 +67,11 @@ function createRunner () {
}) })
} }
function formatViteError (errorData) { function formatViteError (errorData, id, importer) {
const errorCode = errorData.name || errorData.reasonCode || errorData.code const errorCode = errorData.name || errorData.reasonCode || errorData.code
const frame = errorData.frame || errorData.source || errorData.pluginCode const frame = errorData.frame || errorData.source || errorData.pluginCode
const getLocId = (locObj = {}) => locObj.file || locObj.id || locObj.url || '' const getLocId = (locObj = {}) => locObj.file || locObj.id || locObj.url || id || ''
const getLocPos = (locObj = {}) => locObj.line ? `${locObj.line}:${locObj.column || 0}` : '' const getLocPos = (locObj = {}) => locObj.line ? `${locObj.line}:${locObj.column || 0}` : ''
const locId = getLocId(errorData.loc) || getLocId(errorData.location) || getLocId(errorData.input) || getLocId(errorData) 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 locPos = getLocPos(errorData.loc) || getLocPos(errorData.location) || getLocPos(errorData.input) || getLocPos(errorData)
@ -85,7 +88,7 @@ function formatViteError (errorData) {
const stack = [ const stack = [
message, message,
'at ' + loc, `at ${loc} ${importer ? `(imported from ${importer})` : ''}`,
errorData.stack errorData.stack
].filter(Boolean).join('\n') ].filter(Boolean).join('\n')