index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { createCompress } from './compress/'
  9. import { createHttps } from './https'
  10. import { createMock } from './mock/'
  11. import { createProgress } from './progress/'
  12. import { createPwa } from './pwa/'
  13. import { createSvgIcons } from './svgSprite/'
  14. import { createUnPlugin } from './unplugin/'
  15. import { createVisualizer } from './visualizer/'
  16. import { compress, https, localEnabled, port, prodEnabled, pwa, pwaDev, report } from '/@/config/'
  17. const viteApp = 'VITE_' + 'APP_'
  18. const viteUser = 'VITE_' + 'USER_'
  19. export const createVitePlugin = (env: Record<string, string>) => {
  20. const vitePlugins: (Plugin | Plugin[])[] = [vue()]
  21. const userName = env[`${viteApp}GITHUB_USER_NAME`]
  22. const secretKey = env[`${viteApp}SECRET_KEY`]
  23. const nodeEnv = env[`${viteUser}NODE_ENV`]
  24. const isEmpty = (value: any) => {
  25. return value == undefined || value == '' || value == null
  26. }
  27. if (isEmpty(userName) || isEmpty(secretKey)) return
  28. if (nodeEnv !== 'development' && (isEmpty(userName) || isEmpty(secretKey))) return
  29. vitePlugins.push(
  30. vueJsx(),
  31. createProgress(env),
  32. createUnPlugin(env),
  33. createMock(localEnabled, prodEnabled),
  34. createSvgIcons(),
  35. createBanner()
  36. )
  37. if (compress) vitePlugins.push(createCompress(compress))
  38. if (pwa) vitePlugins.push(createPwa(nodeEnv, pwaDev))
  39. if (https) vitePlugins.push(createHttps())
  40. if (report) vitePlugins.push(createVisualizer())
  41. return vitePlugins
  42. }
  43. export const createWatch = (env: Record<string, string>) => {
  44. //为了防止新同事忘记配置授权码而造成项目无法打包,请保留以下提示
  45. const userName = env[`${viteApp}GITHUB_USER_NAME`]
  46. const secretKey = env[`${viteApp}SECRET_KEY`]
  47. const nodeEnv = env[`${viteUser}NODE_ENV`]
  48. if (nodeEnv === 'production' && (userName === 'test' || secretKey === 'preview')) {
  49. console.log(
  50. `${pc.red(
  51. '\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'
  52. )}`
  53. )
  54. process.exit()
  55. }
  56. if (nodeEnv === 'development') {
  57. chokidar.watch('./src/views').on('change', (path) => {
  58. if (path.endsWith('vue')) {
  59. console.log(
  60. `\n${pc.gray(dayjs().format('HH:mm:ss'))} ${pc.cyan('[Vue Sh' + 'op Vite]')} ${pc.cyan(`http://localhost:${port}/`)} ${pc.green(
  61. 'update success'
  62. )} `
  63. )
  64. }
  65. })
  66. }
  67. }