vite.config.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { defineConfig } from 'vite'
  2. import { createVuePlugin } from 'vite-plugin-vue2'
  3. import viteCompression from 'vite-plugin-compression'
  4. import path from 'path'
  5. import glob from 'glob'
  6. import fs from 'fs'
  7. // 多页面入口配置
  8. const entryConfig = function () {
  9. const viteconfig = {}
  10. // 含有.js的文件
  11. const entryFiles = glob.sync('./src/pages/*/*.js')
  12. // 获取模板
  13. const temp = fs.readFileSync('./index.html')
  14. // 模板增加ts入口
  15. entryFiles.forEach((entry) => {
  16. const projectName = entry.split('/')[3]
  17. try {
  18. fs.accessSync(`./${projectName}.html`)
  19. } catch (error) {
  20. // 重构模板
  21. const html = temp.toString().replace(/\.\/src\/[^\s]*/g, `${entry}"></script>`)
  22. fs.writeFile(`./${projectName}.html`, html, (err) => {
  23. if (err) console.error(err)
  24. })
  25. }
  26. viteconfig[projectName] = path.resolve(__dirname, `./${projectName}.html`)
  27. })
  28. return viteconfig
  29. }
  30. export default ({ mode }) => defineConfig({
  31. base: './',
  32. resolve: {
  33. alias: {
  34. /*
  35. 路径别名
  36. 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
  37. */
  38. '@': path.resolve(__dirname, './src')
  39. }
  40. },
  41. plugins: [createVuePlugin(), viteCompression()],
  42. server: {
  43. host: 'localhost',
  44. port: 6547,
  45. open: true,
  46. strictPort: false,
  47. https: false,
  48. // 反向代理
  49. proxy: {
  50. '/uploadOneInvoicePic': {
  51. target: 'https://ocr.caner.top',
  52. changeOrigin: true,
  53. rewrite: (path) => path.replace(/^\//, '')
  54. },
  55. '/getInvoiceResult': {
  56. target: 'https://ocr.caner.top',
  57. changeOrigin: true,
  58. rewrite: (path) => path.replace(/^\//, '')
  59. },
  60. }
  61. },
  62. build: {
  63. rollupOptions: {
  64. input: entryConfig(),
  65. // output: { //静态资源分类打包
  66. // chunkFileNames: 'js/[name]-[hash].js',
  67. // entryFileNames: 'js/[name]-[hash].js',
  68. // assetFileNames: 'assets/[name]-[hash].[ext]'
  69. // }
  70. },
  71. terserOptions: { //去掉打印
  72. compress: {
  73. drop_console: true,
  74. drop_debugger: true,
  75. },
  76. },
  77. assetsDir:'static'
  78. },
  79. define: { //环境变量配置
  80. 'process.env': {
  81. 'SERVERURL': mode === 'development' ? 'https://ocr.caner.top':'https://ocr.caner.top'
  82. }
  83. }
  84. })