-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.mjs
64 lines (57 loc) · 1.84 KB
/
vite.mjs
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
import colors from 'picocolors'
import fetch from "node-fetch"
import { parseThemeData } from './theme.mjs'
const VITE_CLIENT = '<script type="module" src="/@vite/client"></script>'
async function request (api, root, url) {
const theme = await parseThemeData(root)
const data = { url, theme }
if (theme.context) {
// for debugging context
data.override_context = theme.context
}
const body = JSON.stringify(data)
const resp = await fetch(api, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
})
return resp.json()
}
export const themeDevServer = () => {
/** @type {import('vite').ResolvedConfig} */
let config
return {
name: 'typlog-theme-dev-server',
/** @param {import('vite').ResolvedConfig} resolvedConfig */
configResolved(resolvedConfig) {
config = resolvedConfig
},
/**
* @param {string} html
* @returns {Promise<string>}
*/
async transformIndexHtml(html, ctx) {
if (config.command === "serve") {
const api = config.env.VITE_THEME_DEVELOP_API || "https://api.typlog.com/v4/theme/develop"
const data = await request(api, config.root, ctx.originalUrl)
let message
if (data.status === 200) {
message = colors.green(`"GET ${ctx.originalUrl} HTTP/1.1"`) + ` ${data.status} ${data.duration}ms`
} else {
message = colors.red(`"GET ${ctx.originalUrl} HTTP/1.1"`) + ` ${data.status}`
}
config.logger.info(message, { timestamp: true })
if (data.status === 200) {
// inject /@vite/client
return data.html.replace('<head>', `<head>\n${VITE_CLIENT}`)
} else {
return `<html><head>${VITE_CLIENT}</head><body><pre>${data.status}: ${data.message}</pre></body></html>\n`
}
} else {
return html
}
},
}
}