vite.config.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { defineConfig, loadEnv,normalizePath} from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { resolve } from 'path'
  4. import { OUTPUT_DIR, brotliSize, chunkSizeWarningLimit, terserOptions, rollupOptions } from './build/constant'
  5. import viteCompression from 'vite-plugin-compression'
  6. import {axiosPre, cesiumApi} from './src/settings/httpSetting'
  7. import { viteMockServe } from 'vite-plugin-mock'
  8. import monacoEditorPlugin from 'vite-plugin-monaco-editor'
  9. import { viteExternalsPlugin } from 'vite-plugin-externals'
  10. import { viteStaticCopy } from 'vite-plugin-static-copy'
  11. import path from 'path'
  12. import cesium from 'vite-plugin-cesium'
  13. function pathResolve(dir: string) {
  14. return resolve(process.cwd(), '.', dir)
  15. }
  16. export default ({ mode }) => defineConfig({
  17. base: process.env.NODE_ENV === 'production' ? './' : '/',
  18. // 路径重定向
  19. resolve: {
  20. alias: [
  21. {
  22. find: /\/#\//,
  23. replacement: pathResolve('types')
  24. },
  25. {
  26. find: '@',
  27. replacement: pathResolve('src')
  28. },
  29. {
  30. find: 'vue-i18n',
  31. replacement: 'vue-i18n/dist/vue-i18n.cjs.js', //解决i8n警告
  32. }
  33. ],
  34. dedupe: ['vue']
  35. },
  36. // 全局 css 注册
  37. css: {
  38. preprocessorOptions: {
  39. scss: {
  40. javascriptEnabled: true,
  41. additionalData: `@import "src/styles/common/style.scss";`
  42. }
  43. }
  44. },
  45. // 开发服务器配置
  46. server: {
  47. host: true,
  48. open: true,
  49. port: Number(loadEnv(mode, process.cwd()).VITE_DEV_PORT),
  50. proxy: {
  51. [axiosPre]: {
  52. // @ts-ignore
  53. target: loadEnv(mode, process.cwd()).VITE_DEV_PATH,
  54. changeOrigin: true,
  55. ws: true,
  56. secure: true,
  57. },
  58. [cesiumApi]: {
  59. target: 'http://192.168.2.249:9033',
  60. changeOrigin: true,
  61. ws: true,
  62. secure: true
  63. }
  64. }
  65. },
  66. plugins: [
  67. vue({
  68. template: {
  69. compilerOptions: {
  70. // 排除 iconify 图标影子组件编译报错
  71. isCustomElement: tag => tag.startsWith('iconify-icon')
  72. }
  73. }
  74. }),
  75. cesium(),
  76. viteExternalsPlugin({
  77. 'cesium': 'Cesium',
  78. 'xbsj-renderer/dist-node/xr-base-utils': 'xbsj["xr-base-utils"]',
  79. 'xbsj-renderer/dist-node/xr-math': 'xbsj["xr-math"]',
  80. 'xbsj-renderer/dist-node/xr-utils': 'xbsj["xr-utils"]',
  81. 'xbsj-renderer/dist-node/xr-cesium': 'xbsj["xr-cesium"]',
  82. 'xbsj-xe2/dist-node/xe2': 'XE2["xe2"]',
  83. 'xbsj-xe2/dist-node/xe2-base': 'XE2["xe2-base"]',
  84. 'xbsj-xe2/dist-node/xe2-base-utils': 'XE2["xe2-base-utils"]',
  85. 'xbsj-xe2/dist-node/xe2-utils': 'XE2["xe2-utils"]',
  86. 'xbsj-xe2/dist-node/xe2-cesium': 'XE2["xe2-cesium"]',
  87. 'xbsj-xe2/dist-node/xe2-mapbox': 'XE2["xe2-mapbox"]',
  88. 'xbsj-xe2/dist-node/xe2-ue': 'XE2["xe2-ue"]',
  89. 'xbsj-xe2/dist-node/utility-xe2-plugin': 'XE2["utility-xe2-plugin"]',
  90. 'xbsj-xe2/dist-node/xe2-all-in-one': 'XE2["xe2-all-in-one"]',
  91. 'xbsj-xe2/dist-node/xe2-base-objects': 'XE2["xe2-base-objects"]',
  92. 'xbsj-xe2/dist-node/xe2-objects': 'XE2["xe2-objects"]',
  93. 'xbsj-xe2/dist-node/xe2-cesium-objects': 'XE2["xe2-cesium-objects"]',
  94. 'xbsj-xe2/dist-node/xe2-ue-objects': 'XE2["xe2-ue-objects"]',
  95. 'xbsj-xe2/dist-node/xe2-openlayers': 'XE2["xe2-openlayers"]',
  96. 'xbsj-xe2/dist-node/xe2-openlayers-objects': 'XE2["xe2-openlayers-objects"]',
  97. 'smplotting-xe2-plugin/dist-node/smplotting-xe2-plugin': 'XE2["smplotting-xe2-plugin"]',
  98. 'smplotting-xe2-plugin/dist-node/smplotting-xe2-plugin-main': 'XE2["smplotting-xe2-plugin-main"]',
  99. 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin': 'XE2["esobjs-xe2-plugin"]',
  100. 'esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main': 'XE2["esobjs-xe2-plugin-main"]',
  101. }, { useWindow: false }),
  102. // 运行和构建时copy
  103. viteStaticCopy({
  104. targets: [
  105. {
  106. src: normalizePath(path.resolve(__dirname, './node_modules/xbsj-xe2/dist-web')),//wu
  107. dest: './js/xbsj-xe2'
  108. },
  109. {
  110. src: normalizePath(path.resolve(__dirname, './node_modules/xbsj-xe2-assets/dist-web')),//w
  111. dest: './js/xbsj-xe2-assets'
  112. },
  113. {
  114. src: normalizePath(path.resolve(__dirname, './node_modules/smplotting-xe2-plugin/dist-web')),//wu
  115. dest: './js/smplotting-xe2-plugin'
  116. },
  117. {
  118. src: normalizePath(path.resolve(__dirname, './node_modules/esobjs-xe2-plugin/dist-web')),
  119. dest: './js/esobjs-xe2-plugin'
  120. },
  121. {
  122. src: normalizePath(path.resolve(__dirname, './node_modules/esobjs-xe2-plugin-assets/dist-web')),//wu
  123. dest: './js/esobjs-xe2-plugin-assets'
  124. },
  125. {
  126. src: normalizePath(path.resolve(__dirname, './node_modules/vue-xe2-plugin/dist-web')),
  127. dest: './js/vue-xe2-plugin'
  128. }
  129. ]
  130. }),
  131. (monacoEditorPlugin as any).default({
  132. languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html']
  133. }),
  134. viteMockServe({
  135. mockPath: '/src/api/mock',
  136. // 开发打包开关
  137. localEnabled: true,
  138. // 生产打包开关
  139. prodEnabled: true,
  140. // 打开后,可以读取 ts 文件模块。 请注意,打开后将无法监视.js 文件
  141. supportTs: true,
  142. // 监视文件更改
  143. watchFiles: true
  144. }),
  145. // 压缩
  146. viteCompression({
  147. verbose: true,
  148. disable: false,
  149. threshold: 10240,
  150. algorithm: 'gzip',
  151. ext: '.gz'
  152. })
  153. ],
  154. build: {
  155. target: 'es2020',
  156. outDir: OUTPUT_DIR,
  157. // minify: 'terser', // 如果需要用terser混淆,可打开这两行
  158. // terserOptions: terserOptions,
  159. rollupOptions: rollupOptions,
  160. reportCompressedSize: brotliSize,
  161. chunkSizeWarningLimit: chunkSizeWarningLimit
  162. }
  163. })