@1-/minify_size 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -32
- package/_.js +55 -22
- package/cli.js +1 -1
- package/package.json +3 -4
- package/file.js +0 -19
package/README.md
CHANGED
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
|
|
19
19
|
Evaluates JavaScript library size under modern network transmission environments supporting Brotli. For all `.js` files in the specified directory, performs:
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
- UTF-8 encoding of the
|
|
21
|
+
- Bundling using `@1-/rolldown` (Rust-based JavaScript bundler wrapping rolldown v1.1.3)
|
|
22
|
+
- UTF-8 encoding of the bundled code
|
|
23
23
|
- Brotli compression via Node.js built-in `node:zlib.brotliCompress` to compute final byte length
|
|
24
|
-
- Aggregation of per-
|
|
24
|
+
- Aggregation of per-bundle sizes and calculation of total bundled compressed size
|
|
25
25
|
|
|
26
26
|
## 2. Usage Demo
|
|
27
27
|
|
|
@@ -46,26 +46,26 @@ minify_size ./src
|
|
|
46
46
|
Example output:
|
|
47
47
|
|
|
48
48
|
```
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
Total bundled size
|
|
49
|
+
index.js 400
|
|
50
|
+
utils.js 250
|
|
51
|
+
Total bundled size 650
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
## 3. Design Concept
|
|
55
55
|
|
|
56
56
|
Execution flow (vertical Mermaid diagram):
|
|
57
57
|
|
|
58
|
-

|
|
59
59
|
|
|
60
60
|
## 4. Tech Stack
|
|
61
61
|
|
|
62
62
|
- **Runtime**: Node.js / Bun
|
|
63
|
-
- **
|
|
63
|
+
- **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler wrapping rolldown v1.1.3)
|
|
64
64
|
- **Brotli Engine**: Built-in `node:zlib` (Brotli compression)
|
|
65
|
-
- **Arg Parser**: `yargs`
|
|
66
|
-
- **Encoding**: `@3-/utf8` (TextEncoder-based UTF-8 encoding)
|
|
67
|
-
- **Output Formatting**: `cli-table3` (Formatted tabular output)
|
|
68
|
-
- **File
|
|
65
|
+
- **Arg Parser**: `yargs` v18.0.0
|
|
66
|
+
- **Encoding**: `@3-/utf8` v0.1.1 (TextEncoder-based UTF-8 encoding)
|
|
67
|
+
- **Output Formatting**: `cli-table3` v0.6.5 (Formatted tabular output)
|
|
68
|
+
- **File Walking**: `@1-/walk` v0.1.1 (Directory traversal utility with concurrency control)
|
|
69
69
|
- **Dependency Management**: npm
|
|
70
70
|
- **Testing**: bun:test
|
|
71
71
|
|
|
@@ -74,14 +74,12 @@ Execution flow (vertical Mermaid diagram):
|
|
|
74
74
|
```
|
|
75
75
|
src/
|
|
76
76
|
├── cli.js # CLI entrypoint, parses directory parameter and invokes main function
|
|
77
|
-
|
|
78
|
-
└── file.js # Single file processing: reading, oxc-minify compression, brotli compression and size calculation
|
|
77
|
+
└── _.js # Directory traversal, bundling, Brotli compression calculation and formatted output
|
|
79
78
|
```
|
|
80
79
|
|
|
81
80
|
## 6. History
|
|
82
81
|
|
|
83
|
-
Brotli was developed by Jyrki Alakuijala and Zoltán Szabadka at Google in 2013. It was initially designed for compression of web fonts, and was later extended to become a general-purpose compression algorithm optimized for web transmission, becoming an industry standard (RFC 7932).
|
|
84
|
-
|
|
82
|
+
Brotli was developed by Jyrki Alakuijala and Zoltán Szabadka at Google in 2013. It was initially designed for compression of web fonts, and was later extended to become a general-purpose compression algorithm optimized for web transmission, becoming an industry standard (RFC 7932). Modern JavaScript bundlers like rolldown leverage Rust's performance to achieve sub-second builds while maintaining compatibility with existing JavaScript tooling ecosystems.
|
|
85
83
|
|
|
86
84
|
## About
|
|
87
85
|
|
|
@@ -108,10 +106,10 @@ This library is developed by [WebC.site](https://webc.site).
|
|
|
108
106
|
|
|
109
107
|
评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。对指定目录中所有 `.js` 文件执行以下操作:
|
|
110
108
|
|
|
111
|
-
- 使用
|
|
112
|
-
-
|
|
109
|
+
- 使用 `@1-/rolldown`(Rust 实现的 JavaScript 打包器)进行打包和语法感知压缩
|
|
110
|
+
- 将打包后代码编码为 UTF-8 字节流
|
|
113
111
|
- 使用 Node.js 内置 `node:zlib.brotliCompress` 计算 Brotli 压缩后字节长度
|
|
114
|
-
-
|
|
112
|
+
- 汇总各打包产物大小,并计算整体打包压缩后大小
|
|
115
113
|
|
|
116
114
|
## 2. 使用演示
|
|
117
115
|
|
|
@@ -136,26 +134,26 @@ minify_size ./src
|
|
|
136
134
|
输出示例:
|
|
137
135
|
|
|
138
136
|
```
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
整体打包压缩后大小
|
|
137
|
+
index.js 400
|
|
138
|
+
utils.js 250
|
|
139
|
+
整体打包压缩后大小 650
|
|
142
140
|
```
|
|
143
141
|
|
|
144
142
|
## 3. 设计思路
|
|
145
143
|
|
|
146
144
|
系统执行流程如下(垂直 Mermaid 流程图):
|
|
147
145
|
|
|
148
|
-

|
|
149
147
|
|
|
150
148
|
## 4. 技术栈
|
|
151
149
|
|
|
152
150
|
- **Runtime**: Node.js / Bun
|
|
153
|
-
- **
|
|
151
|
+
- **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler wrapping rolldown v1.1.3)
|
|
154
152
|
- **Brotli Engine**: 内置 `node:zlib` (Brotli 压缩)
|
|
155
|
-
- **Arg Parser**: `yargs`
|
|
156
|
-
- **Encoding**: `@3-/utf8` (TextEncoder-based UTF-8 encoding)
|
|
157
|
-
- **Output Formatting**: `cli-table3` (Formatted tabular output)
|
|
158
|
-
- **File
|
|
153
|
+
- **Arg Parser**: `yargs` v18.0.0
|
|
154
|
+
- **Encoding**: `@3-/utf8` v0.1.1 (TextEncoder-based UTF-8 encoding)
|
|
155
|
+
- **Output Formatting**: `cli-table3` v0.6.5 (Formatted tabular output)
|
|
156
|
+
- **File Walking**: `@1-/walk` v0.1.1 (Directory traversal utility with concurrency control)
|
|
159
157
|
- **Dependency Management**: npm
|
|
160
158
|
- **Testing**: bun:test
|
|
161
159
|
|
|
@@ -164,14 +162,12 @@ file.js 250
|
|
|
164
162
|
```
|
|
165
163
|
src/
|
|
166
164
|
├── cli.js # CLI 命令行入口,解析目录参数并调用主函数
|
|
167
|
-
|
|
168
|
-
└── file.js # 单文件处理:读取、oxc-minify 压缩、brotli 压缩及大小计算
|
|
165
|
+
└── _.js # 目录遍历、打包处理、Brotli压缩计算及格式化输出
|
|
169
166
|
```
|
|
170
167
|
|
|
171
168
|
## 6. 历史故事
|
|
172
169
|
|
|
173
|
-
Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。它最初被设计用于压缩网页字体,后来发展为通用压缩算法,用于优化网页传输,并成为行业标准(RFC 7932
|
|
174
|
-
|
|
170
|
+
Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。它最初被设计用于压缩网页字体,后来发展为通用压缩算法,用于优化网页传输,并成为行业标准(RFC 7932)。现代 JavaScript bundlers like rolldown leverage Rust's performance to achieve sub-second builds while maintaining compatibility with existing JavaScript tooling ecosystems.
|
|
175
171
|
|
|
176
172
|
## 关于
|
|
177
173
|
|
package/_.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
1
|
+
import { relative, join } from "node:path";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
2
3
|
import { promisify } from "node:util";
|
|
3
4
|
import { brotliCompress } from "node:zlib";
|
|
4
5
|
import Table from "cli-table3";
|
|
5
6
|
import { FILE } from "@1-/walk";
|
|
6
7
|
import walkRelIgnore from "@1-/walk/walkRelIgnore.js";
|
|
7
8
|
import utf8e from "@3-/utf8";
|
|
8
|
-
import
|
|
9
|
+
import bundle from "@1-/rolldown";
|
|
9
10
|
|
|
10
11
|
const BROTLI = promisify(brotliCompress),
|
|
11
12
|
TABLE_STYLE = { "padding-left": 0, "padding-right": 0 },
|
|
@@ -25,34 +26,66 @@ const BROTLI = promisify(brotliCompress),
|
|
|
25
26
|
right: "",
|
|
26
27
|
"right-mid": "",
|
|
27
28
|
middle: " ",
|
|
29
|
+
},
|
|
30
|
+
/*
|
|
31
|
+
获取字符串 brotli 压缩后大小
|
|
32
|
+
参数: str 待压缩字符串
|
|
33
|
+
返回值: 字节数
|
|
34
|
+
*/
|
|
35
|
+
br = async (str) => (await BROTLI(utf8e(str))).length,
|
|
36
|
+
/*
|
|
37
|
+
输出打包大小表格
|
|
38
|
+
参数:
|
|
39
|
+
list 包含 [相对路径, 大小] 的数组
|
|
40
|
+
total 整体压缩后大小
|
|
41
|
+
*/
|
|
42
|
+
show = (list, total) => {
|
|
43
|
+
const table = new Table({ chars: NO_BORDER, style: TABLE_STYLE });
|
|
44
|
+
list.forEach(([rel_path, size]) => table.push([rel_path, size]));
|
|
45
|
+
table.push(["整体打包压缩后大小", total]);
|
|
46
|
+
console.log(table.toString());
|
|
28
47
|
};
|
|
29
48
|
|
|
30
|
-
|
|
49
|
+
/*
|
|
50
|
+
打包并计算指定目录下 JS 文件压缩后大小
|
|
51
|
+
参数: dir 目录路径
|
|
52
|
+
返回值: 整体打包压缩大小 (字节)
|
|
53
|
+
*/
|
|
31
54
|
export default async (dir) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
55
|
+
const real_dir = realpathSync(dir),
|
|
56
|
+
files = [],
|
|
57
|
+
input = {},
|
|
58
|
+
out_map = {};
|
|
59
|
+
|
|
60
|
+
await walkRelIgnore(real_dir, (kind, rel_path) => {
|
|
61
|
+
if (kind === FILE && rel_path.endsWith(".js") && !/(^|\/)tests?(\/|$)/.test(rel_path)) {
|
|
36
62
|
files.push(rel_path);
|
|
37
63
|
}
|
|
38
64
|
});
|
|
39
|
-
files.sort();
|
|
40
65
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
66
|
+
if (!files.length) {
|
|
67
|
+
show([], 0);
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
files.sort().forEach((rel_path) => {
|
|
72
|
+
const abs = realpathSync(join(real_dir, rel_path));
|
|
73
|
+
input[rel_path.slice(0, -3)] = abs;
|
|
74
|
+
out_map[abs] = abs;
|
|
75
|
+
});
|
|
52
76
|
|
|
53
|
-
|
|
77
|
+
const chunks = await bundle(input, {}, true, undefined, out_map),
|
|
78
|
+
sizes = (
|
|
79
|
+
await Promise.all(
|
|
80
|
+
chunks.map(async ([abs_out_path, code]) => [
|
|
81
|
+
relative(real_dir, abs_out_path),
|
|
82
|
+
await br(code),
|
|
83
|
+
code,
|
|
84
|
+
]),
|
|
85
|
+
)
|
|
86
|
+
).sort((a, b) => a[0].localeCompare(b[0])),
|
|
87
|
+
total_size = await br(sizes.map(([, , code]) => code).join(""));
|
|
88
|
+
show(sizes, total_size);
|
|
54
89
|
|
|
55
|
-
table.push(["整体打包压缩后大小", total_size]);
|
|
56
|
-
console.log(table.toString());
|
|
57
90
|
return total_size;
|
|
58
91
|
};
|
package/cli.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1-/minify_size",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Minify JS and output brotli compressed size | 压缩 JS 并输出 brotli 压缩后大小",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"br",
|
|
@@ -25,11 +25,10 @@
|
|
|
25
25
|
"./*": "./*"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@1-/
|
|
29
|
-
"@
|
|
28
|
+
"@1-/rolldown": "^0.1.7",
|
|
29
|
+
"@1-/walk": "^0.1.2",
|
|
30
30
|
"@3-/utf8": "^0.1.1",
|
|
31
31
|
"cli-table3": "^0.6.5",
|
|
32
|
-
"oxc-minify": "^0.135.0",
|
|
33
32
|
"yargs": "^18.0.0"
|
|
34
33
|
}
|
|
35
34
|
}
|
package/file.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import read from "@3-/read";
|
|
2
|
-
import { minifySync } from "oxc-minify";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
|
-
import { brotliCompress } from "node:zlib";
|
|
5
|
-
import utf8e from "@3-/utf8";
|
|
6
|
-
import { basename } from "node:path";
|
|
7
|
-
|
|
8
|
-
const BROTLI = promisify(brotliCompress),
|
|
9
|
-
MINIFY_OPT = {
|
|
10
|
-
compress: { target: "esnext" },
|
|
11
|
-
codegen: { removeWhitespace: true },
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
// file_path 压缩文件路径。返回 [brotli压缩后大小, 压缩后代码]
|
|
15
|
-
export default async (file_path) => {
|
|
16
|
-
const code = read(file_path),
|
|
17
|
-
{ code: mini } = minifySync(basename(file_path), code, MINIFY_OPT);
|
|
18
|
-
return [(await BROTLI(utf8e(mini))).length, mini];
|
|
19
|
-
};
|