@1-/minify_size 0.1.0

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 +192 -0
  2. package/_.js +45 -0
  3. package/cli.js +18 -0
  4. package/file.js +15 -0
  5. package/package.json +37 -0
package/README.md ADDED
@@ -0,0 +1,192 @@
1
+ [English](#en) | [中文](#zh)
2
+
3
+ ---
4
+
5
+ <a id="en"></a>
6
+ # @1-/minify_size : Minify JS and output zstd compressed size
7
+
8
+ - [@1-/minify_size : Minify JS and output zstd compressed size](#1-minify_size-minify-js-and-output-zstd-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)
13
+ - [5. Code Structure](#5-code-structure)
14
+ - [6. History](#6-history)
15
+ - [About](#about)
16
+
17
+ ## 1. Introduction
18
+
19
+ Minifies JS files in a specified directory and calculates their size after zstd compression.
20
+ Evaluates JS library size under modern transmission environments supporting Zstd.
21
+
22
+ ## 2. Usage Demo
23
+
24
+ Install dependency:
25
+
26
+ ```bash
27
+ npm install @1-/minify_size
28
+ ```
29
+
30
+ or install globally:
31
+
32
+ ```bash
33
+ npm install -g @1-/minify_size
34
+ ```
35
+
36
+ Run command:
37
+
38
+ ```bash
39
+ minify_size ./src
40
+ ```
41
+
42
+ Example output:
43
+
44
+ ```
45
+ _.js 400
46
+ file.js 250
47
+ Total 650
48
+ ```
49
+
50
+ ## 3. Design Concept
51
+
52
+ Execution process:
53
+
54
+ ![](https://fastly.jsdelivr.net/gh/webc-fs/-@n-/Jsm3fz4EQSGdooVgJJCw.svg)
55
+
56
+ ## 4. Tech Stack
57
+
58
+ - **Runtime**: Node.js / Bun
59
+ - **JS Minifier**: `oxc-minify` (JavaScript minifier implemented in Rust)
60
+ - **Zstd Engine**: `@mongodb-js/zstd` (Zstandard binding)
61
+ - **Arg Parser**: `yargs`
62
+ - **Encoding**: `@3-/utf8` (TextEncoder-based UTF-8 encoding)
63
+ - **Output Formatting**: `cli-table3` (Formatted tabular output)
64
+ - **File System**: Node.js built-in `fs.promises`
65
+ - **Dependency Management**: npm
66
+ - **Testing**: bun:test
67
+
68
+ ## 5. Code Structure
69
+
70
+ ```
71
+ src/
72
+ ├── 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, zstd compression and size calculation
75
+ ```
76
+
77
+ Key configuration:
78
+ - oxc-minify: `target: "esnext"`, `removeWhitespace: true`
79
+ - zstd compression level: `3` (default)
80
+ - Input validation: Directory parameter required
81
+ - Error handling: Proper exit codes for missing parameters
82
+
83
+ ## 6. History
84
+
85
+ Zstandard (Zstd) was developed by Yann Collet at Facebook in 2015. Yann became interested in data compression while writing games for the HP 48 graphing calculator. He previously developed the LZ4 compression algorithm, which features high decompression speed. Subsequently, he designed Zstd to balance compression ratio and speed, which later became an industry standard (RFC 8478).
86
+
87
+ ## About
88
+
89
+ This library is developed by [WebC.site](https://webc.site).
90
+
91
+ [WebC.site](https://webc.site): A new paradigm of web development for AI
92
+
93
+
94
+ ---
95
+
96
+ <a id="zh"></a>
97
+ # @1-/minify_size : 压缩 JS 并输出 zstd 压缩后大小
98
+
99
+ - [@1-/minify_size : 压缩 JS 并输出 zstd 压缩后大小](#1-minify_size-压缩-js-并输出-zstd-压缩后大小)
100
+ - [1. 功能介绍](#1-功能介绍)
101
+ - [2. 使用演示](#2-使用演示)
102
+ - [3. 设计思路](#3-设计思路)
103
+ - [4. 技术栈](#4-技术栈)
104
+ - [5. 代码结构](#5-代码结构)
105
+ - [6. 历史故事](#6-历史故事)
106
+ - [关于](#关于)
107
+
108
+ ## 1. 功能介绍
109
+
110
+ 对指定目录中的 JS 文件进行压缩,并计算经 zstd 算法压缩后的体积。
111
+ 用于评估 JS 库在支持 Zstd 的传输环境下的网络传输体积指标。
112
+
113
+ ## 2. 使用演示
114
+
115
+ 安装依赖:
116
+
117
+ ```bash
118
+ npm install @1-/minify_size
119
+ ```
120
+
121
+ 或全局安装:
122
+
123
+ ```bash
124
+ npm install -g @1-/minify_size
125
+ ```
126
+
127
+ 运行命令:
128
+
129
+ ```bash
130
+ minify_size ./src
131
+ ```
132
+
133
+ 输出示例:
134
+
135
+ ```
136
+ _.js 400
137
+ file.js 250
138
+ 合计 650
139
+ ```
140
+
141
+ ## 3. 设计思路
142
+
143
+ 系统执行流程如下:
144
+
145
+ ```mermaid
146
+ graph TD
147
+ A[CLI 输入目录] --> B[读取目录下所有 JS 文件]
148
+ B --> C[并发处理每个文件]
149
+ C --> D[使用 oxc-minify 进行压缩]
150
+ D --> E[将压缩代码转换为 UTF-8 编码]
151
+ E --> F[通过 zstd 算法压缩 level 3]
152
+ F --> G[计算压缩后字节长度]
153
+ G --> H[使用 cli-table3 格式化输出结果]
154
+ ```
155
+
156
+ ## 4. 技术栈
157
+
158
+ - **Runtime**: Node.js / Bun
159
+ - **JS Minifier**: `oxc-minify` (Rust 实现的 JavaScript 压缩器)
160
+ - **Zstd Engine**: `@mongodb-js/zstd` (Zstandard 绑定实现)
161
+ - **Arg Parser**: `yargs`
162
+ - **Encoding**: `@3-/utf8` (TextEncoder-based UTF-8 encoding)
163
+ - **Output Formatting**: `cli-table3` (Formatted tabular output)
164
+ - **File System**: Node.js built-in `fs.promises`
165
+ - **Dependency Management**: npm
166
+ - **Testing**: bun:test
167
+
168
+ ## 5. 代码结构
169
+
170
+ ```
171
+ src/
172
+ ├── cli.js # CLI 命令行入口,解析目录参数并调用主函数
173
+ ├── _.js # 目录遍历、并发调度文件处理、汇总统计并格式化输出
174
+ └── file.js # 单文件处理:读取、oxc-minify 压缩、zstd 压缩及大小计算
175
+ ```
176
+
177
+ 核心配置:
178
+ - oxc-minify: `target: "esnext"`, `removeWhitespace: true`
179
+ - zstd compression level: `3` (default)
180
+ - Input validation: Directory parameter required
181
+ - Error handling: Proper exit codes for missing parameters
182
+
183
+ ## 6. 历史故事
184
+
185
+ Zstandard(Zstd)由 Yann Collet 于 2015 年在 Facebook 主导开发。Yann 在为 HP 48 图形计算器编写游戏时对数据压缩产生兴趣。他此前开发了 LZ4 压缩算法,其解压速度极快。此后,他设计出 Zstd,均衡了压缩率与运行速度,并成为行业标准(RFC 8478)。
186
+
187
+ ## 关于
188
+
189
+ 本库由 [WebC.site](https://webc.site) 开发。
190
+
191
+ [WebC.site](https://webc.site) : 面向人工智能的网站开发新范式
192
+
package/_.js ADDED
@@ -0,0 +1,45 @@
1
+ import { readdir } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import Table from "cli-table3";
4
+ import fileMinify from "./file.js";
5
+
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
+ };
23
+
24
+ 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 fileMinify(file_path);
31
+ return [name, size];
32
+ }),
33
+ ),
34
+ table = new Table({
35
+ chars: NO_BORDER,
36
+ style: { "padding-left": 0, "padding-right": 0 },
37
+ });
38
+
39
+ sizes.forEach(([name, size]) => table.push([name, size]));
40
+
41
+ const total = sizes.reduce((acc, [, size]) => acc + size, 0);
42
+ table.push(["合计", total]);
43
+ console.log(table.toString());
44
+ return total;
45
+ };
package/cli.js ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ import yargs from "yargs";
3
+ import { hideBin } from "yargs/helpers";
4
+ import minify from "./_.js";
5
+
6
+ const { dir } = yargs(hideBin(process.argv))
7
+ .usage("Usage: $0 <dir>")
8
+ .command("$0 <dir>", "minify js and show zstd size", (y) => {
9
+ y.positional("dir", {
10
+ describe: "directory to minify",
11
+ type: "string",
12
+ });
13
+ })
14
+ .demandCommand(1, "You must specify a directory")
15
+ .help()
16
+ .alias("h", "help").argv;
17
+
18
+ await minify(dir);
package/file.js ADDED
@@ -0,0 +1,15 @@
1
+ import read from "@3-/read";
2
+ import { minifySync } from "oxc-minify";
3
+ import { compress } from "@mongodb-js/zstd";
4
+ import utf8e from "@3-/utf8";
5
+ import { basename } from "node:path";
6
+
7
+ export default async (file_path) => {
8
+ const code = read(file_path),
9
+ { code: mini } = minifySync(basename(file_path), code, {
10
+ compress: { target: "esnext" },
11
+ codegen: { removeWhitespace: true },
12
+ }),
13
+ zip = await compress(utf8e(mini), 3);
14
+ return zip.length;
15
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@1-/minify_size",
3
+ "version": "0.1.0",
4
+ "description": "Minify JS and output zstd compressed size | 压缩 JS 并输出 zstd 压缩后大小",
5
+ "keywords": [
6
+ "compression",
7
+ "minify",
8
+ "size",
9
+ "zstd"
10
+ ],
11
+ "homepage": "https://github.com/webc-site/npm/tree/main/minify_size",
12
+ "license": "MulanPSL-2.0",
13
+ "author": "x-at-01@googlegroups.com",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/webc-site/npm.git"
17
+ },
18
+ "bin": {
19
+ "minify_size": "./cli.js"
20
+ },
21
+ "type": "module",
22
+ "exports": {
23
+ ".": "./_.js",
24
+ "./*": "./*"
25
+ },
26
+ "dependencies": {
27
+ "@3-/read": "^0.1.4",
28
+ "@3-/utf8": "^0.1.1",
29
+ "@mongodb-js/zstd": "^7.0.0",
30
+ "cli-table3": "^0.6.5",
31
+ "oxc-minify": "^0.135.0",
32
+ "yargs": "^18.0.0"
33
+ },
34
+ "trustedDependencies": [
35
+ "@mongodb-js/zstd"
36
+ ]
37
+ }