index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import vue from '@vitejs/plugin-vue'
  2. import vueJsx from '@vitejs/plugin-vue-jsx'
  3. import chokidar from 'chokidar'
  4. import dayjs from 'dayjs'
  5. import pc from 'picocolors'
  6. import type { Plugin } from 'vite'
  7. import { createBanner } from './banner/'
  8. import { createMock } from './mock/'
  9. import { createProgress } from './progress/'
  10. import { createPwa } from './pwa/'
  11. import { createSvgIcons } from './svgSprite/'
  12. import { createUnPlugin } from './unplugin/'
  13. import { port } from '/@/config/'
  14. const viteApp = 'VITE_' + 'APP_'
  15. const viteUser = 'VITE_' + 'USER_'
  16. export const createVitePlugin = (env: Record<string, string>) => {
  17. const vitePlugins: (Plugin | Plugin[])[] = [vue()]
  18. const userName = env[`${viteApp}GITHUB_USER_NAME`]
  19. const secretKey = env[`${viteApp}SECRET_KEY`]
  20. const nodeEnv = env[`${viteUser}NODE_ENV`]
  21. const isEmpty = (value: any) => {
  22. return value == undefined || value == '' || value == null
  23. }
  24. if (isEmpty(userName) || isEmpty(secretKey)) return
  25. if (nodeEnv !== 'development') if (isEmpty(userName) || isEmpty(secretKey)) return
  26. vitePlugins.push(vueJsx())
  27. vitePlugins.push(createProgress(env) as any)
  28. vitePlugins.push(createUnPlugin(env))
  29. vitePlugins.push(createPwa())
  30. vitePlugins.push(createMock())
  31. vitePlugins.push(createSvgIcons())
  32. vitePlugins.push(createBanner())
  33. return vitePlugins
  34. }
  35. export const createWatch = (env: Record<string, string>) => {
  36. //为了防止新同事忘记配置授权码而造成项目无法打包,请保留以下提示
  37. const userName = env[`${viteApp}GITHUB_USER_NAME`]
  38. const secretKey = env[`${viteApp}SECRET_KEY`]
  39. const nodeEnv = env[`${viteUser}NODE_ENV`]
  40. if (nodeEnv === 'production') {
  41. if (userName === 'test' || secretKey === 'preview') {
  42. console.log(
  43. `${pc.red(
  44. '\u68c0\u6d4b\u5230\u60a8\u7684\u7528\u6237\u540d\u6216\u006b\u0065\u0079\u672a\u914d\u7f6e\uff0c\u006b\u0065\u0079\u5728\u8d2d\u4e70\u65f6\u901a\u8fc7\u90ae\u4ef6\u9080\u8bf7\u51fd\u53d1\u653e\uff0c\u5982\u60a8\u5df2\u8d2d\u4e70\u8bf7\u4ed4\u7ec6\u9605\u8bfb\u6587\u6863\u5e76\u8fdb\u884c\u914d\u7f6e\uff0c\u914d\u7f6e\u5b8c\u6210\u540e\u65b9\u53ef\u6253\u5305\u4f7f\u7528\u3002\u8d2d\u4e70\u5730\u5740\uff1a\u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0076\u0075\u0065\u002d\u0061\u0064\u006d\u0069\u006e\u002d\u0062\u0065\u0061\u0075\u0074\u0069\u0066\u0075\u006c\u002e\u0063\u006f\u006d\u002f\u0061\u0075\u0074\u0068\u006f\u0072\u0069\u007a\u0061\u0074\u0069\u006f\u006e\u002f\u0073\u0068\u006f\u0070\u002d\u0076\u0069\u0074\u0065\u002e\u0068\u0074\u006d\u006c'
  45. )}`
  46. )
  47. process.exit()
  48. }
  49. }
  50. if (nodeEnv === 'development') {
  51. chokidar.watch('./src/views').on('change', (path) => {
  52. if (path.endsWith('vue')) {
  53. console.log(
  54. `\n${pc.gray(dayjs().format('HH:mm:ss'))} ${pc.cyan('[Vue Sh' + 'op Vite]')} ${pc.cyan(`http://localhost:${port}/`)} ${pc.green(
  55. 'update success'
  56. )} `
  57. )
  58. }
  59. })
  60. }
  61. }