@1-/minify_size 0.1.5 → 0.1.7

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 +59 -47
  2. package/_.js +27 -19
  3. package/cli.js +8 -8
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -6,77 +6,83 @@
6
6
  # @1-/minify_size : Minify JavaScript and report Brotli-compressed size
7
7
 
8
8
  - [@1-/minify_size : Minify JavaScript and report Brotli-compressed size](#1-minify_size-minify-javascript-and-report-brotli-compressed-size)
9
- - [1. Introduction](#1-introduction)
10
- - [2. Usage Demo](#2-usage-demo)
11
- - [3. Design Concept](#3-design-concept)
12
- - [4. Tech Stack](#4-tech-stack)
9
+ - [1. Functionality](#1-functionality)
10
+ - [2. Usage](#2-usage)
11
+ - [3. Design](#3-design)
12
+ - [4. Technology Stack](#4-technology-stack)
13
13
  - [5. Code Structure](#5-code-structure)
14
14
  - [6. History](#6-history)
15
15
  - [About](#about)
16
16
 
17
- ## 1. Introduction
17
+ ## 1. Functionality
18
18
 
19
- Evaluates JavaScript library size under modern network transmission environments supporting Brotli. For all `.js` files in the specified directory, performs:
19
+ Measure JavaScript code transmission size under Brotli-enabled network environments. Supports specifying either a single JavaScript file or directory, performing:
20
20
 
21
- - Bundling using `@1-/rolldown` (Rust-based JavaScript bundler)
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
- - Returns total bundled compressed size (bytes)
21
+ - Bundling with `@1-/rolldown` (Rust-based JavaScript bundler)
22
+ - UTF-8 encoding using `@3-/utf8` TextEncoder
23
+ - Brotli compression via Node.js built-in `node:zlib.brotliCompress`
24
+ - Returning total compressed byte count of bundled output
25
25
 
26
- ## 2. Usage Demo
26
+ Excludes test files matching `/^(|\/)tests?(\/|$)/` and `node_modules` directories.
27
27
 
28
- Install dependency:
28
+ ## 2. Usage
29
+
30
+ Install locally:
29
31
 
30
32
  ```bash
31
33
  npm install @1-/minify_size
32
34
  ```
33
35
 
34
- or install globally:
36
+ Install globally:
35
37
 
36
38
  ```bash
37
39
  npm install -g @1-/minify_size
38
40
  ```
39
41
 
40
- Run command (specify the directory to analyze):
42
+ Execute with target file or directory:
41
43
 
42
44
  ```bash
45
+ # Process single file
46
+ minify_size ./src/index.js
47
+
48
+ # Process entire directory
43
49
  minify_size ./src
44
50
  ```
45
51
 
46
- Example output:
52
+ Output example:
47
53
 
48
54
  ```
49
55
  650
50
56
  ```
51
57
 
52
- ## 3. Design Concept
58
+ ## 3. Design
53
59
 
54
60
  Execution flow (vertical Mermaid diagram):
55
61
 
56
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@D7/nQLapVbeR_9qMiOldgug.svg)
62
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@v8/w24sosbAjA0LSvPhfzpA.svg)
57
63
 
58
- ## 4. Tech Stack
64
+ ## 4. Technology Stack
59
65
 
60
- - **Runtime**: Node.js / Bun
66
+ - **Runtime**: Bun / Node.js
61
67
  - **Bundler**: `@1-/rolldown` v0.1.7 (Rust-based JavaScript bundler)
62
- - **Brotli Engine**: Built-in `node:zlib` (Brotli compression)
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)
66
- - **Dependency Management**: npm
68
+ - **Compression**: `node:zlib.brotliCompress` (built-in Brotli)
69
+ - **Argument parsing**: `yargs` v18.0.0
70
+ - **Encoding**: `@3-/utf8` v0.1.1 (TextEncoder-based UTF-8)
71
+ - **File traversal**: `@1-/walk` v0.1.2 (directory traversal utility)
72
+ - **Package management**: npm
67
73
  - **Testing**: bun:test
68
74
 
69
75
  ## 5. Code Structure
70
76
 
71
77
  ```
72
78
  src/
73
- ├── cli.js # CLI entrypoint, parses directory parameter and invokes main function
74
- └── _.js # Directory traversal, bundling, Brotli compression calculation
79
+ ├── cli.js # CLI entrypoint, parses path parameter and invokes main function
80
+ └── _.js # Path handling, bundling, Brotli compression calculation
75
81
  ```
76
82
 
77
83
  ## 6. History
78
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
+ Brotli was developed by Jyrki Alakuijala and Zoltán Szabadka at Google in 2013. Initially designed for web font compression, it evolved into a general-purpose algorithm optimized for web transmission and became an industry standard (RFC 7932). Modern JavaScript bundlers like rolldown leverage Rust's performance for sub-second builds while maintaining compatibility with existing JavaScript tooling ecosystems.
80
86
 
81
87
  ## About
82
88
 
@@ -101,30 +107,36 @@ This library is developed by [WebC.site](https://webc.site).
101
107
 
102
108
  ## 1. 功能介绍
103
109
 
104
- 评估 JavaScript 库在支持 Brotli 的网络传输环境下的实际传输体积。对指定目录中所有 `.js` 文件执行以下操作:
110
+ 测量 JavaScript 代码在支持 Brotli 的网络环境中的传输体积。支持指定单个 JavaScript 文件或目录,执行以下操作:
105
111
 
106
112
  - 使用 `@1-/rolldown`(Rust 实现的 JavaScript 打包器)进行打包
107
- - 将打包后代码编码为 UTF-8 字节流
108
- - 使用 Node.js 内置 `node:zlib.brotliCompress` 计算 Brotli 压缩后字节长度
109
- - 返回整体打包压缩后大小(字节)
113
+ - 使用 `@3-/utf8` TextEncoder 进行 UTF-8 编码
114
+ - 使用 Node.js 内置 `node:zlib.brotliCompress` 进行 Brotli 压缩
115
+ - 返回整体打包输出的压缩后字节长度
116
+
117
+ 排除匹配 `/^(|\/)tests?(\/|$)/` 的测试文件和 `node_modules` 目录。
110
118
 
111
119
  ## 2. 使用演示
112
120
 
113
- 安装依赖:
121
+ 本地安装:
114
122
 
115
123
  ```bash
116
124
  npm install @1-/minify_size
117
125
  ```
118
126
 
119
- 或全局安装:
127
+ 全局安装:
120
128
 
121
129
  ```bash
122
130
  npm install -g @1-/minify_size
123
131
  ```
124
132
 
125
- 运行命令(指定待分析的目录):
133
+ 执行命令并指定目标文件或目录:
126
134
 
127
135
  ```bash
136
+ # 处理单个文件
137
+ minify_size ./src/index.js
138
+
139
+ # 处理整个目录
128
140
  minify_size ./src
129
141
  ```
130
142
 
@@ -136,32 +148,32 @@ minify_size ./src
136
148
 
137
149
  ## 3. 设计思路
138
150
 
139
- 系统执行流程如下(垂直 Mermaid 流程图):
151
+ 执行流程(垂直 Mermaid 流程图):
140
152
 
141
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@Qx/di2QsMfRjPdfYr2IasNQ.svg)
153
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@j6/mgP5-ZcSQPBbhsd-KNOA.svg)
142
154
 
143
155
  ## 4. 技术栈
144
156
 
145
- - **Runtime**: Node.js / Bun
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)
151
- - **Dependency Management**: npm
152
- - **Testing**: bun:test
157
+ - **运行时**: Bun / Node.js
158
+ - **打包器**: `@1-/rolldown` v0.1.7 (Rust 实现的 JavaScript 打包器)
159
+ - **压缩算法**: `node:zlib.brotliCompress` (内置 Brotli)
160
+ - **参数解析**: `yargs` v18.0.0
161
+ - **编码**: `@3-/utf8` v0.1.1 (TextEncoder 实现的 UTF-8)
162
+ - **文件遍历**: `@1-/walk` v0.1.2 (目录遍历工具)
163
+ - **包管理**: npm
164
+ - **测试**: bun:test
153
165
 
154
166
  ## 5. 代码结构
155
167
 
156
168
  ```
157
169
  src/
158
- ├── cli.js # CLI 命令行入口,解析目录参数并调用主函数
159
- └── _.js # 目录遍历、打包处理、Brotli压缩计算
170
+ ├── cli.js # CLI 入口,解析路径参数并调用主函数
171
+ └── _.js # 路径处理、打包、Brotli 压缩计算
160
172
  ```
161
173
 
162
174
  ## 6. 历史故事
163
175
 
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.
176
+ Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。最初专为网页字体压缩设计,后发展为通用压缩算法,针对网页传输优化,并成为行业标准(RFC 7932)。现代 JavaScript 打包器如 rolldown 利用 Rust 的性能优势实现亚秒级构建,同时保持与现有 JavaScript 工具生态的兼容性。
165
177
 
166
178
  ## 关于
167
179
 
package/_.js CHANGED
@@ -1,5 +1,5 @@
1
- import { join, isAbsolute } from "node:path";
2
- import { realpathSync } from "node:fs";
1
+ import { join, isAbsolute, dirname, basename } from "node:path";
2
+ import { realpathSync, statSync } from "node:fs";
3
3
  import { promisify } from "node:util";
4
4
  import { brotliCompress } from "node:zlib";
5
5
  import { FILE } from "@1-/walk";
@@ -16,21 +16,30 @@ const BROTLI = promisify(brotliCompress),
16
16
  br = async (str) => (await BROTLI(utf8e(str))).length;
17
17
 
18
18
  /*
19
- 打包并计算指定目录下 JS 文件压缩后大小
20
- 参数: dir 目录路径
19
+ 打包并计算指定目录或 JS 文件压缩后大小
20
+ 参数: dir 目录或文件路径
21
21
  返回值: 整体打包压缩大小 (字节)
22
22
  */
23
23
  export default async (dir) => {
24
- const real_dir = realpathSync(dir),
24
+ const real_path = realpathSync(dir),
25
+ stat = statSync(real_path),
26
+ is_file = stat.isFile(),
27
+ real_dir = is_file ? dirname(real_path) : real_path,
25
28
  files = [],
26
29
  input = {},
27
30
  out_map = {};
28
31
 
29
- await walkRelIgnore(real_dir, (kind, rel_path) => {
30
- if (kind === FILE && rel_path.endsWith(".js") && !/(^|\/)tests?(\/|$)/.test(rel_path)) {
31
- files.push(rel_path);
32
+ if (is_file) {
33
+ if (real_path.endsWith(".js")) {
34
+ files.push(basename(real_path));
32
35
  }
33
- });
36
+ } else {
37
+ await walkRelIgnore(real_dir, (kind, rel_path) => {
38
+ if (kind === FILE && rel_path.endsWith(".js") && !/(^|\/)tests?(\/|$)/.test(rel_path)) {
39
+ files.push(rel_path);
40
+ }
41
+ });
42
+ }
34
43
 
35
44
  if (!files.length) {
36
45
  return 0;
@@ -43,15 +52,14 @@ export default async (dir) => {
43
52
  });
44
53
 
45
54
  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(""));
55
+ input,
56
+ {
57
+ external: (id) => !id.startsWith(".") && !isAbsolute(id),
58
+ },
59
+ true,
60
+ real_dir,
61
+ out_map,
62
+ );
55
63
 
56
- return total_size;
64
+ return await br(chunks.map(([, code]) => code).join(""));
57
65
  };
package/cli.js CHANGED
@@ -3,17 +3,17 @@ import yargs from "yargs";
3
3
  import { hideBin } from "yargs/helpers";
4
4
  import minify from "./_.js";
5
5
 
6
- // 解析参数并压缩指定目录下所有 JS 文件
7
- const { dir } = yargs(hideBin(process.argv))
8
- .usage("Usage: $0 <dir>")
9
- .command("$0 <dir>", "minify js and show brotli size", (y) => {
10
- y.positional("dir", {
11
- describe: "directory to minify",
6
+ // 解析参数并压缩指定目录或 JS 文件
7
+ const { path } = yargs(hideBin(process.argv))
8
+ .usage("Usage: $0 <path>")
9
+ .command("$0 <path>", "minify js and show brotli size", (y) => {
10
+ y.positional("path", {
11
+ describe: "directory or file path to minify",
12
12
  type: "string",
13
13
  });
14
14
  })
15
- .demandCommand(1, "You must specify a directory")
15
+ .demandCommand(1, "You must specify a directory or file path")
16
16
  .help()
17
17
  .alias("h", "help").argv;
18
18
 
19
- console.log(await minify(dir));
19
+ console.log(await minify(path));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1-/minify_size",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Minify JS and output brotli compressed size | 压缩 JS 并输出 brotli 压缩后大小",
5
5
  "keywords": [
6
6
  "br",