diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index db6768a8c6..8001c389cd 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -126,7 +126,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set = ne app.use('/module', defineLazyEventHandler(() => { const viteServer = ctx.ssrServer! - const node: ViteNodeServer = new ViteNodeServer(viteServer, { + const node = new ViteNodeServer(viteServer, { deps: { inline: [ /\/node_modules\/(.*\/)?(nuxt|nuxt3|nuxt-nightly)\//, @@ -139,6 +139,7 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set = ne web: [], }, }) + const isExternal = createIsExternal(viteServer, ctx.nuxt.options.rootDir, ctx.nuxt.options.modulesDir) node.shouldExternalize = async (id: string) => { const result = await isExternal(id) @@ -156,13 +157,17 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set = ne if (isAbsolute(moduleId) && !isFileServingAllowed(moduleId, viteServer)) { throw createError({ statusCode: 403 /* Restricted */ }) } - const module = await node.fetchModule(moduleId).catch((err) => { + const module = await node.fetchModule(moduleId).catch(async (err) => { const errorData = { code: 'VITE_ERROR', id: moduleId, stack: '', ...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 }) }) return module