vite.config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import legacyPlugin from '@vitejs/plugin-legacy'
  2. import AutoImport from 'unplugin-auto-import/vite'
  3. import Components from 'unplugin-vue-components/vite'
  4. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
  5. import Banner from 'vite-plugin-banner'
  6. import * as path from 'path'
  7. import * as dotenv from 'dotenv'
  8. import * as fs from 'fs'
  9. import vuePlugin from '@vitejs/plugin-vue'
  10. import GvaPosition from './vitePlugin/gvaPosition'
  11. import GvaPositionServer from './vitePlugin/codeServer'
  12. import fullImportPlugin from './vitePlugin/fullImport/fullImport.js'
  13. import { svgBuilder } from './vitePlugin/svgIcon/svgIcon.js'
  14. // @see https://cn.vitejs.dev/config/
  15. export default ({ command, mode }) => {
  16. const NODE_ENV = mode || 'development'
  17. const envFiles = [`.env.${NODE_ENV}`]
  18. for (const file of envFiles) {
  19. const envConfig = dotenv.parse(fs.readFileSync(file))
  20. for (const k in envConfig) {
  21. process.env[k] = envConfig[k]
  22. }
  23. }
  24. const timestamp = Date.parse(new Date())
  25. const optimizeDeps = {}
  26. const alias = {
  27. '@': path.resolve(__dirname, './src'),
  28. vue$: 'vue/dist/vue.runtime.esm-bundler.js'
  29. }
  30. const esbuild = {}
  31. const rollupOptions = {
  32. output: {
  33. entryFileNames: 'assets/087AC4D233B64EB0[name].js',
  34. chunkFileNames: 'assets/087AC4D233B64EB0[name].js',
  35. assetFileNames: 'assets/087AC4D233B64EB0[name].[ext]'
  36. }
  37. }
  38. const config = {
  39. base: './', // index.html文件所在位置
  40. root: './', // js导入的资源路径,src
  41. resolve: {
  42. alias
  43. },
  44. define: {
  45. 'process.env': {}
  46. },
  47. server: {
  48. // 如果使用docker-compose开发模式,设置为false
  49. open: true,
  50. port: process.env.VITE_CLI_PORT,
  51. proxy: {
  52. // IAM服务
  53. [process.env.VITE_BASE_API + '/iam']:{
  54. target: 'http://localhost:8080',
  55. changeOrigin: true,
  56. },
  57. // 把key的路径代理到target位置
  58. // detail: https://cli.vuejs.org/config/#devserver-proxy
  59. [process.env.VITE_BASE_API + '/cms']: {
  60. target: `${process.env.VITE_SERVICE_PATH}:${process.env.VITE_SERVICE_PORT}/`,
  61. changeOrigin: false,
  62. rewrite: path => path.replace(new RegExp('^'), '')
  63. },
  64. [process.env.VITE_BASE_API]: {
  65. // 需要代理的路径 例如 '/api'
  66. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
  67. changeOrigin: false,
  68. rewrite: path =>
  69. path.replace(new RegExp('^' + process.env.VITE_BASE_API), '')
  70. }
  71. }
  72. },
  73. build: {
  74. minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
  75. manifest: false, // 是否产出manifest.json
  76. sourcemap: false, // 是否产出sourcemap.json
  77. outDir: 'dist', // 产出目录
  78. rollupOptions
  79. },
  80. esbuild,
  81. optimizeDeps,
  82. plugins: [
  83. process.env.VITE_POSITION === 'open' && GvaPositionServer(),
  84. process.env.VITE_POSITION === 'open' && GvaPosition(),
  85. legacyPlugin({
  86. targets: [
  87. 'Android > 39',
  88. 'Chrome >= 60',
  89. 'Safari >= 10.1',
  90. 'iOS >= 10.3',
  91. 'Firefox >= 54',
  92. 'Edge >= 15'
  93. ]
  94. }),
  95. vuePlugin(),
  96. svgBuilder('./src/assets/icons/'),
  97. [Banner(`\n Build based on gin-vue-admin \n Time : ${timestamp}`)]
  98. ],
  99. css: {
  100. preprocessorOptions: {
  101. scss: {
  102. additionalData: `@use "@/style/element/index.scss" as *;`
  103. }
  104. }
  105. }
  106. }
  107. if (NODE_ENV === 'development') {
  108. config.plugins.push(fullImportPlugin())
  109. } else {
  110. config.plugins.push(
  111. AutoImport({
  112. resolvers: [ElementPlusResolver()]
  113. }),
  114. Components({
  115. resolvers: [
  116. ElementPlusResolver({
  117. importStyle: 'sass'
  118. })
  119. ]
  120. })
  121. )
  122. }
  123. return config
  124. }