-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
84 lines (65 loc) · 2.25 KB
/
vite.config.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
{
name: 'vite-plugin-index-html',
// A custom middleware to intercept the request to the /home route
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
if (req.url === '/home') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
const shouldResolveToId = 'home.html'
await server.moduleGraph.ensureEntryFromUrl(shouldResolveToId, false, true)
const resource = await server.pluginContainer.load(shouldResolveToId)
if (!resource) {
server.config.logger.error(`Failed to load resource ${shouldResolveToId}`)
res.statusCode = 500
res.write('Failed to load resource')
return res.end()
}
res.write(resource.code)
return res.end()
}
next();
});
},
configResolved(config) {
config.build.rollupOptions.input = {
home: 'home.html'
}
},
resolveId(id) {
if (id === 'home.html') {
return id
}
if (id.endsWith('?proxy')) {
return id
}
return null
},
async load(id) {
if (id === 'home.html') {
const proxyCode = await this.load({
id: 'some-code?proxy'
})
// BUG: Right here, the proxy code is not loaded in the dev mode.
// The `code` property is missing.
return {
code: `<div>Hello World</div><pre>${proxyCode.code}</pre>`,
}
}
if (id.endsWith('?proxy')) {
const code = `
const result = "Hello world"
export default result
`
return {
code: code
}
}
return null
}
}
],
});