vite.config.js 2.2 KB

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