vite.config.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { defineConfig, loadEnv } from 'vite'
  2. import react from '@vitejs/plugin-react-swc'
  3. import {AntdResolve, createStyleImportPlugin} from "vite-plugin-style-import";
  4. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  5. // @ts-ignore
  6. import qiankun from 'vite-plugin-qiankun'
  7. import mdx from '@mdx-js/rollup'
  8. import * as path from 'path'
  9. import fse from 'fs-extra'
  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. qiankun('nginx-ui',{
  36. useDevMode: mode != 'production'
  37. }),
  38. ],
  39. css: {
  40. preprocessorOptions: {
  41. less: {
  42. javascriptEnabled: true
  43. }
  44. }
  45. },
  46. resolve:{
  47. alias: {
  48. '@': path.resolve(__dirname,'./src'),
  49. 'docs': path.resolve(__dirname,'./docs')
  50. }
  51. },
  52. assetsInclude: ["**/*.md"],
  53. server:{
  54. proxy: {
  55. ...(mode === 'desktop')? {
  56. "/api":{
  57. target: 'http://127.0.0.1:38080',
  58. rewrite: path => path.replace(/^\/api/,"")
  59. }
  60. } : {
  61. "/api":{
  62. // target: 'http://10.10.0.1:8080',
  63. target: 'http://127.0.0.1:8080',
  64. rewrite: path => path.replace(/^\/api/,"")
  65. }
  66. }
  67. }
  68. },
  69. build: {
  70. sourcemap: false,
  71. minify: true,
  72. rollupOptions: {
  73. output: {
  74. // 通过正则或者其他逻辑来确定哪些依赖应该被打包到vendor chunk中
  75. manualChunks(filename){
  76. if (filename.includes('/node_modules/ace-builds')) {
  77. return `ace-builds-${acePackage.version}`;
  78. }
  79. if (filename.includes('/node_modules/react')) {
  80. return `react-all-${reactPackage.version}`;
  81. }
  82. },
  83. chunkFileNames: `cdn/[name].js`
  84. }
  85. }
  86. }
  87. }
  88. })