@1-/minify_size 0.1.2 → 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.
Files changed (5) hide show
  1. package/README.md +42 -36
  2. package/_.js +84 -38
  3. package/cli.js +1 -1
  4. package/package.json +3 -3
  5. package/file.js +0 -19
package/README.md CHANGED
@@ -3,9 +3,9 @@
3
3
  ---
4
4
 
5
5
  <a id="en"></a>
6
- # @1-/minify_size : Minify JS and output brotli compressed size
6
+ # @1-/minify_size : Minify JavaScript and report Brotli-compressed size
7
7
 
8
- - [@1-/minify_size : Minify JS and output brotli compressed size](#1-minify_size-minify-js-and-output-brotli-compressed-size)
8
+ - [@1-/minify_size : Minify JavaScript and report Brotli-compressed size](#1-minify_size-minify-javascript-and-report-brotli-compressed-size)
9
9
  - [1. Introduction](#1-introduction)
10
10
  - [2. Usage Demo](#2-usage-demo)
11
11
  - [3. Design Concept](#3-design-concept)
@@ -16,8 +16,12 @@
16
16
 
17
17
  ## 1. Introduction
18
18
 
19
- Minifies JavaScript files in a specified directory and calculates their size after Brotli compression.
20
- Evaluates JavaScript library size under modern network transmission environments supporting Brotli.
19
+ Evaluates JavaScript library size under modern network transmission environments supporting Brotli. For all `.js` files in the specified directory, performs:
20
+
21
+ - Bundling using `@1-/rolldown` (Rust-based JavaScript bundler wrapping rolldown v1.1.3)
22
+ - UTF-8 encoding of the bundled code
23
+ - Brotli compression via Node.js built-in `node:zlib.brotliCompress` to compute final byte length
24
+ - Aggregation of per-bundle sizes and calculation of total bundled compressed size
21
25
 
22
26
  ## 2. Usage Demo
23
27
 
@@ -33,7 +37,7 @@ or install globally:
33
37
  npm install -g @1-/minify_size
34
38
  ```
35
39
 
36
- Run command:
40
+ Run command (specify the directory to analyze):
37
41
 
38
42
  ```bash
39
43
  minify_size ./src
@@ -42,26 +46,26 @@ minify_size ./src
42
46
  Example output:
43
47
 
44
48
  ```
45
- _.js 400
46
- file.js 250
47
- Total 650
49
+ index.js 400
50
+ utils.js 250
51
+ Total bundled size 650
48
52
  ```
49
53
 
50
54
  ## 3. Design Concept
51
55
 
52
- Execution process:
56
+ Execution flow (vertical Mermaid diagram):
53
57
 
54
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@RL/NUbVPqfWeEufBqPl_sqg.svg)
58
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@mF/8IyEaPelxy8ant9q1lyA.svg)
55
59
 
56
60
  ## 4. Tech Stack
57
61
 
58
62
  - **Runtime**: Node.js / Bun
59
- - **JS Minifier**: `oxc-minify` (JavaScript minifier implemented in Rust)
63
+ - **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler wrapping rolldown v1.1.3)
60
64
  - **Brotli Engine**: Built-in `node:zlib` (Brotli compression)
61
- - **Arg Parser**: `yargs`
62
- - **Encoding**: `@3-/utf8` (TextEncoder-based UTF-8 encoding)
63
- - **Output Formatting**: `cli-table3` (Formatted tabular output)
64
- - **File Reading**: `@3-/read` (Lightweight file reading utility)
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)
65
69
  - **Dependency Management**: npm
66
70
  - **Testing**: bun:test
67
71
 
@@ -70,13 +74,12 @@ Execution process:
70
74
  ```
71
75
  src/
72
76
  ├── cli.js # CLI entrypoint, parses directory parameter and invokes main function
73
- ├── _.js # Directory traversal, concurrent file processing, aggregation and formatted output
74
- └── 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
75
78
  ```
76
79
 
77
80
  ## 6. History
78
81
 
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).
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.
80
83
 
81
84
  ## About
82
85
 
