Skip to content

Commit

Permalink
fix(vite-node): include importer in error stack (nuxt#7607)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Sep 19, 2022
1 parent c4fe852 commit 57a8a86
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/vite/src/runtime/vite-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ export default async (ssrContext) => {
}

function createRunner () {
const _importers = new Map()
return new ViteNodeRunner({
root: viteNodeOptions.root, // Equals to Nuxt `srcDir`
base: viteNodeOptions.base,
resolveId (id, importer) { _importers.set(id, importer) },
async fetchModule (id) {
// TODO: fix in vite-node
id = id.replace(/\/\//g, '/')
const importer = _importers.get(id)
_importers.delete(id)
id = id.replace(/\/\//g, '/') // TODO: fix in vite-node
return await viteNodeFetch('/module/' + encodeURI(id)).catch((err) => {
const errorData = err?.data?.data
if (!errorData) {
throw err
}
let _err
try {
const { message, stack } = formatViteError(errorData)
const { message, stack } = formatViteError(errorData, id, importer)
_err = createError({
statusMessage: 'Vite Error',
message,
Expand All @@ -64,11 +67,11 @@ function createRunner () {
})
}

function formatViteError (errorData) {
function formatViteError (errorData, id, importer) {
const errorCode = errorData.name || errorData.reasonCode || errorData.code
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 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)
Expand All @@ -85,7 +88,7 @@ function formatViteError (errorData) {

const stack = [
message,
'at ' + loc,
`at ${loc} ${importer ? `(imported from ${importer})` : ''}`,
errorData.stack
].filter(Boolean).join('\n')

Expand Down

0 comments on commit 57a8a86

Please sign in to comment.