vite.config.js 3.7 KB

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