2021-01-12 13:17:26 +08:00
|
|
|
const createVuePlugin = require('@vitejs/plugin-vue')
|
2021-02-02 13:11:34 +08:00
|
|
|
const getTransformedVueSrc = require('./utils/get-demo-by-path')
|
2021-02-03 21:38:43 +08:00
|
|
|
const createCssrPlugin = require('./vite-plugin-css-render')
|
|
|
|
const siteIndexTransFormPlugin = require('./vite-plugin-index-tranform')
|
2020-11-12 13:30:04 +08:00
|
|
|
|
2021-02-02 13:11:34 +08:00
|
|
|
const fileRegex = /\.(md|entry)$/
|
2021-01-12 13:17:26 +08:00
|
|
|
|
|
|
|
const vuePlugin = createVuePlugin({
|
|
|
|
include: [/\.vue$/, /\.md$/, /\.entry$/]
|
|
|
|
})
|
|
|
|
|
2021-02-03 21:38:43 +08:00
|
|
|
const createDemoPlugin = () => {
|
2021-01-12 13:17:26 +08:00
|
|
|
const naiveDemoVitePlugin = {
|
|
|
|
name: 'demo-vite',
|
|
|
|
transform (_, id) {
|
2021-02-02 13:11:34 +08:00
|
|
|
if (fileRegex.test(id)) {
|
|
|
|
return getTransformedVueSrc(id)
|
2021-01-12 13:17:26 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
async handleHotUpdate (ctx) {
|
|
|
|
const { file } = ctx
|
|
|
|
if (fileRegex.test(file)) {
|
2021-02-02 13:11:34 +08:00
|
|
|
const code = await getTransformedVueSrc(file)
|
2021-01-12 13:17:26 +08:00
|
|
|
return vuePlugin.handleHotUpdate({
|
|
|
|
...ctx,
|
|
|
|
read: () => code
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 13:11:34 +08:00
|
|
|
|
2021-02-03 21:38:43 +08:00
|
|
|
const cssrPlugin = createCssrPlugin()
|
2021-01-12 13:17:26 +08:00
|
|
|
|
2021-02-03 21:38:43 +08:00
|
|
|
return [siteIndexTransFormPlugin, naiveDemoVitePlugin, vuePlugin, cssrPlugin]
|
2021-01-12 13:17:26 +08:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:38:43 +08:00
|
|
|
module.exports = createDemoPlugin
|