@1-/minify_size 0.1.6 → 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 +24 -16
  2. package/_.js +27 -19
  3. package/cli.js +8 -8
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  ## 1. Functionality
18
18
 
19
- Measure JavaScript library transmission size under Brotli-enabled network environments. Processes all `.js` files in a specified directory by:
19
+ Measure JavaScript code transmission size under Brotli-enabled network environments. Supports specifying either a single JavaScript file or directory, performing:
20
20
 
21
21
  - Bundling with `@1-/rolldown` (Rust-based JavaScript bundler)
22
22
  - UTF-8 encoding using `@3-/utf8` TextEncoder
@@ -39,9 +39,13 @@ Install globally:
39
39
  npm install -g @1-/minify_size
40
40
  ```
41
41
 
42
- Execute with target directory:
42
+ Execute with target file or directory:
43
43
 
44
44
  ```bash
45
+ # Process single file
46
+ minify_size ./src/index.js
47
+
48
+ # Process entire directory
45
49
  minify_size ./src
46
50
  ```
47
51
 
@@ -55,7 +59,7 @@ Output example:
55
59
 
56
60
  Execution flow (vertical Mermaid diagram):
57
61
 
58
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@al/eo5PAnMMwP9cRbLWbHQg.svg)
62
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@v8/w24sosbAjA0LSvPhfzpA.svg)
59
63
 
60
64
  ## 4. Technology Stack
61
65
 
@@ -72,8 +76,8 @@ Execution flow (vertical Mermaid diagram):
72
76
 
73
77
  ```
74
78
  src/
75
- ├── cli.js # CLI entrypoint, parses directory parameter and invokes main function
76
- └── _.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
77
81
  ```
78
82
 
79
83
  ## 6. History
@@ -93,17 +97,17 @@ This library is developed by [WebC.site](https://webc.site).
93
97
  # @1-/minify_size : Minify JavaScript and report Brotli-compressed size
94
98
 
95
99
  - [@1-/minify_size : Minify JavaScript and report Brotli-compressed size](#1-minify_size-minify-javascript-and-report-brotli-compressed-size)
96
- - [1. 功能性](#1-功能性)
97
- - [2. 使用方法](#2-使用方法)
100
+ - [1. 功能介绍](#1-功能介绍)
101
+ - [2. 使用演示](#2-使用演示)
98
102
  - [3. 设计思路](#3-设计思路)
99
103
  - [4. 技术栈](#4-技术栈)
100
104
  - [5. 代码结构](#5-代码结构)
101
- - [6. 历史背景](#6-历史背景)
105
+ - [6. 历史故事](#6-历史故事)
102
106
  - [关于](#关于)
103
107
 
104
- ## 1. 功能性
108
+ ## 1. 功能介绍
105
109
 
106
- 测量 JavaScript 库在支持 Brotli 的网络环境中的传输体积。对指定目录中所有 `.js` 文件执行以下操作:
110
+ 测量 JavaScript 代码在支持 Brotli 的网络环境中的传输体积。支持指定单个 JavaScript 文件或目录,执行以下操作:
107
111
 
108
112
  - 使用 `@1-/rolldown`(Rust 实现的 JavaScript 打包器)进行打包
109
113
  - 使用 `@3-/utf8` TextEncoder 进行 UTF-8 编码
@@ -112,7 +116,7 @@ This library is developed by [WebC.site](https://webc.site).
112
116
 
113
117
  排除匹配 `/^(|\/)tests?(\/|$)/` 的测试文件和 `node_modules` 目录。
114
118
 
115
- ## 2. 使用方法
119
+ ## 2. 使用演示
116
120
 
117
121
  本地安装:
118
122
 
@@ -126,9 +130,13 @@ npm install @1-/minify_size
126
130
  npm install -g @1-/minify_size
127
131
  ```
128
132
 
129
- 执行命令并指定目标目录:
133
+ 执行命令并指定目标文件或目录:
130
134
 
131
135
  ```bash
136
+ # 处理单个文件
137
+ minify_size ./src/index.js
138
+
139
+ # 处理整个目录
132
140
  minify_size ./src
133
141
  ```
134
142
 
@@ -142,7 +150,7 @@ minify_size ./src
142
150
 
143
151
  执行流程(垂直 Mermaid 流程图):
144
152
 
145
- ![](https://fastly.jsdelivr.net/gh/webc-fs/-@yh/rGQ5Y3ZbsLqLF6zENpxQ.svg)
153
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@j6/mgP5-ZcSQPBbhsd-KNOA.svg)
146
154
 
147
155
  ## 4. 技术栈
148
156
 
@@ -159,11 +167,11 @@ minify_size ./src
159
167
 
160
168
  ```
161
169
  src/
162
- ├── cli.js # CLI 入口,解析目录参数并调用主函数
163
- └── _.js # 目录遍历、打包处理、Brotli 压缩计算
170
+ ├── cli.js # CLI 入口,解析路径参数并调用主函数
171
+ └── _.js # 路径处理、打包、Brotli 压缩计算
164
172
  ```
165
173
 
166
- ## 6. 历史背景
174
+ ## 6. 历史故事
167
175
 
168
176
  Brotli 由 Google 的 Jyrki Alakuijala 和 Zoltán Szabadka 于 2013 年开发。最初专为网页字体压缩设计,后发展为通用压缩算法,针对网页传输优化,并成为行业标准(RFC 7932)。现代 JavaScript 打包器如 rolldown 利用 Rust 的性能优势实现亚秒级构建,同时保持与现有 JavaScript 工具生态的兼容性。
169
177
 
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
- real_dir,
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.6",
3
+ "version": "0.1.7",
4
4
  "description": "Minify JS and output brotli compressed size | 压缩 JS 并输出 brotli 压缩后大小",
5
5
  "keywords": [
6
6
  "br",