@@ -88,9 +91,9 @@ This library is developed by [WebC.site](https://webc.site).
88
91
  ---
89
92
 
90
93
  <a id="zh"></a>
91
- # @1-/minify_size : Minify JS and output brotli compressed size
94
+ # @1-/minify_size : Minify JavaScript and report Brotli-compressed size
92
95
 
93
- - [@1-/minify_size : Minify JS and output brotli compressed size](#1-minify_size-minify-js-and-output-brotli-compressed-size)
96
+ - [@1-/minify_size : Minify JavaScript and report Brotli-compressed size](#1-minify_size-minify-javascript-and-report-brotli-compressed-size)
94
97
  - [1. 功能介绍](#1-功能介绍)
95
98
  - [2. 使用演示](#2-使用演示)
96
99
  - [3. 设计思路](#3-设计思路)
@@ -101,8 +104,12 @@ This library is developed by [WebC.site](https://webc.site).
101
104
 
102
105
  ## 1. 功能介绍
103
106
 
104
- 压缩指定目录中的 JavaScript 文件,并计算经 Brotli 算法压缩后的体积。
105
- 用于评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。
107
+ 评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。对指定目录中所有 `.js` 文件执行以下操作:
108
+
109
+ - 使用 `@1-/rolldown`(Rust 实现的 JavaScript 打包器)进行打包和语法感知压缩
110
+ - 将打包后代码编码为 UTF-8 字节流
111
+ - 使用 Node.js 内置 `node:zlib.brotliCompress` 计算 Brotli 压缩后字节长度
112
+ - 汇总各打包产物大小,并计算整体打包压缩后大小
106
113
 
107
114
  ## 2. 使用演示
108
115
 
@@ -118,7 +125,7 @@ npm install @1-/minify_size
118
125
  npm install -g @1-/minify_size
119
126
  ```
120
127
 
121
- 运行命令:
128
+ 运行命令(指定待分析的目录):
122
129
 
123
130
  ```bash
124
131
  minify_size ./src
@@ -127,26 +134,26 @@ minify_size ./src
127
134
  输出示例:
128
135
 
129
136
  ```
130
- _.js 400
131
- file.js 250
132
- 合计 650
137
+ index.js 400
138
+ utils.js 250
139
+ 整体打包压缩后大小 650
133
140
  ```
134
141
 
135
142
  ## 3. 设计思路
136
143
 
137
- 系统执行流程如下:
144
+ 系统执行流程如下(垂直 Mermaid 流程图):
138
145
 
139
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@Eg/nKfDG6I2Cy7wB6zHgcjA.svg)
146
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@Cg/dFGhNxdolTp2uwIBl2vg.svg)
140
147
 
141
148
  ## 4. 技术栈
142
149
 
143
150
  - **Runtime**: Node.js / Bun
144
- - **JS Minifier**: `oxc-minify` (Rust 实现的 JavaScript 压缩器)
151
+ - **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler wrapping rolldown v1.1.3)
145
152
  - **Brotli Engine**: 内置 `node:zlib` (Brotli 压缩)
146
- - **Arg Parser**: `yargs`
147
- - **Encoding**: `@3-/utf8` (TextEncoder-based UTF-8 encoding)
148
- - **Output Formatting**: `cli-table3` (Formatted tabular output)
149
- - **File Reading**: `@3-/read` (Lightweight file reading utility)
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)
150
157
  - **Dependency Management**: npm
151
158
  - **Testing**: bun:test
152
159
 
@@ -155,13 +162,12 @@ file.js 250
155
162
  ```
156
163
  src/
157
164
  ├── cli.js # CLI 命令行入口,解析目录参数并调用主函数
158
- ├── _.js # 目录遍历、并发调度文件处理、汇总统计并格式化输出
159
- └── file.js # 单文件处理:读取、oxc-minify 压缩、brotli 压缩及大小计算
165
+ └── _.js # 目录遍历、打包处理、Brotli压缩计算及格式化输出
160
166
  ```
161
167
 
162
168
  ## 6. 历史故事
163
169
 
164
- Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。它最初被设计用于压缩网页字体,后来发展为通用压缩算法,用于优化网页传输,并成为行业标准(RFC 7932)。
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.
165
171
 
166
172
  ## 关于
167
173
 
package/_.js CHANGED
@@ -1,45 +1,91 @@
1
- import { readdir } from "node:fs/promises";
2
- import { join } from "node:path";
1
+ import { relative, join } from "node:path";
2
+ import { realpathSync } from "node:fs";
3
+ import { promisify } from "node:util";
4
+ import { brotliCompress } from "node:zlib";
3
5
  import Table from "cli-table3";
4
- import minify from "./file.js";
6
+ import { FILE } from "@1-/walk";
7
+ import walkRelIgnore from "@1-/walk/walkRelIgnore.js";
8
+ import utf8e from "@3-/utf8";
9
+ import bundle from "@1-/rolldown";
5
10
 
6
- const NO_BORDER = {
7
- top: "",
8
- "top-mid": "",
9
- "top-left": "",
10
- "top-right": "",
11
- bottom: "",
12
- "bottom-mid": "",
13
- "bottom-left": "",
14
- "bottom-right": "",
15
- left: "",
16
- "left-mid": "",
17
- mid: "",
18
- "mid-mid": "",
19
- right: "",
20
- "right-mid": "",
21
- middle: " ",
22
- };
11
+ const BROTLI = promisify(brotliCompress),
12
+ TABLE_STYLE = { "padding-left": 0, "padding-right": 0 },
13
+ NO_BORDER = {
14
+ top: "",
15
+ "top-mid": "",
16
+ "top-left": "",
17
+ "top-right": "",
18
+ bottom: "",
19
+ "bottom-mid": "",
20
+ "bottom-left": "",
21
+ "bottom-right": "",
22
+ left: "",
23
+ "left-mid": "",
24
+ mid: "",
25
+ "mid-mid": "",
26
+ right: "",
27
+ "right-mid": "",
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());
47
+ };
23
48
 
49
+ /*
50
+ 打包并计算指定目录下 JS 文件压缩后大小
51
+ 参数: dir 目录路径
52
+ 返回值: 整体打包压缩大小 (字节)
53
+ */
24
54
  export default async (dir) => {
25
- const files = await readdir(dir, { withFileTypes: true }),
26
- js = files.filter((file) => file.isFile() && file.name.endsWith(".js")),
27
- sizes = await Promise.all(
28
- js.map(async ({ name }) => {
29
- const file_path = join(dir, name),
30
- size = await minify(file_path);
31
- return [name, size];
32
- }),
33
- ),
34
- total = sizes.reduce((acc, [, size]) => acc + size, 0),
35
- table = new Table({
36
- chars: NO_BORDER,
37
- style: { "padding-left": 0, "padding-right": 0 },
38
- });
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)) {
62
+ files.push(rel_path);
63
+ }
64
+ });
65
+
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
+ });
39
76
 
