forked from tusen-ai/naive-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite-plugin-demo.js
32 lines (31 loc) · 1000 Bytes
/
vite-plugin-demo.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
const getDemoByPath = require('./utils/get-demo-by-path')
module.exports = function () {
const configureServer = [
async ({ app, resolver, watcher }) => {
// check and send hmr message
watcher.on('change', async (file) => {
if (file.endsWith('.md')) {
const content = await getDemoByPath(file)
watcher.handleVueReload(file, Date.now(), content)
}
})
app.use(async (ctx, next) => {
if (/\/@modules\/naive-ui\/?/.test(ctx.path)) {
ctx.path = ctx.path.replace(/\/@modules\/naive-ui\/?/, '/@naive-ui/index.js')
}
if (/\.md$/.test(ctx.path) || /\.entry$/.test(ctx.path)) {
const publicPath = ctx.path
const filePath = resolver.requestToFile(publicPath)
const content = await getDemoByPath(filePath)
// make it Treat as vue
ctx.vue = true
ctx.body = content
}
await next()
})
}
]
return {
configureServer
}
}