naive-ui/build/vite-plugin-demo.js
2024-07-12 02:19:13 +08:00

38 lines
977 B
JavaScript

const createVuePlugin = require('@vitejs/plugin-vue')
const getTransformedVueSrc = require('./utils/get-demo-by-path')
const createCssrPlugin = require('./vite-plugin-css-render')
const siteIndexTransFormPlugin = require('./vite-plugin-index-tranform')
const fileRegex = /\.(md|vue)$/
const vuePlugin = createVuePlugin({
include: [/\.vue$/, /\.md$/]
})
function createDemoPlugin() {
const naiveDemoVitePlugin = {
name: 'demo-vite',
transform(_, id) {
if (fileRegex.test(id)) {
return getTransformedVueSrc(id)
}
},
async handleHotUpdate(ctx) {
const { file } = ctx
if (fileRegex.test(file)) {
const code = await getTransformedVueSrc(file)
return vuePlugin.handleHotUpdate({
...ctx,
read: () => code
})
}
}
}
const cssrPlugin = createCssrPlugin()
return [siteIndexTransFormPlugin, naiveDemoVitePlugin, vuePlugin, cssrPlugin]
}
module.exports = createDemoPlugin