vite.config.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {defineConfig, loadEnv} from 'vite'
  2. import react from '@vitejs/plugin-react'
  3. import {AntdResolve, createStyleImportPlugin} from "vite-plugin-style-import";
  4. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  5. import mdx from '@mdx-js/rollup'
  6. import * as path from 'path'
  7. import fse from 'fs-extra'
  8. const readDepInfo = (module: string) => {
  9. const filePath = path.join(__dirname, 'node_modules', module, 'package.json')
  10. if (fse.existsSync(filePath)) {
  11. return fse.readJSONSync(filePath)
  12. }
  13. return {
  14. version: 'unknown'
  15. }
  16. }
  17. const acePackage = readDepInfo('ace-builds')
  18. const reactPackage = readDepInfo('react')
  19. export default defineConfig(({command, mode}) => {
  20. console.log('command,mode', command, mode)
  21. const env = loadEnv(mode, process.cwd(), '')
  22. console.log('env', env.VITE_BASE_API)
  23. return {
  24. plugins: [
  25. mdx({
  26. format: 'detect',
  27. include: ["**/*.md", '**/*.mdx']
  28. }),
  29. react(),
  30. createStyleImportPlugin({
  31. resolves: [AntdResolve()]
  32. }),
  33. ],
  34. css: {
  35. preprocessorOptions: {
  36. less: {
  37. javascriptEnabled: true
  38. }
  39. }
  40. },
  41. resolve: {
  42. alias: {
  43. '@': path.resolve(__dirname, './src'),
  44. 'docs': path.resolve(__dirname, './docs')
  45. }
  46. },
  47. assetsInclude: ["**/*.md"],
  48. server: {
  49. proxy: {
  50. ...(mode === 'desktop') ? {
  51. "/api": {
  52. target: 'http://127.0.0.1:38080',
  53. rewrite: path => path.replace(/^\/api/, "")
  54. }
  55. } : {
  56. "/api": {
  57. // target: 'http://10.10.0.1:8080',
  58. target: 'http://127.0.0.1:8080',
  59. rewrite: path => path.replace(/^\/api/, "")
  60. },
  61. },
  62. "/rdm/": {
  63. // target: 'http://10.10.0.1:8080',
  64. // target: 'http://10.10.0.1:38085',
  65. target: 'http://127.0.0.1:38085',
  66. rewrite: path => path.replace(/^\/api/, ""),
  67. ws: true
  68. },
  69. }
  70. },
  71. build: {
  72. sourcemap: false,
  73. minify: true,
  74. rollupOptions: {
  75. output: {
  76. // 通过正则或者其他逻辑来确定哪些依赖应该被打包到vendor chunk中
  77. manualChunks(filename) {
  78. if (filename.includes('/node_modules/ace-builds')) {
  79. return `ace-builds-${acePackage.version}`;
  80. }
  81. if (filename.includes('/node_modules/react')) {
  82. return `react-all-${reactPackage.version}`;
  83. }
  84. },
  85. chunkFileNames: `cdn/[name].js`
  86. }
  87. }
  88. }
  89. }
  90. })