vite.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. 'element-plus',
  104. 'pinia',
  105. '@vueuse/core',
  106. 'axios',
  107. '@wangeditor/editor',
  108. '@wangeditor/editor-for-vue',
  109. '@tinymce/tinymce-vue'
  110. ]
  111. },
  112. publicDir: false,
  113. define: {
  114. BASE_URL: env.BASE_URL
  115. },
  116. css: {
  117. preprocessorOptions: {
  118. // 全局样式引入
  119. scss: {
  120. javascriptEnabled: true,
  121. additionalData: `
  122. @use "src/styles/var.scss" as *;
  123. `
  124. }
  125. }
  126. },
  127. preprocessorOptions: {
  128. scss: {
  129. // additionalData: `@use "@/assets/styles/element.scss" as *;`,
  130. }
  131. },
  132. server: {
  133. host: '0.0.0.0',
  134. port: 8000,
  135. open: true, // 自动打开浏览器
  136. cors: true, // 允许跨域
  137. strictPort: false, // 端口占用直接退出
  138. hmr: true,
  139. proxy: {
  140. [env.VITE_BASE_URL]: {
  141. // 线上API地址
  142. target: 'http://m.test.duanju.dududus.com/api',
  143. // 本地API地址
  144. // target: 'http://localhost:8989',
  145. changeOrigin: true,
  146. rewrite: path => path.replace(new RegExp('^' + env.VITE_BASE_URL), '')
  147. }
  148. },
  149. fs: {
  150. allow: ['./']
  151. }
  152. },
  153. build: {
  154. sourcemap: false,
  155. // 消除打包大小超出警告
  156. chunkSizeWarningLimit: 2000,
  157. minify: 'terser',
  158. terserOptions: {
  159. compress: {
  160. drop_console: false,
  161. pure_funcs: ['console.log', 'console.info'],
  162. drop_debugger: true
  163. }
  164. },
  165. // emptyOutDir: false,
  166. outDir: 'dist',
  167. assetsDir: 'assets',
  168. rollupOptions: {
  169. input: path.resolve(__dirname, 'index.html'),
  170. output: {
  171. chunkFileNames: 'assets/js/[name]-[hash].js',
  172. entryFileNames: 'assets/js/[name]-[hash].js',
  173. assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
  174. }
  175. }
  176. }
  177. };
  178. });