Skip to content

Commit ab6913d

Browse files
author
Taois
committed
feat:打包体积
1 parent 105e7fd commit ab6913d

File tree

7 files changed

+290
-28
lines changed

7 files changed

+290
-28
lines changed

.pkgignore

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 开发和测试文件
2+
test.js
3+
quick-test.js
4+
build.js
5+
start.js
6+
test-video.html
7+
8+
# 文档文件
9+
README.md
10+
LICENSE
11+
API_DOCS.md
12+
13+
# Git 相关
14+
.gitignore
15+
.git/
16+
17+
# Node.js 相关
18+
node_modules/.cache/
19+
node_modules/*/test/
20+
node_modules/*/tests/
21+
node_modules/*/.nyc_output/
22+
node_modules/*/coverage/
23+
node_modules/*/docs/
24+
node_modules/*/doc/
25+
node_modules/*/examples/
26+
node_modules/*/example/
27+
node_modules/*/benchmark/
28+
node_modules/*/benchmarks/
29+
30+
# Puppeteer 相关 - 排除大部分 Chromium 文件
31+
node_modules/puppeteer/.local-chromium/
32+
node_modules/puppeteer/lib/esm/
33+
node_modules/puppeteer/src/
34+
node_modules/puppeteer/test/
35+
node_modules/puppeteer/docs/
36+
37+
# 开发环境专用模块
38+
node_modules/pino-pretty/
39+
40+
# TypeScript 定义文件
41+
node_modules/@types/
42+
*.d.ts
43+
44+
# 日志文件
45+
*.log
46+
logs/
47+
48+
# 临时文件
49+
*.tmp
50+
*.temp
51+
.DS_Store
52+
Thumbs.db
53+
54+
# 开发工具配置
55+
.vscode/
56+
.idea/
57+
*.swp
58+
*.swo
59+
60+
# 构建输出
61+
dist/
62+
build/
63+
64+
# 包管理器文件
65+
package-lock.json
66+
yarn.lock
67+
pnpm-lock.yaml
68+
69+
# 其他大型依赖的非必要部分
70+
node_modules/*/CHANGELOG.md
71+
node_modules/*/HISTORY.md
72+
node_modules/*/AUTHORS
73+
node_modules/*/CONTRIBUTORS
74+
node_modules/*/*.md
75+
node_modules/*/*.txt
76+
node_modules/*/LICENSE*
77+
node_modules/*/LICENCE*

build-optimized.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 版本可能需要更多运行时依赖');
File renamed without changes.

docs/BUILD_GUIDE.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 构建指南
2+
3+
本项目提供了多种构建选项来优化最终可执行文件的体积。
4+
5+
## 构建选项
6+
7+
### 1. 标准构建 (GZip 压缩)
8+
```bash
9+
npm run build:all
10+
```
11+
- 使用 GZip 压缩
12+
- 兼容性最好
13+
- 体积适中
14+
15+
### 2. Brotli 压缩构建
16+
```bash
17+
npm run build:brotli
18+
```
19+
- 使用 Brotli 压缩
20+
- 比 GZip 体积更小
21+
- 压缩率更高
22+
23+
### 3. 轻量级构建
24+
```bash
25+
npm run build:lite
26+
```
27+
- 使用 Brotli 压缩
28+
- 禁用字节码缓存 (`--no-bytecode`)
29+
- 公开所有包 (`--public-packages=*`)
30+
- 体积最小
31+
32+
### 4. 迷你构建
33+
```bash
34+
npm run build:mini
35+
```
36+
- 最激进的压缩选项
37+
- 添加 `--no-warnings` 选项
38+
- 体积最小,但可能需要更多运行时依赖
39+
40+
### 5. 优化构建 (推荐)
41+
```bash
42+
npm run build:optimized
43+
```
44+
- 自动构建多个版本
45+
- 提供详细的体积统计
46+
- 包含标准版和迷你版
47+
48+
## 体积优化技术
49+
50+
### 1. 压缩算法
51+
- **GZip**: 标准压缩,兼容性好
52+
- **Brotli**: 更高压缩率,体积更小
53+
54+
### 2. PKG 选项
55+
- `--no-bytecode`: 禁用字节码缓存,减少体积
56+
- `--public-packages=*`: 将所有包标记为公开,减少包装开销
57+
- `--no-warnings`: 禁用警告,减少输出体积
58+
59+
### 3. 资源排除
60+
- 使用 `.pkgignore` 文件排除不必要的文件
61+
- 移除了大部分 Puppeteer Chromium 文件
62+
- 排除开发和测试文件
63+
64+
### 4. 依赖优化
65+
- 只包含必要的运行时文件
66+
- 排除文档、测试和示例文件
67+
- 移除 TypeScript 定义文件
68+
69+
## 文件说明
70+
71+
- `.pkgignore`: 定义打包时要排除的文件和目录
72+
- `build-optimized.js`: 优化构建脚本,提供详细统计
73+
- `BUILD_GUIDE.md`: 本构建指南
74+
75+
## 使用建议
76+
77+
1. **开发测试**: 使用 `npm run build:win` (或对应平台)
78+
2. **生产部署**: 使用 `npm run build:optimized`
79+
3. **极限压缩**: 使用 `npm run build:mini`
80+
81+
## 注意事项
82+
83+
- 迷你版本可能在某些环境下需要额外的运行时依赖
84+
- Brotli 压缩的文件启动时间可能稍长
85+
- 建议在目标环境中测试压缩版本的兼容性

