fullImport.js 857 B

1234567891011121314151617181920212223
  1. import * as path from 'path'
  2. export default function fullImportPlugin() {
  3. let config
  4. return {
  5. name: 'fullImportElementPlus',
  6. async configResolved(conf) {
  7. config = conf
  8. },
  9. transform(code, id) {
  10. const sourcePath = path.join(config.root, 'src/main.js').split(path.sep).join('/')
  11. const targetPath = id.split(path.sep).join('/')
  12. if (sourcePath === targetPath) {
  13. const name = 'ElementPlus'
  14. // 引入 ElementPlus 和 样式
  15. code = code.replace(`import { createApp } from 'vue'`, ($1) => $1 + `\nimport ${name} from 'element-plus'`)
  16. code = code.replace(`import './style/element_visiable.scss'`, ($1) => $1 + `\nimport 'element-plus/theme-chalk/src/index.scss'`)
  17. code = code.replace('.mount(', ($1) => `.use(${name})` + $1)
  18. return code
  19. }
  20. return code
  21. }
  22. }
  23. }