vite.config.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import vueJsx from '@vitejs/plugin-vue-jsx'
  4. // import eslintPlugin from 'vite-plugin-eslint'
  5. import { createHtmlPlugin } from 'vite-plugin-html'
  6. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  7. import vueSetupExtend from 'unplugin-vue-setup-extend-plus/vite'
  8. import path from 'path'
  9. import pkg from './package.json'
  10. import dayjs from 'dayjs'
  11. import { wrapperEnv } from './build/getEnv'
  12. const { dependencies, devDependencies, name, version } = pkg
  13. const __APP_INFO__ = {
  14. pkg: { dependencies, devDependencies, name, version },
  15. lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
  16. }
  17. // https://vitejs.dev/config/
  18. export default defineConfig(({ mode /*command,*/ }: ConfigEnv): UserConfig => {
  19. // if (command === 'serve') {
  20. // return {
  21. // // serve 独有配置
  22. // }
  23. // } else {
  24. // return {
  25. // // build 独有配置
  26. // }
  27. // }
  28. const process_cwd = process.cwd()
  29. // 获取 .env 环境配置文件
  30. const env = loadEnv(mode, process_cwd)
  31. const viteEnv = wrapperEnv(env)
  32. return {
  33. base: viteEnv.VITE_PUBLIC_PATH,
  34. resolve: {
  35. // Vite路径别名配置
  36. alias: {
  37. '@': path.resolve(__dirname, './src'),
  38. 'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
  39. },
  40. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.vue', '.json'] // 默认 ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
  41. },
  42. define: {
  43. __APP_INFO__: JSON.stringify(__APP_INFO__)
  44. },
  45. css: {
  46. devSourcemap: true,
  47. preprocessorOptions: {
  48. scss: {
  49. additionalData: `@import "@/styles/variables.scss";`
  50. }
  51. }
  52. },
  53. server: {
  54. host: '0.0.0.0',
  55. port: viteEnv.VITE_PORT,
  56. open: viteEnv.VITE_OPEN,
  57. cors: true,
  58. // Load proxy configuration from .env.development
  59. proxy: {}
  60. },
  61. plugins: [
  62. // vue支持
  63. vue(),
  64. // JSX支持
  65. vueJsx({
  66. // 默认支持 .[jt]sx .vue 和 .js 需要支持 jsx 写法 需要 配置
  67. include: /\.[jt]sx$/ // |\.vue$
  68. }),
  69. // // esLint 报错信息显示在浏览器界面上
  70. // eslintPlugin(),
  71. // name 可以写在 script 标签上
  72. vueSetupExtend({}),
  73. // 注入变量到 html 文件
  74. createHtmlPlugin({
  75. inject: {
  76. data: { title: viteEnv.VITE_APP_TITLE }
  77. }
  78. }),
  79. // 使用 svg 图标
  80. createSvgIconsPlugin({
  81. // 指定需要缓存的图标文件夹
  82. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
  83. // 指定symbolId格式
  84. symbolId: 'icon-[dir]-[name]'
  85. })
  86. ]
  87. }
  88. })