首页 > 其他分享 >vite plugin

vite plugin

时间:2022-12-15 16:37:24浏览次数:56  
标签:src code return plugin path import vite


// 自定义插件 ./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

相关文章