fix(vite): add transformation result to log for parse errors (#28508)

This commit is contained in:
Julien Huang 2024-08-12 23:07:53 +02:00 committed by GitHub
parent c4cad445ea
commit fefe4e5c8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -126,7 +126,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
app.use('/module', defineLazyEventHandler(() => { app.use('/module', defineLazyEventHandler(() => {
const viteServer = ctx.ssrServer! const viteServer = ctx.ssrServer!
const node: ViteNodeServer = new ViteNodeServer(viteServer, { const node = new ViteNodeServer(viteServer, {
deps: { deps: {
inline: [ inline: [
/\/node_modules\/(.*\/)?(nuxt|nuxt3|nuxt-nightly)\//, /\/node_modules\/(.*\/)?(nuxt|nuxt3|nuxt-nightly)\//,
@ -139,6 +139,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
web: [], web: [],
}, },
}) })
const isExternal = createIsExternal(viteServer, ctx.nuxt.options.rootDir, ctx.nuxt.options.modulesDir) const isExternal = createIsExternal(viteServer, ctx.nuxt.options.rootDir, ctx.nuxt.options.modulesDir)
node.shouldExternalize = async (id: string) => { node.shouldExternalize = async (id: string) => {
const result = await isExternal(id) const result = await isExternal(id)
@ -156,13 +157,17 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set<string> = ne
if (isAbsolute(moduleId) && !isFileServingAllowed(moduleId, viteServer)) { if (isAbsolute(moduleId) && !isFileServingAllowed(moduleId, viteServer)) {
throw createError({ statusCode: 403 /* Restricted */ }) throw createError({ statusCode: 403 /* Restricted */ })
} }
const module = await node.fetchModule(moduleId).catch((err) => { const module = await node.fetchModule(moduleId).catch(async (err) => {
const errorData = { const errorData = {
code: 'VITE_ERROR', code: 'VITE_ERROR',
id: moduleId, id: moduleId,
stack: '', stack: '',
...err, ...err,
} }
if (!errorData.frame && errorData.code === 'PARSE_ERROR') {
errorData.frame = await node.transformModule(moduleId, 'web').then(({ code }) => `${err.message || ''}\n${code}`).catch(() => undefined)
}
throw createError({ data: errorData }) throw createError({ data: errorData })
}) })
return module return module