vite.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // 把key的路径代理到target位置
  53. // detail: https://cli.vuejs.org/config/#devserver-proxy
  54. [process.env.VITE_BASE_API + '/v1']: {
  55. target: `${process.env.VITE_ICVP_SERVER}/`,
  56. changeOrigin: false,
  57. rewrite: path => path.replace(new RegExp('^'), '')
  58. },
  59. [process.env.VITE_BASE_API]: {
  60. // 需要代理的路径 例如 '/api'
  61. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
  62. changeOrigin: false,
  63. rewrite: path =>
  64. path.replace(new RegExp('^' + process.env.VITE_BASE_API), '')
  65. }
  66. }
  67. },
  68. build: {
  69. minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
  70. manifest: false, // 是否产出manifest.json
  71. sourcemap: false, // 是否产出sourcemap.json
  72. outDir: 'dist', // 产出目录
  73. rollupOptions
  74. },
  75. esbuild,
  76. optimizeDeps,
  77. plugins: [
  78. process.env.VITE_POSITION === 'open' && GvaPositionServer(),
  79. process.env.VITE_POSITION === 'open' && GvaPosition(),
  80. legacyPlugin({
  81. targets: [
  82. 'Android > 39',
  83. 'Chrome >= 60',
  84. 'Safari >= 10.1',
  85. 'iOS >= 10.3',
  86. 'Firefox >= 54',
  87. 'Edge >= 15'
  88. ]
  89. }),
  90. vuePlugin(),
  91. svgBuilder('./src/assets/icons/'),
  92. [Banner(`\n Build based on gin-vue-admin \n Time : ${timestamp}`)]
  93. ],
  94. css: {
  95. preprocessorOptions: {
  96. scss: {
  97. additionalData: `@use "@/style/element/index.scss" as *;`
  98. }
  99. }
  100. }
  101. }
  102. if (NODE_ENV === 'development') {
  103. config.plugins.push(fullImportPlugin())
  104. } else {
  105. config.plugins.push(
  106. AutoImport({
  107. resolvers: [ElementPlusResolver()]
  108. }),
  109. Components({
  110. resolvers: [
  111. ElementPlusResolver({
  112. importStyle: 'sass'
  113. })
  114. ]
  115. })
  116. )
  117. }
  118. return config
  119. }