1
+ import { type PluginOption , defineConfig , splitVendorChunkPlugin , loadEnv } from 'vite'
2
+ import { join } from "node:path"
3
+ import vue from '@vitejs/plugin-vue'
4
+ import { viteExternalsPlugin } from 'vite-plugin-externals'
5
+ import { insertHtml , h } from 'vite-plugin-insert-html'
6
+ import { viteStaticCopy } from 'vite-plugin-static-copy'
7
+ import compress from 'vite-plugin-compression'
8
+
9
+ export default defineConfig ( ( context ) => {
10
+ const mode = context . mode
11
+ const envDir = 'env'
12
+ const env = loadEnv ( mode , envDir )
13
+ const base = '/'
14
+ const cesiumBaseUrl = env [ 'VITE_CESIUM_BASE_URL' ]
15
+ const isProd = mode === 'production'
16
+
17
+ const plugins : PluginOption [ ] = [
18
+ vue ( ) ,
19
+ splitVendorChunkPlugin ( ) ,
20
+ viteExternalsPlugin ( {
21
+ cesium : 'Cesium' ,
22
+ } ) ,
23
+ insertHtml ( {
24
+ head : [
25
+ h ( 'script' , {
26
+ src : isProd ? `${ cesiumBaseUrl } Cesium.js` : `${ base } ${ cesiumBaseUrl } Cesium.js`
27
+ } )
28
+ ]
29
+ } ) ,
30
+ ]
31
+ if ( ! isProd ) {
32
+ // 开发模式,复制 node_modules 下的 cesium 依赖
33
+ const cesiumLibraryRoot = 'node_modules/cesium/Build/CesiumUnminified/'
34
+ const cesiumLibraryCopyToRootPath = 'libs/cesium/' // 相对于打包后的路径
35
+ const cesiumStaticSourceCopyOptions = [ 'Assets' , 'ThirdParty' , 'Workers' , 'Widgets' ] . map ( ( dirName ) => {
36
+ return {
37
+ src : `${ cesiumLibraryRoot } ${ dirName } /*` ,
38
+ dest : `${ cesiumLibraryCopyToRootPath } ${ dirName } `
39
+ }
40
+ } )
41
+ plugins . push (
42
+ viteStaticCopy ( {
43
+ targets : [
44
+ {
45
+ src : `${ cesiumLibraryRoot } Cesium.js` ,
46
+ dest : cesiumLibraryCopyToRootPath
47
+ } ,
48
+ ...cesiumStaticSourceCopyOptions
49
+ ]
50
+ } ) ,
51
+ )
52
+ }
53
+ plugins . push ( compress ( {
54
+ threshold : 10 * 1024 // 10KB 以下不压缩
55
+ } ) )
56
+
57
+ console . log ( env )
58
+
59
+ return {
60
+ plugins,
61
+ envDir,
62
+ base,
63
+ build : {
64
+ chunkSizeWarningLimit : 1024 * 1024 ,
65
+ reportCompressedSize : false ,
66
+ } ,
67
+ resolve : {
68
+ alias : {
69
+ '@' : join ( __dirname , "src" )
70
+ }
71
+ }
72
+ }
73
+ } )
0 commit comments