@@ -2,11 +2,103 @@ import { defineConfig } from 'vite'
22import react from '@vitejs/plugin-react'
33// import htmlPurge from 'vite-plugin-purgecss'
44import { cwd , version } from './common.js'
5- import { resolve } from 'path'
5+ import { resolve , join } from 'path'
66import def from './def.js'
77import commonjs from 'vite-plugin-commonjs'
8+ import {
9+ readdirSync ,
10+ statSync
11+ } from 'fs'
12+ import { filesize } from 'filesize'
813// import externalGlobals from 'rollup-plugin-external-globals'
914
15+ function getDirSize ( dirPath ) {
16+ let size = 0
17+ const files = readdirSync ( dirPath , { withFileTypes : true } )
18+ for ( const file of files ) {
19+ const fullPath = join ( dirPath , file . name )
20+ if ( file . isDirectory ( ) ) {
21+ size += getDirSize ( fullPath )
22+ } else {
23+ size += statSync ( fullPath ) . size
24+ }
25+ }
26+ return size
27+ }
28+
29+ function showTotalSizePlugin ( ) {
30+ return {
31+ name : 'show-total-size' ,
32+ closeBundle : {
33+ sequential : true ,
34+ order : 'post' ,
35+ handler ( ) {
36+ const outDir = resolve ( cwd , 'public' )
37+ try {
38+ const totalSize = getDirSize ( outDir )
39+ console . log ( `\n\x1b[32m✨ Total assets size: ${ filesize ( totalSize ) } \x1b[0m\n` )
40+ } catch ( e ) {
41+ console . error ( e )
42+ }
43+ }
44+ }
45+ }
46+ }
47+
48+ const manualChunks = ( id ) => {
49+ if ( id . includes ( 'node_modules' ) ) {
50+ if ( id . match ( / n o d e _ m o d u l e s \/ ( r e a c t | r e a c t - d o m | s c h e d u l e r ) \/ / ) ||
51+ id . includes ( 'object-assign' ) ||
52+ id . includes ( 'loose-envify' ) ) {
53+ return 'react-vendor'
54+ }
55+ if (
56+ id . includes ( 'react-delta-hooks' ) ||
57+ id . includes ( 'react-markdown' )
58+ ) {
59+ return 'react-utils'
60+ }
61+ if ( id . includes ( 'lodash-es' ) ) {
62+ return 'lodash-es'
63+ }
64+ if ( id . includes ( 'dayjs' ) ) {
65+ return 'dayjs'
66+ }
67+ if ( id . includes ( '@ant-design/icons' ) ) {
68+ return 'ant-icons'
69+ }
70+ if ( id . includes ( '@ant-design' ) || id . includes ( '@rc-component' ) || id . includes ( 'classnames' ) || id . includes ( '@ctrl/tinycolor' ) ) {
71+ return 'react-vendor'
72+ }
73+ if ( id . includes ( 'antd' ) ) {
74+ return 'antd'
75+ }
76+ if ( id . includes ( '@xterm' ) ) {
77+ return 'xterm'
78+ }
79+ if ( id . includes ( 'manate' ) ) {
80+ return 'manate'
81+ }
82+ if ( id . includes ( 'zmodem-ts' ) ) {
83+ return 'zmodem-ts'
84+ }
85+ if ( id . includes ( 'electerm-icons' ) ) {
86+ return 'electerm-icons'
87+ }
88+ if ( id . includes ( '@novnc/novnc' ) ) {
89+ return 'novnc'
90+ }
91+ if ( id . includes ( 'ironrdp-wasm' ) ) {
92+ return 'ironrdp-wasm'
93+ }
94+ if ( id . includes ( 'spice-client' ) ) {
95+ return 'spice'
96+ }
97+ // Combine rest of node_modules into one chunk
98+ return 'vendor'
99+ }
100+ }
101+
10102function buildInput ( ) {
11103 return {
12104 electerm : resolve ( cwd , 'src/client/entry-web/electerm.jsx' ) ,
@@ -24,7 +116,8 @@ export default defineConfig({
24116 // react: 'React',
25117 // 'react-dom': 'ReactDOM'
26118 // }),
27- react ( { include : / \. ( m d x | j s | j s x | t s | t s x | m j s ) $ / } )
119+ react ( { include : / \. ( m d x | j s | j s x | t s | t s x | m j s ) $ / } ) ,
120+ showTotalSizePlugin ( )
28121 ] ,
29122 // optimizeDeps: {
30123 // esbuildOptions: {
@@ -49,46 +142,7 @@ export default defineConfig({
49142 // 'react-dom'
50143 // ],
51144 output : {
52- manualChunks : {
53- react : [ 'react-dom' ] ,
54- 'lodash-es' : [ 'lodash-es' ] ,
55- dayjs : [ 'dayjs' ] ,
56- antd1 : [
57- '@ant-design/colors' ,
58- '@ant-design/cssinjs' ,
59- '@ant-design/react-slick' ,
60- '@ctrl/tinycolor' ,
61- 'classnames' ,
62- '@rc-component/color-picker' ,
63- '@rc-component/mutate-observer' ,
64- '@rc-component/tour' ,
65- '@rc-component/trigger' ,
66- 'scroll-into-view-if-needed' ,
67- 'throttle-debounce'
68- ] ,
69- antd : [ 'antd' ] ,
70- '@ant-design/icons' : [ '@ant-design/icons' ] ,
71- xterm : [
72- '@xterm/xterm'
73- ] ,
74- 'xterm-addon1' : [
75- '@xterm/addon-attach' ,
76- '@xterm/addon-canvas' ,
77- '@xterm/addon-fit' ,
78- '@xterm/addon-ligatures'
79- ] ,
80- 'xterm-addon2' : [
81- '@xterm/addon-search' ,
82- '@xterm/addon-unicode11' ,
83- '@xterm/addon-web-links' ,
84- '@xterm/addon-webgl'
85- ] ,
86- '@electerm/electerm-themes' : [ '@electerm/electerm-themes' ] ,
87- manate : [ 'manate' ] ,
88- 'spice-client' : [ 'spice-client' ] ,
89- 'electerm-icons' : [ 'electerm-icons' ] ,
90- 'react-utils' : [ 'react' , 'react-colorful' , 'react-delta-hooks' ]
91- } ,
145+ manualChunks,
92146 inlineDynamicImports : false ,
93147 format : 'esm' ,
94148 entryFileNames : `js/[name]-${ version } .js` ,
@@ -105,6 +159,10 @@ export default defineConfig({
105159 } ,
106160 resolve : {
107161 alias : {
162+ '@xterm/addon-image' : resolve ( fakePath , 'xterm-addon.js' ) ,
163+ '@xterm/addon-ligatures' : resolve ( fakePath , 'xterm-addon.js' ) ,
164+ // '@xterm/addon-unicode11': resolve(fakePath, 'xterm-addon.js'),
165+ '@xterm/addon-webgl' : resolve ( fakePath , 'xterm-addon.js' ) ,
108166 'react-markdown' : resolve ( fakePath , 'react-markdown.jsx' ) ,
109167 '@novnc/novnc/core/rfb.js' : resolve ( fakePath , 'novnc.js' ) ,
110168 'zmodem-ts/dist/zsentry.js' : resolve ( fakePath , 'zmodem.js' ) ,
0 commit comments