vite.config.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { UserConfig, ConfigEnv, loadEnv, defineConfig } from 'vite';
  2. import createPlugins from './vite/plugins';
  3. import path from 'path';
  4. export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  5. const env = loadEnv(mode, process.cwd());
  6. return {
  7. // 部署生产环境和开发环境下的URL。
  8. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  9. // 例如 https://www.yujin.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.yujin.vip/admin/,则设置 baseUrl 为 /admin/。
  10. base: env.VITE_APP_CONTEXT_PATH,
  11. resolve: {
  12. alias: {
  13. '~': path.resolve(__dirname, './'),
  14. '@': path.resolve(__dirname, './src')
  15. },
  16. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  17. },
  18. // https://cn.vitejs.dev/config/#resolve-extensions
  19. plugins: createPlugins(env, command === 'build'),
  20. server: {
  21. host: '0.0.0.0',
  22. port: Number(env.VITE_APP_PORT),
  23. open: true,
  24. proxy: {
  25. [env.VITE_APP_BASE_API]: {
  26. // target: 'http://dm.yujin.shuziyunyao.com/api',
  27. target: 'http://stlm.yujin.shuziyunyao.com/trace-api',
  28. // target: 'http://192.168.1.68:8080',
  29. changeOrigin: true,
  30. ws: true,
  31. rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
  32. }
  33. }
  34. },
  35. // vite.config.js
  36. build: {
  37. rollupOptions: {
  38. output: {
  39. manualChunks(id) {
  40. if (id.includes('node_modules')) {
  41. return 'vendor'; // 分离第三方依赖
  42. }
  43. }
  44. }
  45. }
  46. },
  47. css: {
  48. preprocessorOptions: {
  49. scss: {
  50. javascriptEnabled: true
  51. }
  52. },
  53. postcss: {
  54. plugins: [
  55. {
  56. postcssPlugin: 'internal:charset-removal',
  57. AtRule: {
  58. charset: (atRule) => {
  59. if (atRule.name === 'charset') {
  60. atRule.remove();
  61. }
  62. }
  63. }
  64. }
  65. ]
  66. }
  67. },
  68. // 预编译
  69. optimizeDeps: {
  70. include: ['vue', 'vue-router', 'pinia', 'axios', '@vueuse/core', 'echarts', '@vueup/vue-quill', 'image-conversion', 'element-plus/es/components/**/css']
  71. }
  72. };
  73. });