1+ #!/usr/bin/env node
2+
3+ const { execSync } = require ( 'child_process' ) ;
4+ const fs = require ( 'fs' ) ;
5+ const path = require ( 'path' ) ;
6+
7+ console . log ( '🚀 开始优化构建...' ) ;
8+
9+ // 清理输出目录
10+ if ( fs . existsSync ( 'dist' ) ) {
11+ console . log ( '🧹 清理旧的构建文件...' ) ;
12+ try {
13+ fs . rmSync ( 'dist' , { recursive : true , force : true } ) ;
14+ } catch ( error ) {
15+ console . log ( '⚠️ 清理失败,继续构建...' ) ;
16+ }
17+ }
18+
19+ // 创建输出目录
20+ fs . mkdirSync ( 'dist' , { recursive : true } ) ;
21+
22+ // 构建配置
23+ const builds = [
24+ {
25+ name : 'Windows (GZip)' ,
26+ command : 'pkg . --targets node18-win-x64 --compress GZip --output dist/pup-sniffer-win.exe'
27+ } ,
28+ {
29+ name : 'Windows (Brotli - 最小体积)' ,
30+ command : 'pkg . --targets node18-win-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-win-mini.exe'
31+ } ,
32+ {
33+ name : 'Linux (GZip)' ,
34+ command : 'pkg . --targets node18-linux-x64 --compress GZip --output dist/pup-sniffer-linux'
35+ } ,
36+ {
37+ name : 'Linux (Brotli - 最小体积)' ,
38+ command : 'pkg . --targets node18-linux-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-linux-mini'
39+ } ,
40+ {
41+ name : 'macOS Intel (GZip)' ,
42+ command : 'pkg . --targets node18-macos-x64 --compress GZip --output dist/pup-sniffer-macos'
43+ } ,
44+ {
45+ name : 'macOS Intel (Brotli - 最小体积)' ,
46+ command : 'pkg . --targets node18-macos-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-macos-mini'
47+ } ,
48+ {
49+ name : 'macOS ARM (GZip)' ,
50+ command : 'pkg . --targets node18-macos-arm64 --compress GZip --output dist/pup-sniffer-macos-arm64'
51+ } ,
52+ {
53+ name : 'macOS ARM (Brotli - 最小体积)' ,
54+ command : 'pkg . --targets node18-macos-arm64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-macos-arm64-mini'
55+ }
56+ ] ;
57+
58+ // 执行构建
59+ for ( const build of builds ) {
60+ console . log ( `\n📦 构建: ${ build . name } ` ) ;
61+ try {
62+ const startTime = Date . now ( ) ;
63+ execSync ( build . command , { stdio : 'inherit' } ) ;
64+ const endTime = Date . now ( ) ;
65+ console . log ( `✅ ${ build . name } 构建完成 (${ ( ( endTime - startTime ) / 1000 ) . toFixed ( 1 ) } s)` ) ;
66+ } catch ( error ) {
67+ console . error ( `❌ ${ build . name } 构建失败:` , error . message ) ;
68+ }
69+ }
70+
71+ // 显示文件大小统计
72+ console . log ( '\n📊 构建结果统计:' ) ;
73+ console . log ( '=' . repeat ( 60 ) ) ;
74+
75+ const distFiles = fs . readdirSync ( 'dist' ) . filter ( file =>
76+ ! file . includes ( '.' ) || file . endsWith ( '.exe' )
77+ ) ;
78+
79+ distFiles . forEach ( file => {
80+ const filePath = path . join ( 'dist' , file ) ;
81+ const stats = fs . statSync ( filePath ) ;
82+ const sizeInMB = ( stats . size / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
83+ console . log ( `${ file . padEnd ( 35 ) } ${ sizeInMB . padStart ( 8 ) } MB` ) ;
84+ } ) ;
85+
86+ console . log ( '=' . repeat ( 60 ) ) ;
87+ console . log ( '🎉 所有构建完成!' ) ;
88+ console . log ( '\n💡 提示:' ) ;
89+ console . log ( '- 使用 GZip 版本获得更好的兼容性' ) ;
90+ console . log ( '- 使用 Brotli mini 版本获得最小体积' ) ;
91+ console . log ( '- mini 版本可能需要更多运行时依赖' ) ;
0 commit comments