@1-/minify_size 0.1.3 → 0.1.5

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.
Files changed (5) hide show
  1. package/README.md +23 -33
  2. package/_.js +40 -41
  3. package/cli.js +2 -2
  4. package/package.json +3 -5
  5. 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
- - Syntax-aware minification using `oxc-minify` (Rust implementation)
22
- - UTF-8 encoding of the minified code
21
+ - Bundling using `@1-/rolldown` (Rust-based JavaScript bundler)
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-file sizes and calculation of total bundled compressed size
24
+ - Returns total bundled compressed size (bytes)
25
25
 
26
26
  ## 2. Usage Demo
27
27
 
@@ -46,26 +46,23 @@ minify_size ./src
46
46
  Example output:
47
47
 
48
48
  ```
49
- _.js 400
50
- file.js 250
51
- Total bundled size 650
49
+ 650
52
50
  ```
53
51
 
54
52
  ## 3. Design Concept
55
53
 
56
54
  Execution flow (vertical Mermaid diagram):
57
55
 
58
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@AI/_OWRSd8Rk6TslPIF1blg.svg)
56
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@D7/nQLapVbeR_9qMiOldgug.svg)
59
57
 
60
58
  ## 4. Tech Stack
61
59
 
62
60
  - **Runtime**: Node.js / Bun
63
- - **JS Minifier**: `oxc-minify` (JavaScript minifier implemented in Rust)
61
+ - **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler)
64
62
  - **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 Reading**: `@3-/read` (Lightweight file reading utility)
63
+ - **Arg Parser**: `yargs` v18.0.0
64
+ - **Encoding**: `@3-/utf8` v0.1.1 (TextEncoder-based UTF-8 encoding)
65
+ - **File Walking**: `@1-/walk` v0.1.2 (Directory traversal utility)
69
66
  - **Dependency Management**: npm
70
67
  - **Testing**: bun:test
71
68
 
@@ -74,14 +71,12 @@ Execution flow (vertical Mermaid diagram):
74
71
  ```
75
72
  src/
76
73
  ├── cli.js # CLI entrypoint, parses directory parameter and invokes main function
77
- ├── _.js # Directory traversal, concurrent file processing, aggregation and formatted output
78
- └── file.js # Single file processing: reading, oxc-minify compression, brotli compression and size calculation
74
+ └── _.js # Directory traversal, bundling, Brotli compression calculation
79
75
  ```
80
76
 
81
77
  ## 6. History
82
78
 
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
-
79
+ 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
80
 
86
81
  ## About
87
82
 
@@ -108,10 +103,10 @@ This library is developed by [WebC.site](https://webc.site).
108
103
 
109
104
  评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。对指定目录中所有 `.js` 文件执行以下操作:
110
105
 
111
- - 使用 `oxc-minify`(Rust 实现)进行语法感知压缩
112
- - 将压缩后代码编码为 UTF-8 字节流
106
+ - 使用 `@1-/rolldown`(Rust 实现的 JavaScript 打包器)进行打包
107
+ - 将打包后代码编码为 UTF-8 字节流
113
108
  - 使用 Node.js 内置 `node:zlib.brotliCompress` 计算 Brotli 压缩后字节长度
114
- - 汇总各文件大小,并计算整体打包压缩后大小
109
+ - 返回整体打包压缩后大小(字节)
115
110
 
116
111
  ## 2. 使用演示
117
112
 
@@ -136,26 +131,23 @@ minify_size ./src
136
131
  输出示例:
137
132
 
138
133
  ```
139
- _.js 400
140
- file.js 250
141
- 整体打包压缩后大小 650
134
+ 650
142
135
  ```
143
136
 
144
137
  ## 3. 设计思路
145
138
 
146
139
  系统执行流程如下(垂直 Mermaid 流程图):
147
140
 
148
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@ko/Gf_-AqDa7IeLBsyqvTSg.svg)
141
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@Qx/di2QsMfRjPdfYr2IasNQ.svg)
149
142
 
150
143
  ## 4. 技术栈
151
144
 
152
145
  - **Runtime**: Node.js / Bun
153
- - **JS Minifier**: `oxc-minify` (Rust 实现的 JavaScript 压缩器)
154
- - **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 Reading**: `@3-/read` (Lightweight file reading utility)
146
+ - **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler)
147
+ - **Brotli Engine**: 内置 `node:zlib` (Brotli compression)
148
+ - **Arg Parser**: `yargs` v18.0.0
149
+ - **Encoding**: `@3-/utf8` v0.1.1 (TextEncoder-based UTF-8 encoding)
150
+ - **File Walking**: `@1-/walk` v0.1.2 (Directory traversal utility)
159
151
  - **Dependency Management**: npm
160
152
  - **Testing**: bun:test
161
153
 
@@ -164,14 +156,12 @@ file.js 250
164
156
  ```
165
157
  src/
166
158
  ├── cli.js # CLI 命令行入口,解析目录参数并调用主函数
167
- ├── _.js # 目录遍历、并发调度文件处理、汇总统计并格式化输出
168
- └── file.js # 单文件处理:读取、oxc-minify 压缩、brotli 压缩及大小计算
159
+ └── _.js # 目录遍历、打包处理、Brotli压缩计算
169
160
  ```
170
161
 
171
162
  ## 6. 历史故事
