-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hmr): add syntax output for build errors (#1083)
Co-authored-by: Shigma <[email protected]>
- Loading branch information
1 parent
5d5ba30
commit 2147314
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Logger } from 'koishi' | ||
import { BuildFailure } from 'esbuild' | ||
import { codeFrameColumns } from '@babel/code-frame' | ||
import { readFileSync } from 'fs' | ||
|
||
const logger = new Logger('watch') | ||
|
||
function isBuildFailure(e: any): e is BuildFailure { | ||
return Array.isArray(e?.errors) && e.errors.every((error: any) => error.text) | ||
} | ||
|
||
export function handleError(e: any) { | ||
if (!isBuildFailure(e)) { | ||
logger.warn(e) | ||
return | ||
} | ||
|
||
for (const error of e.errors) { | ||
if (!error.location) { | ||
logger.warn(error.text) | ||
continue | ||
} | ||
try { | ||
const { file, line, column } = error.location | ||
const source = readFileSync(file, 'utf8') | ||
const formatted = codeFrameColumns(source, { | ||
start: { line, column }, | ||
}, { | ||
highlightCode: true, | ||
message: error.text, | ||
}) | ||
logger.warn(`File: ${file}:${line}:${column}\n` + formatted) | ||
} catch (e) { | ||
logger.warn(e) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters