-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.ts
60 lines (54 loc) · 1.85 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { Plugin } from "vite"
import { compileSFCTemplate } from "./compiler"
import { parseVueRequest, normalizeOverlayScripts, getVueDepsBrowserHash } from "./utils"
import { v2, v3 } from "./overlay/index.json"
import { queryParserMiddleware, launchEditorMiddleware } from "./middleware"
export type VitePluginInspectorOptions = {
vue?: number
enabled?: boolean
}
function VitePluginInspector(options: VitePluginInspectorOptions = { vue: 3, enabled: true }): Plugin {
const { scripts, styles, overlayContainerScript } = options.vue === 2 ? v2 : v3
return {
name: "vite-plugin-vue-inspector",
enforce: "pre",
apply: "serve",
transform(code, id) {
const { filename, query } = parseVueRequest(id)
const isJsx = filename.endsWith(".jsx") || filename.endsWith(".tsx") || (filename.endsWith(".vue") && query.isJsx)
const isTpl = filename.endsWith(".vue") && query.type !== "style"
if (isJsx || isTpl)
return compileSFCTemplate({ code, id: filename, type: isJsx ? "jsx" : "template" })
return code
},
configureServer(server) {
server.middlewares.use(queryParserMiddleware)
server.middlewares.use(launchEditorMiddleware)
},
transformIndexHtml(html, { server }) {
return {
html,
tags: [{
tag: "script",
children: `window.__VUE_INSPECTOR_INITIAL_ENABLED__=${options.enabled ?? true};${overlayContainerScript}`,
injectTo: "body",
}, {
tag: "script",
attrs: {
type: "module",
},
children: normalizeOverlayScripts({
hash: getVueDepsBrowserHash(server),
scripts,
}),
injectTo: "body",
}, {
tag: "style",
children: styles,
injectTo: "head",
}],
}
},
}
}
export default VitePluginInspector