-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite-plugin-lit-shell.ts
44 lines (39 loc) · 1.13 KB
/
vite-plugin-lit-shell.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
const fileRegex = /\.s?css$/
import { render } from '@lit-labs/ssr'
import { collectResult } from '@lit-labs/ssr/lib/render-result'
import { decode } from 'html-entities'
import './src/lib/pg-button/pg-button'
import main from './src/main'
import convert from './convert-scss'
import type { Plugin } from 'vite'
export function litConvertScss(): Plugin {
return {
name: 'html-lit-convert-scss',
config() {
convert()
},
handleHotUpdate({ file, modules }) {
const needsUpdate = !!file.match(fileRegex)
if (needsUpdate) {
convert()
}
},
}
}
export function litShell() {
return {
name: 'html-lit-shell',
async transformIndexHtml(html) {
// return html // disable transformation and injection of main.ts
// match for body opening and closing tags
const bodyRegex = /<body[^>]*>([\s\S]*)<\/body>/
const res = render(main)
// insert buttons into body
let outHtml = decode(await collectResult(res))
// replace body with buttons
outHtml = html.replace(bodyRegex, `<body>${outHtml}</body>`)
return outHtml
},
order: 'post',
}
}