zcompress-vite-plugin 0.1.3 → 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.
package/README.md CHANGED
@@ -8,7 +8,7 @@ Vite 生产构建资源压缩插件 — gzip / zstd / brotli,多线程并行
8
8
  npm install zcompress-vite-plugin --save-dev
9
9
  ```
10
10
 
11
- 安装后自动下载对应平台的 zcompress 二进制。如果下载失败,手动安装:
11
+ 安装后会自动下载对应平台的 `zcompress` 二进制。下载失败可手动安装:
12
12
 
13
13
  | 平台 | 命令 |
14
14
  |------|------|
@@ -35,17 +35,7 @@ export default {
35
35
  };
36
36
  ```
37
37
 
38
- `vite build` 后,`dist-compressed/` 目录下就是压缩后的文件:
39
-
40
- ```
41
- dist-compressed/
42
- ├── index.html.gz
43
- ├── style.css.gz
44
- ├── app.js.gz
45
- └── logo.svg.gz
46
- ```
47
-
48
- 配合 Nginx 的 `gzip_static on` 直接使用。
38
+ `vite build` 后,`dist-compressed/` 目录下就是压缩后的文件。
49
39
 
50
40
  ## 选项
51
41
 
@@ -56,13 +46,35 @@ dist-compressed/
56
46
  | `threads` | `number` | `0` | 线程数 (0=CPU 核心数) |
57
47
  | `verbose` | `boolean` | `false` | 显示详细输出 |
58
48
  | `cache` | `boolean` | `false` | 跳过未修改文件 |
59
- | `include` | `string[]` | `[]` | 额外压缩的扩展名 (如 `['.ts']`) |
60
- | `exclude` | `string[]` | `[]` | 排除的扩展名 (如 `['.map']`) |
49
+ | `include` | `string[]` | `[]` | 额外压缩扩展名 |
50
+ | `exclude` | `string[]` | `[]` | 排除扩展名 |
61
51
  | `binaryPath` | `string` | 自动 | 手动指定二进制路径 |
52
+ | `failOnError` | `boolean` | `true` | 压缩失败时让 `vite build` 失败 |
62
53
 
63
- ## 默认压缩的文件类型
54
+ ## 故障排查
55
+
56
+ ### `postinstall` 下载二进制 404
57
+
58
+ 如果看到类似:
59
+
60
+ ```txt
61
+ https://github.com/luochuan2008/zcompress/releases/download/vX.Y.Z/zcompress-macos-arm64 → 404
62
+ ```
64
63
 
65
- `.js` `.mjs` `.cjs` `.css` `.html` `.htm` `.json` `.svg` `.png` `.jpg` `.jpeg` `.gif` `.ico` `.ttf` `.woff` `.woff2` `.xml` `.csv` `.wasm`
64
+ 说明 npm 版本已发布,但对应 GitHub Release 还没上传该平台二进制。
65
+
66
+ 解决方式:
67
+
68
+ 1. 手动安装 CLI(上面安装表)
69
+ 2. 在插件里指定 `binaryPath`
70
+
71
+ ```js
72
+ zcompress({
73
+ binaryPath: '/absolute/path/to/zcompress',
74
+ })
75
+ ```
76
+
77
+ 默认 `failOnError: true`,失败会直接中断构建(不会静默跳过压缩)。
66
78
 
67
79
  ## License
68
80
 
@@ -80,11 +92,16 @@ High-performance multi-threaded asset compression for Vite production builds. 5-
80
92
  npm install zcompress-vite-plugin --save-dev
81
93
  ```
82
94
 
83
- The correct platform binary is downloaded automatically. If that fails:
95
+ The package auto-downloads a platform binary. If that fails, install/build manually:
84
96
 
85
97
  ```bash
86
- brew install zcompress # macOS
87
- zig build install # from source
98
+ # macOS
99
+ brew install zstd brotli
100
+ zig build -Doptimize=ReleaseFast
101
+
102
+ # Linux
103
+ sudo apt-get install -y libzstd-dev libbrotli-dev
104
+ zig build -Doptimize=ReleaseFast
88
105
  ```
89
106
 
90
107
  ## Usage
@@ -97,7 +114,7 @@ export default {
97
114
  plugins: [
98
115
  zcompress({
99
116
  algo: 'brotli', // 'gzip' | 'zstd' | 'brotli'
100
- level: 11, // 1-9
117
+ level: 9, // 1-9
101
118
  threads: 0, // 0 = auto
102
119
  cache: true, // skip unchanged files
103
120
  })
@@ -105,15 +122,7 @@ export default {
105
122
  };
106
123
  ```
107
124
 
108
- After `vite build`, find compressed assets in `dist-compressed/`:
109
-
110
- ```
111
- dist-compressed/
112
- ├── index.html.br
113
- ├── style.css.br
114
- ├── app.js.br
115
- └── logo.svg.br
116
- ```
125
+ After `vite build`, compressed assets are written to `dist-compressed/`.
117
126
 
118
127
  ## Options
119
128
 
@@ -127,6 +136,32 @@ dist-compressed/
127
136
  | `include` | `string[]` | `[]` | Extra extensions to compress |
128
137
  | `exclude` | `string[]` | `[]` | Extensions to skip |
129
138
  | `binaryPath` | `string` | auto | Override binary path |
