@1-/minify_size 0.1.2 → 0.1.3

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 (4) hide show
  1. package/README.md +30 -20
  2. package/_.js +44 -31
  3. package/file.js +8 -8
  4. package/package.json +2 -1
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
+ - Syntax-aware minification using `oxc-minify` (Rust implementation)
22
+ - UTF-8 encoding of the minified code
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
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,16 +46,16 @@ minify_size ./src
42
46
  Example output:
43
47
 
44
48
  ```
45
- _.js 400
46
- file.js 250
47
- Total 650
49
+ _.js 400
50
+ file.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/-@AI/_OWRSd8Rk6TslPIF1blg.svg)
55
59
 
56
60
  ## 4. Tech Stack
57
61
 
@@ -78,6 +82,7 @@ src/
78
82
 
79
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).
80
84
 
85
+
81
86
  ## About
82
87
 
83
88
  This library is developed by [WebC.site](https://webc.site).
@@ -88,9 +93,9 @@ This library is developed by [WebC.site](https://webc.site).
88
93
  ---
89
94
 
90
95
  <a id="zh"></a>
91
- # @1-/minify_size : Minify JS and output brotli compressed size
96
+ # @1-/minify_size : Minify JavaScript and report Brotli-compressed size
92
97
 
93
- - [@1-/minify_size : Minify JS and output brotli compressed size](#1-minify_size-minify-js-and-output-brotli-compressed-size)
98
+ - [@1-/minify_size : Minify JavaScript and report Brotli-compressed size](#1-minify_size-minify-javascript-and-report-brotli-compressed-size)
94
99
  - [1. 功能介绍](#1-功能介绍)
95
100
  - [2. 使用演示](#2-使用演示)
96
101
  - [3. 设计思路](#3-设计思路)
@@ -101,8 +106,12 @@ This library is developed by [WebC.site](https://webc.site).
101
106
 
102
107
  ## 1. 功能介绍
103
108
 
104
- 压缩指定目录中的 JavaScript 文件,并计算经 Brotli 算法压缩后的体积。
105
- 用于评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。
109
+ 评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。对指定目录中所有 `.js` 文件执行以下操作:
110
+
111
+ - 使用 `oxc-minify`(Rust 实现)进行语法感知压缩
112
+ - 将压缩后代码编码为 UTF-8 字节流
113
+ - 使用 Node.js 内置 `node:zlib.brotliCompress` 计算 Brotli 压缩后字节长度
114
+ - 汇总各文件大小,并计算整体打包压缩后大小
106
115
 
107
116
  ## 2. 使用演示
108
117
 
@@ -118,7 +127,7 @@ npm install @1-/minify_size
118
127
  npm install -g @1-/minify_size
119
128
  ```
120
129
 
121
- 运行命令:
130
+ 运行命令(指定待分析的目录):
122
131
 
123
132
  ```bash
124
133
  minify_size ./src
@@ -127,16 +136,16 @@ minify_size ./src
127
136
  输出示例:
128
137
 
129
138
  ```
130
- _.js 400
131
- file.js 250
132
- 合计 650
139
+ _.js 400
140
+ file.js 250
141
+ 整体打包压缩后大小 650
133
142
  ```
134
143
 
135
144
  ## 3. 设计思路
136
145
 
137
- 系统执行流程如下:
146
+ 系统执行流程如下(垂直 Mermaid 流程图):
138
147
 
139
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@Eg/nKfDG6I2Cy7wB6zHgcjA.svg)
148
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@ko/Gf_-AqDa7IeLBsyqvTSg.svg)
140
149
 
141
150
  ## 4. 技术栈
142
151
 
@@ -163,6 +172,7 @@ src/
163
172
 
164
173
  Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。它最初被设计用于压缩网页字体,后来发展为通用压缩算法,用于优化网页传输,并成为行业标准(RFC 7932)。
165
174
 
175
+
166
176
  ## 关于
167
177
 
168
178
  本库由 [WebC.site](https://webc.site) 开发。
package/_.js CHANGED
@@ -1,45 +1,58 @@
1
- import { readdir } from "node:fs/promises";
2
1
  import { join } from "node:path";
2
+ import { promisify } from "node:util";
3
+ import { brotliCompress } from "node:zlib";
3
4
  import Table from "cli-table3";
5
+ import { FILE } from "@1-/walk";
6
+ import walkRelIgnore from "@1-/walk/walkRelIgnore.js";
7
+ import utf8e from "@3-/utf8";
4
8
  import minify from "./file.js";
5
9
 
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
- };
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
+ };
23
29
 
30
+ // dir 目录路径。返回整体打包压缩后大小
24
31
  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
- }),
32
+ // 收集并排序所有 js 文件
33
+ const files = [];
34
+ await walkRelIgnore(dir, (kind, rel_path) => {
35
+ if (kind === FILE && rel_path.endsWith(".js")) {
36
+ files.push(rel_path);
37
+ }
38
+ });
39
+ files.sort();
40
+
41
+ // 压缩各文件并计算整体打包压缩后大小
42
+ const sizes = await Promise.all(
43
+ files.map(async (rel_path) => [rel_path, ...(await minify(join(dir, rel_path)))]),
33
44
  ),
34
- total = sizes.reduce((acc, [, size]) => acc + size, 0),
45
+ total_size = files.length
46
+ ? (await BROTLI(utf8e(sizes.map(([, , mini]) => mini).join("")))).length
47
+ : 0,
35
48
  table = new Table({
36
49
  chars: NO_BORDER,
37
- style: { "padding-left": 0, "padding-right": 0 },
50
+ style: TABLE_STYLE,
38
51
  });
39
52
 
40
- sizes.forEach(([name, size]) => table.push([name, size]));
53
+ sizes.forEach(([rel_path, size]) => table.push([rel_path, size]));
41
54
 
42
- table.push(["合计", total]);
55
+ table.push(["整体打包压缩后大小", total_size]);
43
56
  console.log(table.toString());
44
- return total;
57
+ return total_size;
45
58
  };
package/file.js CHANGED
@@ -5,15 +5,15 @@ import { brotliCompress } from "node:zlib";
5
5
  import utf8e from "@3-/utf8";
6
6
  import { basename } from "node:path";
7
7
 
8
- const BROTLI = promisify(brotliCompress);
8
+ const BROTLI = promisify(brotliCompress),
9
+ MINIFY_OPT = {
10
+ compress: { target: "esnext" },
11
+ codegen: { removeWhitespace: true },
12
+ };
9
13
 
10
- // file_path: 文件路径;返回 minify 且 brotli 压缩后的字节数
14
+ // file_path 压缩文件路径。返回 [brotli压缩后大小, 压缩后代码]
11
15
  export default async (file_path) => {
12
16
  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;
17
+ { code: mini } = minifySync(basename(file_path), code, MINIFY_OPT);
18
+ return [(await BROTLI(utf8e(mini))).length, mini];
19
19
  };
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.3",
4
4
  "description": "Minify JS and output brotli compressed size | 压缩 JS 并输出 brotli 压缩后大小",
5
5
  "keywords": [
6
6
  "br",
@@ -25,6 +25,7 @@
25
25
  "./*": "./*"
26
26
  },
27
27
  "dependencies": {
28
+ "@1-/walk": "^0.1.1",
28
29
  "@3-/read": "^0.1.4",
29
30
  "@3-/utf8": "^0.1.1",
30
31
  "cli-table3": "^0.6.5",