-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (64 loc) · 1.97 KB
/
index.js
File metadata and controls
71 lines (64 loc) · 1.97 KB
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
import { getPackageInfo, deepMergeWith } from 'vituum/utils/common.js'
import send from './send.js'
import process from 'node:process'
const { name } = getPackageInfo(import.meta.url)
/**
* @type {import('@vituum/vite-plugin-send/types').PluginUserConfig}
*/
export const defaultOptions = {
content: null,
filename: process.env.VITUUM_SEND_FILE || null,
from: process.env.VITUUM_SEND_FROM || 'example@example.com',
to: process.env.VITUUM_SEND_TO || null,
host: process.env.VITUUM_SEND_HOST || null,
user: process.env.VITUUM_SEND_USER || null,
pass: process.env.VITUUM_SEND_PASS || null,
insertScriptBefore: '</head>',
}
/**
* @param {import('@vituum/vite-plugin-send/types').PluginUserConfig} options
* @returns {import('vite').Plugin}
*/
const plugin = (options = {}) => {
options = deepMergeWith(defaultOptions, options)
return {
name,
enforce: 'pre',
configureServer(server) {
server.ws.on('my:send', async ({ content }) => {
await send({
content,
from: options.from,
to: options.to,
host: options.host,
user: options.user,
pass: options.pass,
})
})
},
transformIndexHtml: {
order: 'pre',
async handler(content, { server }) {
if (!server) {
return content
}
const html = `
<script type="module">
if (import.meta.hot && window.location.search === '?send') {
import.meta.hot.send('my:send', {
filename: window.location.href,
content:
new XMLSerializer().serializeToString(document.doctype) +
document.documentElement.outerHTML.replace(/<script\\b[^>]*>[\\s\\S]*?<\\/script>/gi, "")
})
}
</script>
`
content = content.replace(options.insertScriptBefore, html + options.insertScriptBefore)
return content
},
},
}
}
export { send }
export default plugin