vite.config.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { defineConfig, loadEnv } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import * as path from 'path'
  4. import AutoImport from 'unplugin-auto-import/vite'
  5. // unocss
  6. import UnoCSS from 'unocss/vite'
  7. // Icons 自动按需引入图标库
  8. import Icons from 'unplugin-icons/vite'
  9. import IconsResolver from 'unplugin-icons/resolver'
  10. import { FileSystemIconLoader } from 'unplugin-icons/loaders'
  11. // Ant Design Vue 4.x 自动按需引入组件
  12. import Components from 'unplugin-vue-components/vite'
  13. import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
  14. // https://vitejs.dev/config/
  15. export default defineConfig(({ mode }) => {
  16. const env = loadEnv(mode, process.cwd(), "")
  17. const proxyObj = {}
  18. console.log('mode-----当前环境', mode, env.VITE_APP_BASE_API,env.VITE_APP_BASE_URL);
  19. if (mode === 'development' && env.VITE_APP_BASE_API && env.VITE_APP_BASE_URL) {
  20. const httpsRE = /^https:\/\//;
  21. const isHttps = httpsRE.test(env.VITE_APP_BASE_URL);
  22. proxyObj[env.VITE_APP_BASE_API] = {
  23. target: env.VITE_APP_BASE_URL,
  24. changeOrigin: true,
  25. // rewrite: path => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ''),
  26. ws: true,
  27. ...(isHttps ? { secure: false } : {}),
  28. }
  29. }
  30. return {
  31. server: {
  32. port: 9000,
  33. proxy: {
  34. // '/jeecgboot': {
  35. // target: 'http://47.99.151.233:9000',
  36. // changeOrigin: true,
  37. // followRedirects: true, // Cookie支持重定向
  38. // // rewrite: (path) => path.replace(/^\/API/, ''),
  39. // },
  40. ...proxyObj
  41. },
  42. },
  43. plugins: [
  44. vue(),
  45. AutoImport({
  46. imports: ['vue', 'vue-router'],
  47. dts: true,
  48. resolvers: [],
  49. }),
  50. Components({
  51. resolvers: [
  52. // Ant Design Vue 4.x 自动按需引入组件
  53. AntDesignVueResolver({
  54. importStyle: false, // css in js
  55. }),
  56. IconsResolver({
  57. customCollections: ['custom'],
  58. }),
  59. ],
  60. }),
  61. UnoCSS(),
  62. Icons({
  63. autoInstall: true,
  64. compiler: 'vue3',
  65. customCollections: {
  66. custom: FileSystemIconLoader('src/assets/icons'),
  67. },
  68. }), // 自动安装
  69. ],
  70. resolve: {
  71. // 设置别名
  72. alias: {
  73. '@': path.resolve(__dirname, 'src'),
  74. Assets: path.resolve(__dirname, 'src/assets'),
  75. Components: path.resolve(__dirname, 'src/components'),
  76. Utils: path.resolve(__dirname, 'src/utils'), // 工具类方法(新创建的)
  77. Config: path.resolve(__dirname, 'src/config'),
  78. Views: path.resolve(__dirname, 'src/views'),
  79. Plugins: path.resolve(__dirname, 'src/plugins'),
  80. Routes: path.resolve(__dirname, 'src/routes'),
  81. API: path.resolve(__dirname, 'src/api'),
  82. Store: path.resolve(__dirname, 'src/store'),
  83. Types: path.resolve(__dirname, 'types'),
  84. },
  85. },
  86. css: {
  87. preprocessorOptions: {
  88. less: {
  89. modifyVars: {
  90. hack: `true; @import (reference) "${path.resolve(__dirname, 'src/assets/styles/variables.less')}";`,
  91. },
  92. javascriptEnabled: true,
  93. },
  94. },
  95. },
  96. build: {
  97. target: 'ESNext',
  98. minify: 'esbuild',
  99. // rollup 配置
  100. rollupOptions: {
  101. output: {
  102. chunkFileNames: 'js/[name]-[hash].js', // 引入文件名的名称
  103. entryFileNames: 'js/[name]-[hash].js', // 包的入口文件名称
  104. assetFileNames: '[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
  105. },
  106. },
  107. },
  108. esbuild: {
  109. drop: [
  110. // 'console', // 如果线上需要打印,就把这行注释掉
  111. 'debugger',
  112. ],
  113. },
  114. define: {
  115. 'process.env.ENABLE_ANALYTICS': JSON.stringify(process.env.NODE_ENV === 'production')
  116. }
  117. }
  118. })