vite.config.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { defineConfig, loadEnv } from 'vite'
  2. import path from 'path'
  3. import vue from '@vitejs/plugin-vue'
  4. const rewritePlugin = (path, excludes) => ({
  5. name: "rewrite-middleware",
  6. configureServer(serve) {
  7. serve.middlewares.use((req, res, next) => {
  8. if (req.url.startsWith(path)) {
  9. const isRaw = excludes.some((dir) => req.url.includes(dir))
  10. if (!isRaw) req.url = path
  11. }
  12. next()
  13. })
  14. },
  15. })
  16. export default ({ mode, command }) => {
  17. const env = loadEnv(mode, process.cwd())
  18. const { VITE_APP_BASE } = env
  19. return defineConfig({
  20. base:'./',
  21. plugins: [vue(),
  22. rewritePlugin('/datav.html/', ['/datav.html/src', '/datav.html/static'])
  23. ],
  24. resolve: {
  25. // https://cn.vitejs.dev/config/#resolve-alias
  26. alias: {
  27. // 设置路径
  28. '~': path.resolve(__dirname, './'),
  29. // 设置别名
  30. '@': path.resolve(__dirname, './src')
  31. },
  32. // https://cn.vitejs.dev/config/#resolve-extensions
  33. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  34. },
  35. build: {
  36. rollupOptions: {
  37. input: {
  38. index: path.resolve(__dirname, 'index.html'),
  39. datav: path.resolve(__dirname, 'datav.html')
  40. }
  41. }
  42. },
  43. // vite 相关配置
  44. server: {
  45. port: 2800,
  46. host: true,
  47. open: true,
  48. proxy: {
  49. '/api': {
  50. target: VITE_APP_BASE,
  51. changeOrigin: true,
  52. rewrite: (p) => p.replace(/^\/api/, '')
  53. },
  54. '/file': {
  55. target: VITE_APP_BASE,
  56. changeOrigin: true,
  57. rewrite: (p) => p.replace(/^\/file/, /file/)
  58. }
  59. },
  60. },
  61. css: {
  62. postcss: {
  63. plugins: [
  64. {
  65. postcssPlugin: 'internal:charset-removal',
  66. AtRule: {
  67. charset: (atRule) => {
  68. if (atRule.name === 'charset') {
  69. atRule.remove();
  70. }
  71. }
  72. }
  73. }
  74. ],
  75. },
  76. },
  77. })
  78. }