|
1 | 1 | import { EventEmitter } from 'node:events'; |
2 | 2 | import path from 'node:path'; |
| 3 | +import { pathToFileURL } from 'node:url'; |
3 | 4 | import type * as vite from 'vite'; |
| 5 | +import { collectErrorMetadata } from '../errors/dev/utils.js'; |
| 6 | +import { getViteErrorPayload } from '../errors/dev/vite.js'; |
4 | 7 | import type { ModuleLoader, ModuleLoaderEventEmitter } from './loader.js'; |
5 | 8 |
|
6 | 9 | export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader { |
@@ -43,6 +46,24 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader { |
43 | 46 | } |
44 | 47 | const msg = args[0] as vite.HMRPayload; |
45 | 48 | if (msg?.type === 'error') { |
| 49 | + // If we have an error, but it didn't go through our error enhancement program, it means that it's a HMR error from |
| 50 | + // vite itself, which goes through a different path. We need to enhance it here. |
| 51 | + if (!(msg as any)['__isEnhancedAstroErrorPayload']) { |
| 52 | + const err = collectErrorMetadata(msg.err, pathToFileURL(viteServer.config.root)); |
| 53 | + getViteErrorPayload(err).then((payload) => { |
| 54 | + events.emit('hmr-error', { |
| 55 | + type: 'error', |
| 56 | + err: { |
| 57 | + message: payload.err.message, |
| 58 | + stack: payload.err.stack, |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + args[0] = payload; |
| 63 | + _wsSend.apply(this, args); |
| 64 | + }); |
| 65 | + return; |
| 66 | + } |
46 | 67 | events.emit('hmr-error', msg); |
47 | 68 | } |
48 | 69 | _wsSend.apply(this, args); |
|
0 commit comments