vite.config.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // 'vue-router': 'VueRouter',
  19. // 'vuex': 'Vuex',
  20. axios: 'axios',
  21. // 'vant': 'vant',
  22. 'js-cookie': 'Cookies'
  23. }
  24. }
  25. export default defineConfig(({ mode, command }) => {
  26. const env = loadEnv(mode, process.cwd())
  27. return {
  28. base: env.VITE_APP_BASE_PUBLIC,
  29. build: {
  30. sourcemap: true,
  31. emptyOutDir: true,
  32. lib: {
  33. entry: './src/entry.js', // 入口文件
  34. name: 'TestBindPhone', // 库的全局变量名
  35. fileName: 'jy-bind-phone' // 输出的文件名
  36. },
  37. rollupOptions: {
  38. // 确保外部化处理 Vue,避免将 Vue等 打包进库
  39. external: ['vue', 'vant', 'vuex', 'vue-router'],
  40. output: {
  41. globals: {
  42. vue: 'Vue'
  43. }
  44. }
  45. },
  46. target: 'es2015' // 指定目标语法版本
  47. },
  48. plugins: [
  49. splitVendorChunkPlugin(),
  50. vue2(),
  51. ViteEjsPlugin({
  52. assets: {
  53. version: Date.now()
  54. }
  55. }),
  56. eslintPlugin(),
  57. // 不打包的库(外部需要通过cdn引入)
  58. viteExternalsPlugin(getExternals(command)),
  59. viteCompression({
  60. threshold: 1024
  61. }),
  62. visualizer(),
  63. cssInjectedByJsPlugin()
  64. ],
  65. resolve: {
  66. alias: [
  67. {
  68. find: /^~/,
  69. replacement: ''
  70. },
  71. {
  72. find: '@',
  73. replacement: resolve(__dirname, 'src')
  74. }
  75. ],
  76. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  77. },
  78. server: {
  79. host: '0.0.0.0',
  80. port: 8080,
  81. proxy: {
  82. // 接口解密iframe
  83. '^/page_decrypt': {
  84. target: 'https://jybx-webtest.jydev.jianyu360.com',
  85. changeOrigin: true
  86. },
  87. '/jyapi': {
  88. target: 'https://app2-jytest.jydev.jianyu360.com',
  89. changeOrigin: true,
  90. rewrite: (path) => path.replace(/^\/jyapi/, '')
  91. },
  92. '/api': {
  93. target: 'https://app2-jytest.jydev.jianyu360.com',
  94. changeOrigin: true,
  95. rewrite: (path) => path.replace(/^\/api/, '')
  96. }
  97. }
  98. }
  99. }
  100. })