vite.config.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {resolve} from 'path'
  2. import type {ConfigEnv, UserConfig} from 'vite'
  3. import {loadEnv} from 'vite'
  4. import {createVitePlugins} from './build/vite'
  5. import {exclude, include} from "./build/vite/optimize"
  6. import postcssPxtoRem from 'postcss-pxtorem'
  7. // 当前执行node命令时文件夹的地址(工作目录)
  8. const root = process.cwd()
  9. // 路径查找
  10. function pathResolve(dir: string) {
  11. return resolve(root, '.', dir)
  12. }
  13. // https://vitejs.dev/config/
  14. export default ({command, mode}: ConfigEnv): UserConfig => {
  15. let env = {} as any
  16. const isBuild = command === 'build'
  17. if (!isBuild) {
  18. env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
  19. } else {
  20. env = loadEnv(mode, root)
  21. }
  22. return {
  23. base: env.VITE_BASE_PATH,
  24. root: root,
  25. // 服务端渲染
  26. server: {
  27. port: env.VITE_PORT, // 端口号
  28. host: "0.0.0.0",
  29. open: env.VITE_OPEN === 'true',
  30. // 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域
  31. // proxy: {
  32. // ['/admin-api']: {
  33. // target: env.VITE_BASE_URL,
  34. // ws: false,
  35. // changeOrigin: true,
  36. // rewrite: (path) => path.replace(new RegExp(`^/admin-api`), ''),
  37. // },
  38. // },
  39. },
  40. // 项目使用的vite插件。 单独提取到build/vite/plugin中管理
  41. plugins: createVitePlugins(),
  42. css: {
  43. postcss: {
  44. plugins: [
  45. postcssPxtoRem({
  46. rootValue: 192, // 按照自己的设计稿修改 1920/10
  47. propList: ['*'],
  48. }),
  49. ]
  50. },
  51. preprocessorOptions: {
  52. scss: {
  53. additionalData: '@use "@/styles/variables.scss" as *;',
  54. javascriptEnabled: true,
  55. silenceDeprecations: ["legacy-js-api"], // 参考自 https://stackoverflow.com/questions/78997907/the-legacy-js-api-is-deprecated-and-will-be-removed-in-dart-sass-2-0-0
  56. }
  57. }
  58. },
  59. resolve: {
  60. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
  61. alias: [
  62. {
  63. find: 'vue-i18n',
  64. replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
  65. },
  66. {
  67. find: /\@\//,
  68. replacement: `${pathResolve('src')}/`
  69. }
  70. ]
  71. },
  72. build: {
  73. minify: 'terser',
  74. outDir: env.VITE_OUT_DIR || 'dist',
  75. sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
  76. // brotliSize: false,
  77. terserOptions: {
  78. compress: {
  79. drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
  80. drop_console: env.VITE_DROP_CONSOLE === 'true'
  81. }
  82. },
  83. rollupOptions: {
  84. output: {
  85. manualChunks: {
  86. echarts: ['echarts'], // 将 echarts 单独打包,参考 https://gitee.com/yudaocode/yudao-ui-admin-vue3/issues/IAB1SX 讨论
  87. 'form-create': ['@form-create/element-ui'], // 参考 https://github.com/yudaocode/yudao-ui-admin-vue3/issues/148 讨论
  88. 'form-designer': ['@form-create/designer'],
  89. }
  90. },
  91. },
  92. },
  93. optimizeDeps: {include, exclude}
  94. }
  95. }