// 自定义插件 ./plugins/fullImportPlugin
import * as path from 'path'
import type { Plugin, ResolvedConfig } from 'vite'
export default function fullImportPlugin () {
let config: ResolvedConfig
return <Plugin>{
name: 'fullImportElementPlus',
async configResolved (conf) {
config = conf
},
transform (code, id) {
// 判断当前处理的是否是 _src/main.ts_
if (path.join(config.root, 'src/main.ts') === id) {
const name = 'ElementPlus'
// 引入 ElementPlus 和 样式
const prepend = `import 'element-plus/theme-chalk/src/index.scss';\n`
code = code.replace('.mount(', ($1) => `.use(${name})` + $1)
return prepend + code
}
return code
}
}
}
标签:src,code,return,plugin,path,import,vite
From: https://www.cnblogs.com/zhuxiang1633/p/16985344.html