172
163
 
173
- Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。它最初被设计用于压缩网页字体,后来发展为通用压缩算法,用于优化网页传输,并成为行业标准(RFC 7932)。
174
-
164
+ 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
165
 
176
166
  ## 关于
177
167
 
package/_.js CHANGED
@@ -1,58 +1,57 @@
1
- import { join } from "node:path";
1
+ import { join, isAbsolute } from "node:path";
2
+ import { realpathSync } from "node:fs";
2
3
  import { promisify } from "node:util";
3
4
  import { brotliCompress } from "node:zlib";
4
- import Table from "cli-table3";
5
5
  import { FILE } from "@1-/walk";
6
6
  import walkRelIgnore from "@1-/walk/walkRelIgnore.js";
7
7
  import utf8e from "@3-/utf8";
8
- import minify from "./file.js";
8
+ import bundle from "@1-/rolldown";
9
9
 
10
10
  const BROTLI = promisify(brotliCompress),
11
- TABLE_STYLE = { "padding-left": 0, "padding-right": 0 },
12
- NO_BORDER = {
13
- top: "",
14
- "top-mid": "",
15
- "top-left": "",
16
- "top-right": "",
17
- bottom: "",
18
- "bottom-mid": "",
19
- "bottom-left": "",
20
- "bottom-right": "",
21
- left: "",
22
- "left-mid": "",
23
- mid: "",
24
- "mid-mid": "",
25
- right: "",
26
- "right-mid": "",
27
- middle: " ",
28
- };
11
+ /*
12
+ 获取字符串 brotli 压缩后大小
13
+ 参数: str 待压缩字符串
14
+ 返回值: 字节数
15
+ */
16
+ br = async (str) => (await BROTLI(utf8e(str))).length;
29
17
 
30
- // dir 目录路径。返回整体打包压缩后大小
18
+ /*
19
+ 打包并计算指定目录下 JS 文件压缩后大小
20
+ 参数: dir 目录路径
21
+ 返回值: 整体打包压缩大小 (字节)
22
+ */
31
23
  export default async (dir) => {
32
- // 收集并排序所有 js 文件
33
- const files = [];
34
- await walkRelIgnore(dir, (kind, rel_path) => {
35
- if (kind === FILE && rel_path.endsWith(".js")) {
24
+ const real_dir = realpathSync(dir),
25
+ files = [],
26
+ input = {},
27
+ out_map = {};
28
+
29
+ await walkRelIgnore(real_dir, (kind, rel_path) => {
30
+ if (kind === FILE && rel_path.endsWith(".js") && !/(^|\/)tests?(\/|$)/.test(rel_path)) {
36
31
  files.push(rel_path);
37
32
  }
38
33
  });
39
- files.sort();
40
34
 
41
- // 压缩各文件并计算整体打包压缩后大小
42
- const sizes = await Promise.all(
43
- files.map(async (rel_path) => [rel_path, ...(await minify(join(dir, rel_path)))]),
44
- ),
45
- total_size = files.length
46
- ? (await BROTLI(utf8e(sizes.map(([, , mini]) => mini).join("")))).length
47
- : 0,
48
- table = new Table({
49
- chars: NO_BORDER,
50
- style: TABLE_STYLE,
51
- });
35
+ if (!files.length) {
36
+ return 0;
37
+ }
52
38
 
53
- sizes.forEach(([rel_path, size]) => table.push([rel_path, size]));
39
+ files.sort().forEach((rel_path) => {
40
+ const abs = realpathSync(join(real_dir, rel_path));
41
+ input[rel_path.slice(0, -3)] = abs;
42
+ out_map[abs] = abs;
43
+ });
44
+
45
+ const chunks = await bundle(
46
+ input,
47
+ {
48
+ external: (id) => !id.startsWith(".") && !isAbsolute(id),
49
+ },
50
+ true,
51
+ undefined,
52
+ out_map,
53
+ ),
54
+ total_size = await br(chunks.map(([, code]) => code).join(""));
54
55
 
55
- table.push(["整体打包压缩后大小", total_size]);
56
- console.log(table.toString());
57
56
  return total_size;
58
57
  };
package/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env bun
2
2
  import yargs from "yargs";
3
3
  import { hideBin } from "yargs/helpers";
4
4
  import minify from "./_.js";
@@ -16,4 +16,4 @@ const { dir } = yargs(hideBin(process.argv))
16
16
  .help()
17
17
  .alias("h", "help").argv;
18
18
 
19
- await minify(dir);
19
+ console.log(await minify(dir));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1-/minify_size",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Minify JS and output brotli compressed size | 压缩 JS 并输出 brotli 压缩后大小",
5
5
  "keywords": [
6
6
  "br",
@@ -25,11 +25,9 @@
25
25
  "./*": "./*"
26
26
  },
27
27
  "dependencies": {
28
- "@1-/walk": "^0.1.1",
29
- "@3-/read": "^0.1.4",
28
+ "@1-/rolldown": "^0.1.7",
29
+ "@1-/walk": "^0.1.2",
30
30
  "@3-/utf8": "^0.1.1",
31
- "cli-table3": "^0.6.5",
32
- "oxc-minify": "^0.135.0",
33
31
  "yargs": "^18.0.0"
34
32
  }
35
33
  }
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
- };