vite.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 envFile = path.resolve(process.cwd(), '.env.development')
  21. if (fs.existsSync(envFile)) {
  22. const envConfig = dotenv.parse(fs.readFileSync(envFile))
  23. for (const k in envConfig) {
  24. process.env[k] = envConfig[k]
  25. }
  26. }
  27. viteLogo(process.env)
  28. const timestamp = Date.parse(new Date())
  29. const optimizeDeps = {}
  30. const alias = {
  31. '@': path.resolve(__dirname, './src'),
  32. 'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
  33. }
  34. const esbuild = {}
  35. // const rollupOptions = {
  36. // output: {
  37. // entryFileNames: 'js/087AC4D233B64EB0[name].js',
  38. // chunkFileNames: 'js/087AC4D233B64EB0[name].js',
  39. // assetFileNames: 'assets/087AC4D233B64EB0[name].[ext]',
  40. // },
  41. // }
  42. const rollupOptions = {
  43. output: {
  44. entryFileNames: 'js/[name].[hash].js', // 添加哈希值
  45. chunkFileNames: 'js/[name].[hash].js', // 添加哈希值
  46. assetFileNames: 'assets/[name].[hash].[ext]', // 添加哈希值
  47. },
  48. }
  49. const config = {
  50. base: './', // index.html文件所在位置
  51. root: './', // js导入的资源路径,src
  52. resolve: {
  53. alias,
  54. },
  55. define: {
  56. 'process.env': {}
  57. },
  58. server: {
  59. // 如果使用docker-compose开发模式,设置为false
  60. open: true,
  61. port: process.env.VITE_CLI_PORT,
  62. proxy: {
  63. // 把key的路径代理到target位置
  64. // detail: https://cli.vuejs.org/config/#devserver-proxy
  65. [process.env.VITE_BASE_API]: { // 需要代理的路径 例如 '/api'
  66. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
  67. changeOrigin: true,
  68. rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),
  69. },
  70. [process.env.VITE_UPLOADS_API]: { // 需要代理的路径 例如 '/api'
  71. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_UPLOADS_PORT}/`, // 代理到 目标路径
  72. changeOrigin: true
  73. },
  74. },
  75. },
  76. build: {
  77. minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
  78. manifest: false, // 是否产出manifest.json
  79. sourcemap: false, // 是否产出sourcemap.json
  80. outDir: 'dist', // 产出目录
  81. rollupOptions,
  82. },
  83. esbuild,
  84. optimizeDeps,
  85. plugins: [
  86. GvaPositionServer(),
  87. GvaPosition(),
  88. legacyPlugin({
  89. targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
  90. }),
  91. vuePlugin(),
  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(
  104. fullImportPlugin()
  105. )
  106. } else {
  107. config.plugins.push(AutoImport({
  108. resolvers: [ElementPlusResolver()]
  109. }),
  110. Components({
  111. resolvers: [ElementPlusResolver({
  112. importStyle: 'sass'
  113. })]
  114. }))
  115. }
  116. return config
  117. }