vite.config.ts 2.7 KB

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