vite.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import legacyPlugin from '@vitejs/plugin-legacy'
  2. import { viteLogo } from './src/core/config'
  3. import Banner from 'vite-plugin-banner'
  4. import * as path from 'path'
  5. import * as dotenv from 'dotenv'
  6. import * as fs from 'fs'
  7. import vuePlugin from '@vitejs/plugin-vue'
  8. import vueDevTools from 'vite-plugin-vue-devtools'
  9. import VueFilePathPlugin from './vitePlugin/componentName/index.js'
  10. import { svgBuilder } from 'vite-auto-import-svg'
  11. import { AddSecret } from './vitePlugin/secret'
  12. // @see https://cn.vitejs.dev/config/
  13. export default ({ mode }) => {
  14. AddSecret('')
  15. const NODE_ENV = mode || 'development'
  16. const envFiles = [`.env.${NODE_ENV}`]
  17. for (const file of envFiles) {
  18. const envConfig = dotenv.parse(fs.readFileSync(file))
  19. for (const k in envConfig) {
  20. process.env[k] = envConfig[k]
  21. }
  22. }
  23. viteLogo(process.env)
  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].[hash].js',
  34. chunkFileNames: 'assets/087AC4D233B64EB0[name].[hash].js',
  35. assetFileNames: 'assets/087AC4D233B64EB0[name].[hash].[ext]'
  36. }
  37. }
  38. const config = {
  39. base: '/', // 编译后js导入的资源路径
  40. root: './', // index.html文件所在位置
  41. publicDir: 'public', // 静态资源文件夹
  42. resolve: {
  43. alias
  44. },
  45. define: {
  46. 'process.env': {}
  47. },
  48. css: {
  49. preprocessorOptions: {
  50. scss: {
  51. api: 'modern-compiler' // or "modern"
  52. }
  53. }
  54. },
  55. server: {
  56. // 如果使用docker-compose开发模式,设置为false
  57. open: true,
  58. port: process.env.VITE_CLI_PORT,
  59. proxy: {
  60. // 把key的路径代理到target位置
  61. // detail: https://cli.vuejs.org/config/#devserver-proxy
  62. [process.env.VITE_BASE_API]: {
  63. // 需要代理的路径 例如 '/api'
  64. target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
  65. changeOrigin: true,
  66. rewrite: (path) =>
  67. path.replace(new RegExp('^' + process.env.VITE_BASE_API), '')
  68. }
  69. }
  70. },
  71. build: {
  72. minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
  73. manifest: false, // 是否产出manifest.json
  74. sourcemap: false, // 是否产出sourcemap.json
  75. outDir: 'dist', // 产出目录
  76. terserOptions: {
  77. compress: {
  78. //生产环境时移除console
  79. drop_console: true,
  80. drop_debugger: true
  81. }
  82. },
  83. rollupOptions
  84. },
  85. esbuild,
  86. optimizeDeps,
  87. plugins: [
  88. process.env.VITE_POSITION === 'open' &&
  89. vueDevTools({ launchEditor: process.env.VITE_EDITOR }),
  90. legacyPlugin({
  91. targets: [
  92. 'Android > 39',
  93. 'Chrome >= 60',
  94. 'Safari >= 10.1',
  95. 'iOS >= 10.3',
  96. 'Firefox >= 54',
  97. 'Edge >= 15'
  98. ]
  99. }),
  100. vuePlugin(),
  101. svgBuilder('./src/assets/icons/'),
  102. svgBuilder('./src/plugin/'),
  103. [Banner(`\n Build based on gin-vue-admin \n Time : ${timestamp}`)],
  104. VueFilePathPlugin('./src/pathInfo.json')
  105. ]
  106. }
  107. return config
  108. }