vite.config.ts 2.6 KB

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