vite.config.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { svgBuilder } from './src/components/SvgIcon/svgBuilder'
  4. const { resolve,path } = require('path')
  5. const PrerenderSPAPlugin = require('prerender-spa-plugin')
  6. const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
  7. // https://vitejs.dev/config/
  8. export default defineConfig({
  9. productionSourceMap: false,
  10. lintOnSave: false, //关闭Eslint
  11. plugins: [
  12. vue(),
  13. svgBuilder('./src/assets/icon/') // svg图标批量引入
  14. ],
  15. configureWebpack: config => {
  16. config.plugins.push(
  17. new PrerenderSPAPlugin({
  18. staticDir: path.join(__dirname, 'dist'),
  19. // 需要进行预渲染的路由路径
  20. routes: ['/about'],
  21. // html文件压缩
  22. minify: {
  23. minifyCSS: true, // css压缩
  24. removeComments: true // 移除注释
  25. },
  26. renderer: new Renderer({
  27. inject: {
  28. // foo: 'bar'
  29. },
  30. // 在 main.js 中 new Vue({ mounted () {document.dispatchEvent(new Event('render-event'))}}),两者的事件名称要对应上。
  31. // renderAfterDocumentEvent: 'render-event'
  32. renderer: new PrerenderSPAPlugin.PuppeteerRenderer({ //这样写renderAfterTime生效了
  33. renderAfterTime: 5000
  34. })
  35. })
  36. })
  37. )
  38. },
  39. resolve: {
  40. alias: [
  41. // 别名配置
  42. {
  43. find: '@',
  44. replacement: resolve(__dirname, 'src')
  45. }
  46. ]
  47. },
  48. define: {
  49. buildTime: JSON.stringify(new Date().toLocaleString())
  50. },
  51. css: {
  52. preprocessorOptions: {
  53. less: {
  54. modifyVars: {
  55. hack: `true; @import (reference) "${resolve('src/assets/css/global.less')}";`
  56. },
  57. javascriptEnabled: true
  58. }
  59. }
  60. },
  61. server: {
  62. hrm: true,
  63. usePolling: true,
  64. // 服务器主机名
  65. host: 'localhost',
  66. // 端口号
  67. port: 3200,
  68. // 设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
  69. strictPort: false,
  70. // 服务器启动时自动在浏览器中打开应用程序,当此值为字符串时,会被用作 URL 的路径名
  71. open: false,
  72. // 自定义代理规则
  73. proxy: {
  74. // 选项写法
  75. '/api': {
  76. target: 'http://jsonplaceholder.typicode.com',
  77. changeOrigin: true,
  78. rewrite: path => path.replace(/^\/api/, '')
  79. }
  80. }
  81. }
  82. })