vite.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { defineConfig, loadEnv } from 'vite';
  2. import vue from '@vitejs/plugin-vue';
  3. import alias from '@rollup/plugin-alias';
  4. import vueJsx from '@vitejs/plugin-vue-jsx';
  5. import path from 'path';
  6. import Icons from 'unplugin-icons/vite';
  7. import IconsResolver from 'unplugin-icons/resolver';
  8. import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
  9. import AutoImport from 'unplugin-auto-import/vite';
  10. import Components from 'unplugin-vue-components/vite';
  11. import { createHtmlPlugin } from 'vite-plugin-html';
  12. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  13. // https://vitejs.dev/config/
  14. export default defineConfig(({ command, mode }) => {
  15. const env = loadEnv(mode, process.cwd(), '');
  16. return {
  17. plugins: [
  18. createSvgIconsPlugin({
  19. // 指定需要缓存的图标文件夹
  20. iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
  21. // 指定symbolId格式
  22. symbolId: 'icon-[dir]-[name]'
  23. }),
  24. vue(),
  25. vueJsx(),
  26. createHtmlPlugin({
  27. minify: true,
  28. template: 'index.html',
  29. inject: {
  30. data: {
  31. title: env.APP_NAME
  32. }
  33. }
  34. }),
  35. alias({
  36. entries: [
  37. {
  38. find: '@',
  39. replacement: path.resolve(__dirname, '.', 'src')
  40. }
  41. ]
  42. }),
  43. AutoImport({
  44. // 目标文件
  45. include: [
  46. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  47. /\.vue$/,
  48. /\.vue\?vue/, // .vue
  49. /\.md$/ // .md
  50. ],
  51. // 生成配置文件,如果是ts项目,通常我们会把声明文件放在根目录/types中,注意,这个文件夹需要先建好,否则可能导致等下无法往里生成auto-imports.d.ts文件
  52. // dts: 'types/auto-imports.d.ts',
  53. vueTemplate: true,
  54. dts: path.resolve(__dirname, 'types', 'auto-imports.d.ts'), // 配置文件生成位置,默认是根目录 /auto-imports.d.ts
  55. imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
  56. // eslint报错解决
  57. eslintrc: {
  58. enabled: false, // Default `false`
  59. filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
  60. globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
  61. },
  62. resolvers: [
  63. // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
  64. ElementPlusResolver(),
  65. // 自动导入图标组件
  66. IconsResolver({
  67. prefix: 'Icon'
  68. })
  69. ]
  70. }),
  71. Components({
  72. resolvers: [
  73. // 自动注册图标组件
  74. IconsResolver({
  75. enabledCollections: ['ep'] //@iconify-json/ep 是 Element Plus 的图标库
  76. })
  77. // 自动导入 Element Plus 组件//dev环境会特别慢加上,build的时候可以放开
  78. // ElementPlusResolver()
  79. ],
  80. dirs: ['src/components/', 'src/layout/'],
  81. extensions: ['vue'],
  82. deep: true,
  83. // dts: true,
  84. dts: path.resolve(__dirname, 'types', 'components.d.ts'), // 配置文件生成位置,默认是根目录 /components.d.ts
  85. include: [/\.vue$/, /\.vue\?vue/],
  86. exclude: [
  87. /[\\/]node_modules[\\/]/,
  88. /[\\/]\.git[\\/]/,
  89. /[\\/]\.nuxt[\\/]/
  90. ]
  91. }),
  92. Icons({
  93. compiler: 'vue3',
  94. autoInstall: true
  95. })
  96. ],
  97. optimizeDeps: {
  98. include: [
  99. 'vue',
  100. 'vue-router',
  101. 'element-plus/es/locale/lang/zh-cn',
  102. 'element-plus/es/locale/lang/en',
  103. 'pinia',
  104. '@vueuse/core',
  105. 'axios',
  106. '@wangeditor/editor',
  107. '@wangeditor/editor-for-vue'
  108. ]
  109. },
  110. publicDir: false,
  111. define: {
  112. BASE_URL: env.BASE_URL
  113. },
  114. css: {
  115. preprocessorOptions: {
  116. // 全局样式引入
  117. scss: {
  118. javascriptEnabled: true,
  119. additionalData: `
  120. @use "src/styles/var.scss" as *;
  121. `
  122. }
  123. }
  124. },
  125. preprocessorOptions: {
  126. scss: {
  127. // additionalData: `@use "@/assets/styles/element.scss" as *;`,
  128. }
  129. },
  130. server: {
  131. host: '0.0.0.0',
  132. port: 8000,
  133. open: true, // 自动打开浏览器
  134. cors: true, // 允许跨域
  135. strictPort: false, // 端口占用直接退出
  136. hmr: true,
  137. proxy: {
  138. [env.VITE_BASE_URL]: {
  139. // 线上API地址
  140. target: 'http://121.37.183.29:8094/api',
  141. // 本地API地址
  142. // target: 'http://localhost:8989',
  143. changeOrigin: true,
  144. rewrite: path => path.replace(new RegExp('^' + env.VITE_BASE_URL), '')
  145. }
  146. },
  147. fs: {
  148. allow: ['./']
  149. }
  150. },
  151. build: {
  152. sourcemap: false,
  153. // 消除打包大小超出警告
  154. chunkSizeWarningLimit: 2000,
  155. minify: 'terser',
  156. terserOptions: {
  157. compress: {
  158. drop_console: false,
  159. pure_funcs: ['console.log', 'console.info'],
  160. drop_debugger: true
  161. }
  162. },
  163. // emptyOutDir: false,
  164. outDir: 'dist',
  165. assetsDir: 'assets',
  166. rollupOptions: {
  167. input: path.resolve(__dirname, 'index.html'),
  168. output: {
  169. chunkFileNames: 'assets/js/[name]-[hash].js',
  170. entryFileNames: 'assets/js/[name]-[hash].js',
  171. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
  172. }
  173. }
  174. }
  175. };
  176. });