139
+ | `failOnError` | `boolean` | `true` | Fail `vite build` when compression fails |
140
+
141
+ ## Troubleshooting
142
+
143
+ ### `postinstall` binary download returns 404
144
+
145
+ If you see:
146
+
147
+ ```txt
148
+ https://github.com/luochuan2008/zcompress/releases/download/vX.Y.Z/zcompress-macos-arm64 → 404
149
+ ```
150
+
151
+ that npm version exists, but the matching GitHub Release binary asset is missing.
152
+
153
+ Workarounds:
154
+
155
+ 1. Install/build the CLI manually
156
+ 2. Set `binaryPath` explicitly
157
+
158
+ ```js
159
+ zcompress({
160
+ binaryPath: '/absolute/path/to/zcompress',
161
+ })
162
+ ```
163
+
164
+ Default `failOnError: true` ensures builds fail loudly (instead of silently skipping compression).
130
165
 
131
166
  ## License
132
167
 
package/index.d.ts CHANGED
@@ -16,6 +16,8 @@ declare module 'zcompress-vite-plugin' {
16
16
  exclude?: string[];
17
17
  /** Override path to zcompress binary */
18
18
  binaryPath?: string;
19
+ /** Fail Vite build if compression fails (default: true) */
20
+ failOnError?: boolean;
19
21
  }
20
22
 
21
23
  import type { Plugin } from 'vite';
package/index.js CHANGED
@@ -15,7 +15,7 @@
15
15
  * };
16
16
  */
17
17
 
18
- import { execSync } from 'node:child_process';
18
+ import { execFileSync } from 'node:child_process';
19
19
  import { existsSync, statSync, readdirSync } from 'node:fs';
20
20
  import { join, dirname, isAbsolute } from 'node:path';
21
21
  import { fileURLToPath } from 'node:url';
@@ -32,6 +32,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
32
32
  * @property {string[]} [include] - Extra file extensions to include
33
33
  * @property {string[]} [exclude] - File extensions to exclude
34
34
  * @property {string} [binaryPath] - Path to zcompress binary
35
+ * @property {boolean} [failOnError=true] - Fail Vite build if compression fails
35
36
  */
36
37
 
37
38
  /** @type {ZCompressOptions} */
@@ -43,6 +44,7 @@ const DEFAULT_OPTIONS = {
43
44
  cache: false,
44
45
  include: [],
45
46
  exclude: [],
47
+ failOnError: true,
46
48
  };
47
49
 
48
50
  /**
@@ -130,7 +132,7 @@ export default function zcompressPlugin(userOptions = {}) {
130
132
  const startTime = Date.now();
131
133
 
132
134
  try {
133
- execSync(cmd, { stdio: 'inherit' });
135
+ execFileSync(binary, args, { stdio: 'inherit' });
134
136
  const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
135
137
 
136
138
  const srcSize = getDirSize(outDir);
@@ -141,8 +143,18 @@ export default function zcompressPlugin(userOptions = {}) {
141
143
 
142
144
  console.log(`[zcompress] ✅ Compressed ${formatSize(srcSize)} → ${formatSize(destSize)} (saved ${savedPct}%) in ${elapsed}s`);
143
145
  } catch (err) {
144
- console.error(`[zcompress] Compression failed: ${err.message}`);
145
- console.error('[zcompress] Make sure zcompress is installed: zig build install');
146
+ const message = [
147
+ `[zcompress] Compression failed: ${err.message}`,
148
+ `[zcompress] Binary used: ${binary}`,
149
+ '[zcompress] If this is a 404 download issue, GitHub Release may be missing prebuilt binaries for this version.',
150
+ '[zcompress] Workaround: install CLI manually (`zig build -Doptimize=ReleaseFast`) and set `binaryPath` in plugin options.',
151
+ ].join('\n');
152
+
153
+ if (options.failOnError !== false) {
154
+ throw new Error(message);
155
+ }
156
+
157
+ console.error(message);
146
158
  }
147
159
  },
148
160
  };
package/install.js CHANGED
@@ -86,7 +86,14 @@ async function downloadBinary() {
86
86
  console.log(`[zcompress] ✓ Binary installed: ${destPath}`);
87
87
  return true;
88
88
  } catch (err) {
89
- console.log(`[zcompress] ⚠ Could not download binary (${err.message}). Build from source: cd zcompress && zig build install`);
89
+ if (String(err.message).includes('HTTP 404')) {
90
+ console.log(`[zcompress] ⚠ Release asset not found for v${VERSION}: ${binaryName}`);
91
+ console.log(`[zcompress] ⚠ Expected URL: ${url}`);
92
+ console.log('[zcompress] ⚠ This usually means the npm version was published before uploading binary assets to GitHub Releases.');
93
+ } else {
94
+ console.log(`[zcompress] ⚠ Could not download binary (${err.message}).`);
95
+ }
96
+ console.log('[zcompress] ℹ Build from source: cd zcompress && zig build -Doptimize=ReleaseFast');
90
97
  // Clean up partial download
91
98
  try { unlinkSync(destPath); } catch (_) { /* ignore */ }
92
99
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zcompress-vite-plugin",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "High-performance multi-threaded asset compression for Vite — powered by Zig",
5
5
  "type": "module",
6
6
  "main": "index.js",