40
- sizes.forEach(([name, size]) => table.push([name, size]));
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);
41
89
 
42
- table.push(["合计", total]);
43
- console.log(table.toString());
44
- return total;
90
+ return total_size;
45
91
  };
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";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1-/minify_size",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Minify JS and output brotli compressed size | 压缩 JS 并输出 brotli 压缩后大小",
5
5
  "keywords": [
6
6
  "br",
@@ -25,10 +25,10 @@
25
25
  "./*": "./*"
26
26
  },
27
27
  "dependencies": {
28
- "@3-/read": "^0.1.4",
28
+ "@1-/rolldown": "^0.1.7",
29
+ "@1-/walk": "^0.1.2",
29
30
  "@3-/utf8": "^0.1.1",
30
31
  "cli-table3": "^0.6.5",
31
- "oxc-minify": "^0.135.0",
32
32
  "yargs": "^18.0.0"
33
33
  }
34
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
-
10
- // file_path: 文件路径;返回 minify 且 brotli 压缩后的字节数
11
- export default async (file_path) => {
12
- const code = read(file_path),
13
- { code: mini } = minifySync(basename(file_path), code, {
14
- compress: { target: "esnext" },
15
- codegen: { removeWhitespace: true },
16
- }),
17
- zip = await BROTLI(utf8e(mini));
18
- return zip.length;
19
- };