-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create vite runtime error overlay plugin (#100)
- Loading branch information
Showing
9 changed files
with
206 additions
and
0 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
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,22 @@ | ||
# vite-runtime-error-overlay | ||
|
||
Vite plugin to show client runtime error via builtin error overlay. | ||
Based on the idea from https://github.com/vitejs/vite/pull/6274#issuecomment-1087749460 | ||
|
||
## usage | ||
|
||
```ts | ||
import { defineConfig } from "vite"; | ||
import { viteRuntimeErrorOverlayPlugin } from "@hiogawa/vite-runtime-error-overlay"; | ||
|
||
export default defineConfig({ | ||
plugins: [viteRuntimeErrorOverlayPlugin()], | ||
}); | ||
``` | ||
|
||
## development | ||
|
||
```sh | ||
pnpm build | ||
pnpm release | ||
``` |
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,35 @@ | ||
{ | ||
"name": "@hiogawa/vite-runtime-error-overlay", | ||
"version": "0.0.1", | ||
"homepage": "https://github.com/hi-ogawa/unocss-preset-antd/tree/main/packages/vite-runtime-error-overlay", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/hi-ogawa/unocss-preset-antd/", | ||
"directory": "packages/vite-runtime-error-overlay" | ||
}, | ||
"license": "MIT", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "tsup", | ||
"release": "pnpm publish --no-git-checks --access public" | ||
}, | ||
"devDependencies": { | ||
"vite": "^4.4.9" | ||
}, | ||
"peerDependencies": { | ||
"vite": "*" | ||
} | ||
} |
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,84 @@ | ||
import { type Plugin, type WebSocketClient } from "vite"; | ||
import { name as packageName } from "../package.json"; | ||
|
||
// based on the idea in | ||
// https://github.com/vitejs/vite/pull/6274#issuecomment-1087749460 | ||
// https://github.com/vitejs/vite/issues/2076 | ||
|
||
// TODO: the PR has utility to construct "frame" | ||
// getStackLineInformation | ||
// generateErrorPayload | ||
// generateFrame | ||
|
||
export function viteRuntimeErrorOverlayPlugin(options?: { | ||
filter?: (error: Error) => boolean; | ||
}): Plugin { | ||
return { | ||
name: packageName, | ||
|
||
apply(config, env) { | ||
return env.command === "serve" && !config.ssr; | ||
}, | ||
|
||
transformIndexHtml() { | ||
return [ | ||
{ | ||
tag: "script", | ||
attrs: { type: "module" }, | ||
children: CLIENT_SCRIPT, | ||
}, | ||
]; | ||
}, | ||
|
||
configureServer(server) { | ||
server.ws.on(MESSAGE_TYPE, (data: unknown, client: WebSocketClient) => { | ||
// deserialize error | ||
const error = Object.assign(new Error(), data); | ||
|
||
if (options?.filter && !options.filter(error)) { | ||
return; | ||
} | ||
|
||
// https://vitejs.dev/guide/api-plugin.html#client-server-communication | ||
// https://github.com/vitejs/vite/blob/5b58eca05939c0667cf9698e83f4f4849f3296f4/packages/vite/src/node/server/middlewares/error.ts#L54-L57 | ||
client.send({ | ||
type: "error", | ||
err: { | ||
message: error.message, | ||
stack: error.stack ?? "", | ||
}, | ||
}); | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
const MESSAGE_TYPE = `${packageName}:error`; | ||
|
||
const CLIENT_SCRIPT = /* js */ ` | ||
import { createHotContext } from "/@vite/client"; | ||
// dummy file path to instantiate import.meta.hot | ||
const hot = createHotContext("/__dummy__${packageName}"); | ||
function sendError(error) { | ||
if (!(error instanceof Error)) { | ||
error = new Error("(unknown runtime error)"); | ||
} | ||
const serialized = { | ||
message: error.message, | ||
stack: error.stack, | ||
}; | ||
hot.send("${MESSAGE_TYPE}", serialized); | ||
} | ||
window.addEventListener("error", (evt) => { | ||
sendError(evt.error); | ||
}); | ||
window.addEventListener("unhandledrejection", (evt) => { | ||
sendError(evt.reason); | ||
}); | ||
`; |
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,4 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"include": ["src"] | ||
} |
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,7 @@ | ||
import { defineConfig } from "tsup"; | ||
|
||
export default defineConfig({ | ||
entry: ["src/index.ts"], | ||
format: ["esm", "cjs"], | ||
dts: true, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.