vite.config.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { resolve } from 'node:path'
  2. import viteCompression from 'vite-plugin-compression'
  3. import { defineConfig, loadEnv, splitVendorChunkPlugin } from 'vite'
  4. import vue2 from '@vitejs/plugin-vue2'
  5. import { ViteEjsPlugin } from 'vite-plugin-ejs'
  6. import { viteExternalsPlugin } from 'vite-plugin-externals'
  7. import { visualizer } from 'rollup-plugin-visualizer'
  8. import eslintPlugin from '@nabla/vite-plugin-eslint'
  9. import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
  10. function getExternals(isDev) {
  11. if (isDev) {
  12. // serve
  13. return {}
  14. }
  15. // build
  16. return {
  17. vue: 'Vue',
  18. // axios: 'axios',
  19. }
  20. }
  21. export default defineConfig(({ mode, command }) => {
  22. const env = loadEnv(mode, process.cwd())
  23. return {
  24. base: env.VITE_APP_BASE_PUBLIC,
  25. build: {
  26. sourcemap: true,
  27. emptyOutDir: true,
  28. lib: {
  29. entry: './src/entry.js', // 入口文件
  30. name: 'leaveSourceVue', // 库的全局变量名
  31. fileName: 'leave-source-vue' // 输出的文件名
  32. },
  33. rollupOptions: {
  34. // 确保外部化处理 Vue,避免将 Vue等 打包进库
  35. external: ['vue', 'vuex', 'vue-router', 'element-ui', 'vant'],
  36. output: {
  37. globals: {
  38. vue: 'Vue'
  39. }
  40. }
  41. },
  42. target: 'es2015' // 指定目标语法版本
  43. },
  44. plugins: [
  45. splitVendorChunkPlugin(),
  46. vue2(),
  47. ViteEjsPlugin({
  48. assets: {
  49. version: Date.now()
  50. }
  51. }),
  52. eslintPlugin(),
  53. // 不打包的库(外部需要通过cdn引入)
  54. viteExternalsPlugin(getExternals(command)),
  55. viteCompression({
  56. threshold: 1024
  57. }),
  58. visualizer(),
  59. cssInjectedByJsPlugin()
  60. ],
  61. resolve: {
  62. alias: [
  63. {
  64. find: /^~/,
  65. replacement: ''
  66. },
  67. {
  68. find: '@',
  69. replacement: resolve(__dirname, 'src')
  70. }
  71. ],
  72. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  73. },
  74. server: {
  75. host: '0.0.0.0',
  76. proxy: {
  77. '/jyapi': {
  78. target: 'https://jybx-webtest.jydev.jianyu360.com',
  79. changeOrigin: true,
  80. rewrite: path => path.replace(/^\/jyapi/, '')
  81. },
  82. '/api': {
  83. target: 'https://jybx-webtest.jydev.jianyu360.com',
  84. changeOrigin: true,
  85. rewrite: path => path.replace(/^\/api/, '')
  86. }
  87. }
  88. }
  89. }
  90. })