You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

2 months ago
  1. import { defineConfig, loadEnv } from 'vite'
  2. import pkg from './package.json' // 新增包信息导入
  3. import dayjs from 'dayjs' // 新增时间库导入
  4. import { fileURLToPath, URL } from 'url' // 新增路径处理模块
  5. import { wrapperEnv } from './build/utils'
  6. import { createVitePlugins } from './build/vite/plugin'
  7. import { createProxy } from './build/vite/proxy'
  8. import { createBuild } from './build/vite/build'
  9. const { dependencies, devDependencies, name, version } = pkg
  10. // 应用信息
  11. const __APP_INFO__ = {
  12. pkg: { dependencies, devDependencies, name, version },
  13. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  14. }
  15. // https://vite.dev/config/
  16. export default defineConfig(({ command, mode }) => {
  17. // console.log('command', command)
  18. const root = process.cwd() // 当前工作目录
  19. const isBuild = command === 'build' // 是否是构建 serve
  20. const env = loadEnv(mode, root) // 加载env环境
  21. // The boolean type read by loadEnv is a string. This function can be converted to boolean type
  22. const viteEnv = wrapperEnv(env)
  23. // console.log('viteEnv', viteEnv)
  24. const { VITE_PUBLIC_PATH, VITE_OUTPUT_DIR } = viteEnv
  25. return {
  26. base: VITE_PUBLIC_PATH,
  27. root,
  28. plugins: createVitePlugins(viteEnv, isBuild),
  29. resolve: {
  30. alias: {
  31. '@': fileURLToPath(new URL('./src', import.meta.url))
  32. }
  33. },
  34. css: {
  35. preprocessorOptions: {
  36. scss: {
  37. charset: false, // 避免出现: build时的 @charset 必须在第一行的警告
  38. additionalData: `
  39. @import "@/styles/mixin.scss";
  40. @import "@/styles/variables.scss";
  41. `
  42. }
  43. }
  44. },
  45. server: {
  46. host: true,
  47. proxy: createProxy()
  48. },
  49. build: createBuild(viteEnv),
  50. define: {
  51. __APP_INFO__: JSON.stringify(__APP_INFO__)
  52. }
  53. }
  54. })