package.json

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,26 @@
1010
"test": "node test.js",
1111
"build": "node build.js",
1212
"build:all": "npm run build:win && npm run build:linux && npm run build:macos && npm run build:macos-arm",
13-
"build:win": "pkg . --targets node18-win-x64 --output dist/pup-sniffer-win.exe",
14-
"build:linux": "pkg . --targets node18-linux-x64 --output dist/pup-sniffer-linux",
15-
"build:macos": "pkg . --targets node18-macos-x64 --output dist/pup-sniffer-macos",
16-
"build:macos-arm": "pkg . --targets node18-macos-arm64 --output dist/pup-sniffer-macos-arm64",
13+
"build:win": "pkg . --targets node18-win-x64 --compress GZip --output dist/pup-sniffer-win.exe",
14+
"build:linux": "pkg . --targets node18-linux-x64 --compress GZip --output dist/pup-sniffer-linux",
15+
"build:macos": "pkg . --targets node18-macos-x64 --compress GZip --output dist/pup-sniffer-macos",
16+
"build:macos-arm": "pkg . --targets node18-macos-arm64 --compress GZip --output dist/pup-sniffer-macos-arm64",
17+
"build:brotli": "npm run build:win:brotli && npm run build:linux:brotli && npm run build:macos:brotli && npm run build:macos-arm:brotli",
18+
"build:win:brotli": "pkg . --targets node18-win-x64 --compress Brotli --output dist/pup-sniffer-win-brotli.exe",
19+
"build:linux:brotli": "pkg . --targets node18-linux-x64 --compress Brotli --output dist/pup-sniffer-linux-brotli",
20+
"build:macos:brotli": "pkg . --targets node18-macos-x64 --compress Brotli --output dist/pup-sniffer-macos-brotli",
21+
"build:macos-arm:brotli": "pkg . --targets node18-macos-arm64 --compress Brotli --output dist/pup-sniffer-macos-arm64-brotli",
22+
"build:lite": "npm run build:win:lite && npm run build:linux:lite && npm run build:macos:lite && npm run build:macos-arm:lite",
23+
"build:win:lite": "pkg . --targets node18-win-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-win-lite.exe",
24+
"build:linux:lite": "pkg . --targets node18-linux-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-linux-lite",
25+
"build:macos:lite": "pkg . --targets node18-macos-x64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-macos-lite",
26+
"build:macos-arm:lite": "pkg . --targets node18-macos-arm64 --compress Brotli --no-bytecode --public-packages=* --output dist/pup-sniffer-macos-arm64-lite",
27+
"build:optimized": "node build-optimized.js",
28+
"build:mini": "npm run build:win:mini && npm run build:linux:mini && npm run build:macos:mini && npm run build:macos-arm:mini",
29+
"build:win:mini": "pkg . --targets node18-win-x64 --compress Brotli --no-bytecode --public-packages=* --no-warnings --output dist/pup-sniffer-win-mini.exe",
30+
"build:linux:mini": "pkg . --targets node18-linux-x64 --compress Brotli --no-bytecode --public-packages=* --no-warnings --output dist/pup-sniffer-linux-mini",
31+
"build:macos:mini": "pkg . --targets node18-macos-x64 --compress Brotli --no-bytecode --public-packages=* --no-warnings --output dist/pup-sniffer-macos-mini",
32+
"build:macos-arm:mini": "pkg . --targets node18-macos-arm64 --compress Brotli --no-bytecode --public-packages=* --no-warnings --output dist/pup-sniffer-macos-arm64-mini",
1733
"clean": "rimraf dist",
1834
"prebuild": "npm run clean"
1935
},
@@ -49,16 +65,25 @@
4965
],
5066
"assets": [
5167
"demo.html",
52-
"node_modules/puppeteer/.local-chromium/**/*",
53-
"node_modules/thread-stream/lib/worker.js",
54-
"node_modules/pino-pretty/**/*"
68+
"node_modules/thread-stream/lib/worker.js"
5569
],
5670
"targets": [
5771
"node18-win-x64",
5872
"node18-linux-x64",
5973
"node18-macos-x64",
6074
"node18-macos-arm64"
6175
],
62-
"outputPath": "dist"
76+
"outputPath": "dist",
77+
"options": [
78+
"--no-bytecode",
79+
"--public-packages=*",
80+
"--no-warnings"
81+
],
82+
"patches": {
83+
"puppeteer": [
84+
"node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js"
85+
]
86+
},
87+
"compress": "Brotli"
6388
}
6489
}

server.cjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ function getResourcePath(filename) {
112112
}
113113

114114
// 创建 Fastify 实例
115+
const isPkg = typeof process.pkg !== 'undefined';
115116
const fastify = Fastify({
116-
logger: {
117+
logger: isPkg ? {
118+
level: 'info'
119+
} : {
117120
level: 'info',
118121
transport: {
119122
target: 'pino-pretty',

test-video.html

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)