-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvitrine.js
51 lines (44 loc) · 1.21 KB
/
vitrine.js
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
import process from 'node:process'
import semver from 'semver'
import Server from './src/server.js'
export default function vitrinePlugin({
includes = [],
prefix = '/components',
template = '_preview.html',
basePaths = ['resources/styles'],
componentPattern = /\.md|\.html?$/i,
} = {}) {
const server = new Server({ prefix, basePaths, componentPattern, template })
server.include(includes)
return {
name: 'vitrine',
version: '0.1.0',
configureServer(vite) {
if (semver.lt(process.versions.node, '20.0.0')) {
console.error("Vitrine requires node version 20 or newer.")
process.exit(1)
}
console.log(` Vitrine 0.1.0`)
vite.middlewares.use((req, res, next) => {
if (req.url.startsWith(prefix)) {
server.handle(req)
.then(body => {
res.setHeader('Content-Type', 'text/html')
res.end(body, 'utf8')
})
.catch(() => {
res.writeHead(404)
res.end()
})
} else {
next()
}
})
},
handleHotUpdate({ file, server }) {
if (componentPattern.test(file)) {
server.ws.send({ type: 'full-reload' })
}
}
}
}