vite.config.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import vue from '@vitejs/plugin-vue';
  2. import viteCompression from 'vite-plugin-compression'
  3. import { resolve } from 'path';
  4. import { defineConfig, loadEnv, ConfigEnv } from 'vite';
  5. const pathResolve = (dir: string): any => {
  6. return resolve(__dirname, '.', dir);
  7. };
  8. const alias: Record<string, string> = {
  9. '/@': pathResolve('./src/'),
  10. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
  11. '@form-create/component-wangeditor': '/node_modules/@form-create/component-wangeditor/dist/index.js',
  12. };
  13. const viteConfig = defineConfig((mode: ConfigEnv) => {
  14. const env = loadEnv(mode.mode, process.cwd());
  15. return {
  16. plugins: [
  17. vue(),
  18. viteCompression({
  19. threshold: 1024 * 20, // 对大于 20k 的文件进行压缩
  20. // filter: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i, // 需要压缩的文件
  21. algorithm: 'gzip', // 压缩方式
  22. ext: 'gz', // 后缀名
  23. deleteOriginFile: false, // 压缩后是否删除压缩源文件
  24. })
  25. ],
  26. root: process.cwd(),
  27. resolve: { alias },
  28. base: mode.command === 'serve' ? './' : env.VITE_PUBLIC_PATH,
  29. optimizeDeps: {
  30. include: ['element-plus/lib/locale/lang/zh-cn', 'element-plus/lib/locale/lang/en', 'element-plus/lib/locale/lang/zh-tw'],
  31. },
  32. server: {
  33. port: env.VITE_PORT as unknown as number,
  34. host: true,
  35. open: true,
  36. hmr: true,
  37. // proxy: {
  38. // [env.VITE_NGINX_PROXY ? env.VITE_NGINX_PROXY : '/xxxxxxxxxxxxx']: {
  39. // target: env.VITE_SERVER_ORIGIN,
  40. // ws: true,
  41. // changeOrigin: true,
  42. // // rewrite: (path) => path.replace(/^\/api/, ''),
  43. // },
  44. // },
  45. },
  46. build: {
  47. outDir: 'sagoo-iot',
  48. sourcemap: false,
  49. cssCodeSplit: false,
  50. chunkSizeWarningLimit: 1500,
  51. rollupOptions: {
  52. output: {
  53. entryFileNames: `assets/[name].${new Date().getTime()}.js`,
  54. chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
  55. assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
  56. compact: true,
  57. manualChunks: {
  58. vue: ['vue', 'vue-router', 'vuex'],
  59. echarts: ['echarts'],
  60. },
  61. },
  62. },
  63. },
  64. css: {
  65. postcss: {
  66. plugins: [
  67. {
  68. postcssPlugin: 'internal:charset-removal',
  69. AtRule: {
  70. charset: (atRule) => {
  71. if (atRule.name === 'charset') {
  72. atRule.remove();
  73. }
  74. },
  75. },
  76. },
  77. ],
  78. },
  79. },
  80. define: {
  81. __VUE_I18N_LEGACY_API__: JSON.stringify(false),
  82. __VUE_I18N_FULL_INSTALL__: JSON.stringify(false),
  83. __INTLIFY_PROD_DEVTOOLS__: JSON.stringify(false),
  84. },
  85. };
  86. });
  87. export default viteConfig;