2021-01-12 13:17:26 +08:00
|
|
|
const createVuePlugin = require('@vitejs/plugin-vue')
|
2020-11-12 13:30:04 +08:00
|
|
|
const getDemoByPath = require('./utils/get-demo-by-path')
|
2021-01-12 13:17:26 +08:00
|
|
|
const createRollupCssRenderPlugin = require('./rollup-plugin-css-render')
|
|
|
|
const demoIndexTransFormPlugin = require('./vite-plugin-index-tranform')
|
2020-11-12 13:30:04 +08:00
|
|
|
|
2021-01-12 13:17:26 +08:00
|
|
|
const fileRegex = /\.(md)|(entry)$/
|
|
|
|
|
|
|
|
const fn = async (id) => {
|
|
|
|
if (fileRegex.test(id)) {
|
|
|
|
const code = await getDemoByPath(id)
|
|
|
|
return {
|
|
|
|
code: code,
|
|
|
|
map: null
|
2020-11-12 13:30:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 13:17:26 +08:00
|
|
|
const vuePlugin = createVuePlugin({
|
|
|
|
include: [/\.vue$/, /\.md$/, /\.entry$/]
|
|
|
|
})
|
|
|
|
|
|
|
|
const createNaiveDemoVitePlugin = () => {
|
|
|
|
const naiveDemoVitePlugin = {
|
|
|
|
name: 'demo-vite',
|
|
|
|
transform (_, id) {
|
|
|
|
return fn(id)
|
|
|
|
},
|
|
|
|
config () {
|
|
|
|
return {
|
|
|
|
transformInclude: /\.(md|entry)$/
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async handleHotUpdate (ctx) {
|
|
|
|
const { file } = ctx
|
|
|
|
if (fileRegex.test(file)) {
|
|
|
|
const code = await getDemoByPath(file)
|
|
|
|
return vuePlugin.handleHotUpdate({
|
|
|
|
...ctx,
|
|
|
|
read: () => code
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const rollupCssRenderPlugin = createRollupCssRenderPlugin()
|
|
|
|
|
|
|
|
return [
|
|
|
|
demoIndexTransFormPlugin,
|
|
|
|
naiveDemoVitePlugin,
|
|
|
|
vuePlugin,
|
|
|
|
rollupCssRenderPlugin
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createNaiveDemoVitePlugin
|