compression-rspack-plugin 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ramon-villain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # compression-rspack-plugin
2
+
3
+ Rust-native parallel compression plugin for [Rspack](https://rspack.dev). Drop-in replacement for [compression-webpack-plugin](https://github.com/webpack/compression-webpack-plugin).
4
+
5
+ Compresses assets **in parallel** across all CPU cores via Rust + [rayon](https://github.com/rayon-rs/rayon), instead of serially on the Node.js main thread.
6
+
7
+ ## Benchmarks
8
+
9
+ Tested on a production Rspack build (~1,400 assets, Apple M3 Pro 14-core).
10
+
11
+ | | compression-webpack-plugin | compression-rspack-plugin |
12
+ |---|---|---|
13
+ | **Build time** (avg of 3) | 82.7s | **37.3s** (2.2x faster) |
14
+ | **Compression phase** (RsDoctor) | 61.1s | **20.3s** (3.0x faster) |
15
+ | .gz files | 1,410 | 1,411 |
16
+ | .br files | 1,444 | 1,444 |
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ pnpm add compression-rspack-plugin
22
+ ```
23
+
24
+ Requires Rust toolchain (`rustc`, `cargo`) for the native addon. Install via [rustup](https://rustup.rs).
25
+
26
+ ## Usage
27
+
28
+ ```js
29
+ import { CompressionRspackPlugin } from 'compression-rspack-plugin'
30
+
31
+ export default {
32
+ plugins: [
33
+ new CompressionRspackPlugin({ test: /\.(js|css|html|json|svg)$/ }),
34
+ new CompressionRspackPlugin({ algorithm: 'brotliCompress', test: /\.(js|css|html|json|svg)$/ }),
35
+ ],
36
+ }
37
+ ```
38
+
39
+ ## Options
40
+
41
+ Same interface as [compression-webpack-plugin](https://github.com/webpack/compression-webpack-plugin#options). Validated via `schema-utils`.
42
+
43
+ | Option | Type | Default | Description |
44
+ |---|---|---|---|
45
+ | `algorithm` | `string \| Function` | `"gzip"` | `"gzip"`, `"brotliCompress"`, `"deflate"`, `"deflateRaw"`, or custom callback |
46
+ | `compressionOptions` | `object` | `{}` | Passed to the algorithm. Defaults to max quality per algorithm. |
47
+ | `filename` | `string \| Function` | `"[path][base].gz"` | Output template. `"[path][base].br"` for brotli. |
48
+ | `test` | `RegExp \| string \| Array` | — | Include matching assets |
49
+ | `include` | `RegExp \| string \| Array` | — | Include matching assets |
50
+ | `exclude` | `RegExp \| string \| Array` | — | Exclude matching assets |
51
+ | `threshold` | `number` | `0` | Min size in bytes |
52
+ | `minRatio` | `number` | `0.8` | Only emit if compressed/original < this |
53
+ | `deleteOriginalAssets` | `boolean \| "keep-source-map" \| Function` | `false` | Remove originals after compression |
54
+
55
+ ## How it works
56
+
57
+ Built-in algorithms (`gzip`, `brotliCompress`, `deflate`, `deflateRaw`) are handled by [flate2](https://github.com/rust-lang/flate2-rs) and [brotli](https://github.com/dropbox/rust-brotli) in Rust via a single napi-rs FFI call. Custom algorithms fall back to Node.js `zlib`.
58
+
59
+ ## Differences from compression-webpack-plugin
60
+
61
+ | | compression-webpack-plugin | compression-rspack-plugin |
62
+ |---|---|---|
63
+ | Compression | Node.js zlib (serial) | Rust rayon (parallel) + zlib fallback |
64
+ | Compilation cache | Yes | Not yet |
65
+ | Child compilations | Yes (scoped via `thisCompilation`) | Yes |
66
+ | `[compressed]` stats flag | Yes | Registered, not rendered by rspack 2.0 beta |
67
+
68
+ ## Development
69
+
70
+ ```bash
71
+ pnpm install # install deps
72
+ pnpm build # native addon + TypeScript
73
+ pnpm test # JS tests (118 tests)
74
+ pnpm test:rust # Rust unit tests
75
+ pnpm lint # biome
76
+ pnpm lint:rs # cargo fmt + clippy
77
+ pnpm typecheck # tsc --noEmit
78
+ ```
79
+
80
+ ## License
81
+
82
+ [MIT](LICENSE)
@@ -0,0 +1,3 @@
1
+ import type { Algorithm, CompressorFn } from "../types.ts";
2
+ export declare const compressNativeBinding: (algorithm: Algorithm, compressionOptions: Record<string, unknown>) => CompressorFn;
3
+ //# sourceMappingURL=native.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../../lib/compressors/native.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAoB,YAAY,EAAE,MAAM,aAAa,CAAC;AA+B7E,eAAO,MAAM,qBAAqB,GAChC,WAAW,SAAS,EACpB,oBAAoB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC1C,YAUF,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { createRequire } from "node:module";
2
+ import zlib from "node:zlib";
3
+ let nativeBinding;
4
+ try {
5
+ nativeBinding = createRequire(import.meta.url)("../../index.cjs");
6
+ }
7
+ catch (err) {
8
+ console.error("[compression-rspack-plugin] Failed to load native addon. Run `pnpm build` first.");
9
+ throw err;
10
+ }
11
+ const extractLevel = (algorithm, options) => {
12
+ if (algorithm === "brotliCompress") {
13
+ const { params } = options;
14
+ if (typeof params === "object" && params !== null) {
15
+ const quality = params[zlib.constants.BROTLI_PARAM_QUALITY];
16
+ return typeof quality === "number" ? quality : undefined;
17
+ }
18
+ return undefined;
19
+ }
20
+ return typeof options.level === "number" ? options.level : undefined;
21
+ };
22
+ export const compressNativeBinding = (algorithm, compressionOptions) => {
23
+ if (typeof algorithm !== "string") {
24
+ throw new Error("Algorithm is not a string");
25
+ }
26
+ return async (eligible) => nativeBinding.compressAssets(eligible, {
27
+ algorithm,
28
+ level: extractLevel(algorithm, compressionOptions),
29
+ });
30
+ };
31
+ //# sourceMappingURL=native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.js","sourceRoot":"","sources":["../../lib/compressors/native.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAW7B,IAAI,aAA4B,CAAC;AACjC,IAAI,CAAC;IACH,aAAa,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,iBAAiB,CAAC,CAAC;AACpE,CAAC;AAAC,OAAO,GAAG,EAAE,CAAC;IACb,OAAO,CAAC,KAAK,CAAC,kFAAkF,CAAC,CAAC;IAClG,MAAM,GAAG,CAAC;AACZ,CAAC;AAED,MAAM,YAAY,GAAG,CAAC,SAAiB,EAAE,OAAgC,EAAsB,EAAE;IAC/F,IAAI,SAAS,KAAK,gBAAgB,EAAE,CAAC;QACnC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAC3B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;YAClD,MAAM,OAAO,GAAI,MAA2C,CAC1D,IAAI,CAAC,SAAS,CAAC,oBAAoB,CACpC,CAAC;YACF,OAAO,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;QAC3D,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,OAAO,OAAO,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACvE,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,SAAoB,EACpB,kBAA2C,EAC7B,EAAE;IAChB,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,KAAK,EAAE,QAAQ,EAAE,EAAE,CACxB,aAAa,CAAC,cAAc,CAAC,QAAQ,EAAE;QACrC,SAAS;QACT,KAAK,EAAE,YAAY,CAAC,SAAS,EAAE,kBAAkB,CAAC;KACnD,CAAC,CAAC;AACP,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Algorithm, CompressorFn } from "../types.ts";
2
+ export declare const compressNodeZlib: (algorithm: Algorithm, compressionOptions: Record<string, unknown>) => CompressorFn;
3
+ //# sourceMappingURL=zlib.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zlib.d.ts","sourceRoot":"","sources":["../../lib/compressors/zlib.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAoB,YAAY,EAAE,MAAM,aAAa,CAAC;AAiB7E,eAAO,MAAM,gBAAgB,GAC3B,WAAW,SAAS,EACpB,oBAAoB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC1C,YAyBF,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { promisify } from "node:util";
2
+ import zlib from "node:zlib";
3
+ import pLimit from "p-limit";
4
+ const zlibLimit = pLimit(parseInt(process.env.UV_THREADPOOL_SIZE ?? "4", 10));
5
+ const CUSTOM_ALGORITHM = "custom";
6
+ const compressWithZlib = async (algorithm, content, options) => {
7
+ const fn = typeof algorithm === "function" ? algorithm : zlib[algorithm];
8
+ if (typeof fn !== "function") {
9
+ throw new Error(`Algorithm "${algorithm}" is not found in "zlib"`);
10
+ }
11
+ return promisify(fn)(content, options);
12
+ };
13
+ export const compressNodeZlib = (algorithm, compressionOptions) => {
14
+ const algorithmName = typeof algorithm === "string" ? algorithm : CUSTOM_ALGORITHM;
15
+ return async (eligible, compilation) => {
16
+ const results = await Promise.all(eligible.map(({ name, buffer }) => zlibLimit(async () => {
17
+ try {
18
+ const compressed = await compressWithZlib(algorithm, buffer, compressionOptions);
19
+ return {
20
+ name,
21
+ buffer: compressed,
22
+ algorithm: algorithmName,
23
+ originalSize: buffer.length,
24
+ compressedSize: compressed.length,
25
+ };
26
+ }
27
+ catch (error) {
28
+ compilation.errors.push(error instanceof Error ? error : new Error(String(error)));
29
+ return null;
30
+ }
31
+ })));
32
+ return results.filter((r) => r !== null);
33
+ };
34
+ };
35
+ //# sourceMappingURL=zlib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zlib.js","sourceRoot":"","sources":["../../lib/compressors/zlib.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,MAAM,MAAM,SAAS,CAAC;AAI7B,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC;AAC9E,MAAM,gBAAgB,GAAG,QAAQ,CAAC;AAElC,MAAM,gBAAgB,GAAG,KAAK,EAC5B,SAAoB,EACpB,OAAe,EACf,OAAgC,EACf,EAAE;IACnB,MAAM,EAAE,GAAG,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAA8B,CAAC,CAAC;IAC9F,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK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 -0,0 +1,11 @@
1
+ import type { Asset } from "@rspack/core";
2
+ import type { Algorithm, DeleteOriginalAssets, FilenameTemplate } from "./types.ts";
3
+ export declare const NATIVE_ALGORITHMS: readonly ["gzip", "brotliCompress", "deflate", "deflateRaw"];
4
+ export type NativeAlgorithm = (typeof NATIVE_ALGORITHMS)[number];
5
+ export declare const isNativeAlgorithm: (alg: unknown) => alg is NativeAlgorithm;
6
+ export declare const ALGORITHM_DEFAULTS: Partial<Record<NativeAlgorithm, Record<string, unknown>>>;
7
+ export declare const getBuffer: (asset: Asset) => Buffer;
8
+ export declare const shouldDeleteOriginal: (policy: DeleteOriginalAssets, filename: string) => boolean;
9
+ export declare const resolveRelatedName: (algorithm: Algorithm, filename: FilenameTemplate) => string;
10
+ export declare const hasImmutablePlaceholder: (filename: FilenameTemplate) => boolean;
11
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../lib/helpers.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE1C,OAAO,KAAK,EAAE,SAAS,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGpF,eAAO,MAAM,iBAAiB,8DAA+D,CAAC;AAC9F,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAC;AAGjE,eAAO,MAAM,iBAAiB,GAAI,KAAK,OAAO,KAAG,GAAG,IAAI,eACR,CAAC;AAEjD,eAAO,MAAM,kBAAkB,EAAE,OAAO,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAOxF,CAAC;AAEF,eAAO,MAAM,SAAS,GAAI,OAAO,KAAK,KAAG,MAaxC,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,QAAQ,oBAAoB,EAAE,UAAU,MAAM,KAAG,OAMrF,CAAC;AAEF,eAAO,MAAM,kBAAkB,GAAI,WAAW,SAAS,EAAE,UAAU,gBAAgB,KAAG,MASrF,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,UAAU,gBAAgB,KAAG,OACG,CAAC"}
@@ -0,0 +1,45 @@
1
+ import crypto from "node:crypto";
2
+ import path from "node:path";
3
+ import zlib from "node:zlib";
4
+ import { KEEP_SOURCE_MAP } from "./types.js";
5
+ export const NATIVE_ALGORITHMS = ["gzip", "brotliCompress", "deflate", "deflateRaw"];
6
+ const NATIVE_SET = new Set(NATIVE_ALGORITHMS);
7
+ export const isNativeAlgorithm = (alg) => typeof alg === "string" && NATIVE_SET.has(alg);
8
+ export const ALGORITHM_DEFAULTS = {
9
+ gzip: { level: zlib.constants.Z_BEST_COMPRESSION },
10
+ deflate: { level: zlib.constants.Z_BEST_COMPRESSION },
11
+ deflateRaw: { level: zlib.constants.Z_BEST_COMPRESSION },
12
+ brotliCompress: {
13
+ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY },
14
+ },
15
+ };
16
+ export const getBuffer = (asset) => {
17
+ let buffer;
18
+ if (typeof asset.source.buffer === "function") {
19
+ buffer = asset.source.buffer();
20
+ }
21
+ else {
22
+ buffer = asset.source.source();
23
+ if (!Buffer.isBuffer(buffer)) {
24
+ buffer = Buffer.from(buffer);
25
+ }
26
+ }
27
+ return buffer;
28
+ };
29
+ export const shouldDeleteOriginal = (policy, filename) => {
30
+ if (typeof policy !== "function") {
31
+ return policy === true || policy === KEEP_SOURCE_MAP;
32
+ }
33
+ return policy(filename);
34
+ };
35
+ export const resolveRelatedName = (algorithm, filename) => {
36
+ if (typeof algorithm === "function") {
37
+ if (typeof filename === "function") {
38
+ return `compression-function-${crypto.createHash("md5").update(String(filename)).digest("hex")}`;
39
+ }
40
+ return `${path.extname(filename.split("?")[0]).slice(1)}ed`;
41
+ }
42
+ return algorithm === "gzip" ? "gzipped" : `${algorithm}ed`;
43
+ };
44
+ export const hasImmutablePlaceholder = (filename) => typeof filename === "string" && /\[(?:name|base|file)]/.test(filename);
45
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../lib/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAK7B,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,MAAM,EAAE,gBAAgB,EAAE,SAAS,EAAE,YAAY,CAAU,CAAC;AAE9F,MAAM,UAAU,GAAG,IAAI,GAAG,CAAS,iBAAiB,CAAC,CAAC;AAEtD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAAY,EAA0B,EAAE,CACxE,OAAO,GAAG,KAAK,QAAQ,IAAI,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAEjD,MAAM,CAAC,MAAM,kBAAkB,GAA8D;IAC3F,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,kBAAkB,EAAE;IAClD,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,kBAAkB,EAAE;IACrD,UAAU,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,kBAAkB,EAAE;IACxD,cAAc,EAAE;QACd,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC,EAAE,IAAI,CAAC,SAAS,CAAC,kBAAkB,EAAE;KACrF;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAY,EAAU,EAAE;IAChD,IAAI,MAA8C,CAAC;IAEnD,IAAI,OAAO,KAAK,CAAC,MAAM,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QAC9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B,GAAG,CAAC,QAA0B,EAAW,EAAE,CAC7E,OAAO,QAAQ,KAAK,QAAQ,IAAI,uBAAuB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { Compiler } from "@rspack/core";
2
+ import type { CompressionRspackPluginOptions } from "./types.ts";
3
+ export declare class CompressionRspackPlugin {
4
+ static readonly PLUGIN_NAME = "CompressionRspackPlugin";
5
+ private readonly opts;
6
+ private readonly compress;
7
+ private readonly relatedName;
8
+ private readonly canPreserveImmutable;
9
+ constructor(baseOptions?: CompressionRspackPluginOptions);
10
+ apply(compiler: Compiler): void;
11
+ private applyResults;
12
+ private getNewFileName;
13
+ }
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAsB,QAAQ,EAAE,MAAM,cAAc,CAAC;AAejE,OAAO,KAAK,EAEV,8BAA8B,EAG/B,MAAM,YAAY,CAAC;AAGpB,qBAAa,uBAAuB;IAClC,MAAM,CAAC,QAAQ,CAAC,WAAW,6BAA6B;IAExD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAkB;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2C;IACpE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAU;gBAEnC,WAAW,GAAE,8BAAmC;IAgD5D,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;YAoDjB,YAAY;IA8E1B,OAAO,CAAC,cAAc;CASvB"}
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ import { createRequire } from "node:module";
2
+ import zlib from "node:zlib";
3
+ import { validate } from "schema-utils";
4
+ import { compressNativeBinding } from "./compressors/native.js";
5
+ import { compressNodeZlib } from "./compressors/zlib.js";
6
+ import { ALGORITHM_DEFAULTS, getBuffer, hasImmutablePlaceholder, isNativeAlgorithm, resolveRelatedName, shouldDeleteOriginal, } from "./helpers.js";
7
+ import { KEEP_SOURCE_MAP } from "./types.js";
8
+ export class CompressionRspackPlugin {
9
+ static PLUGIN_NAME = "CompressionRspackPlugin";
10
+ opts;
11
+ compress;
12
+ relatedName;
13
+ canPreserveImmutable;
14
+ constructor(baseOptions = {}) {
15
+ validate(createRequire(import.meta.url)("./options.json"), baseOptions, {
16
+ name: "Compression Plugin",
17
+ baseDataPath: "options",
18
+ });
19
+ const options = Object.assign({}, {
20
+ algorithm: "gzip",
21
+ threshold: 0,
22
+ minRatio: 0.8,
23
+ deleteOriginalAssets: false,
24
+ compressionOptions: {},
25
+ }, baseOptions);
26
+ if (typeof options.algorithm === "string" && !isNativeAlgorithm(options.algorithm)) {
27
+ if (typeof zlib[options.algorithm] !== "function") {
28
+ throw new Error(`Algorithm "${options.algorithm}" is not found in "zlib"`);
29
+ }
30
+ }
31
+ const compressionOptions = Object.assign({}, isNativeAlgorithm(options.algorithm) ? ALGORITHM_DEFAULTS[options.algorithm] : undefined, options.compressionOptions);
32
+ this.opts = {
33
+ filename: options.algorithm === "brotliCompress" ? "[path][base].br" : "[path][base].gz",
34
+ ...options,
35
+ compressionOptions,
36
+ };
37
+ this.relatedName = resolveRelatedName(this.opts.algorithm, this.opts.filename);
38
+ this.canPreserveImmutable = hasImmutablePlaceholder(this.opts.filename);
39
+ this.compress = (isNativeAlgorithm(options.algorithm) ? compressNativeBinding : compressNodeZlib)(options.algorithm, compressionOptions);
40
+ }
41
+ apply(compiler) {
42
+ compiler.hooks.thisCompilation.tap(CompressionRspackPlugin.PLUGIN_NAME, (compilation) => {
43
+ compilation.hooks.processAssets.tapPromise({
44
+ name: CompressionRspackPlugin.PLUGIN_NAME,
45
+ stage: compiler.rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER,
46
+ additionalAssets: true,
47
+ }, async () => {
48
+ const eligible = [];
49
+ for (const asset of compilation.getAssets()) {
50
+ if (!compiler.rspack.ModuleFilenameHelpers.matchObject.bind(undefined, this.opts)(asset.name) ||
51
+ asset.info?.compressed) {
52
+ continue;
53
+ }
54
+ const buffer = getBuffer(asset);
55
+ if (buffer.length >= this.opts.threshold) {
56
+ eligible.push({ name: asset.name, buffer });
57
+ }
58
+ }
59
+ if (eligible.length === 0)
60
+ return;
61
+ await this.applyResults(compilation, compiler, eligible);
62
+ });
63
+ compilation.hooks.statsPrinter.tap(CompressionRspackPlugin.PLUGIN_NAME, (statsPrinter) => {
64
+ statsPrinter.hooks.print
65
+ .for("asset.info.compressed")
66
+ .tap(CompressionRspackPlugin.PLUGIN_NAME, (compressed, { green, formatFlag }) => compressed ? green(formatFlag("compressed")) : undefined);
67
+ });
68
+ });
69
+ }
70
+ async applyResults(compilation, compiler, eligible) {
71
+ const results = await this.compress(eligible, compilation);
72
+ if (results.length === 0)
73
+ return;
74
+ const filesAndRelatedToDelete = new Map();
75
+ for (const result of results) {
76
+ if (result.originalSize > 0 &&
77
+ result.compressedSize / result.originalSize >= this.opts.minRatio) {
78
+ continue;
79
+ }
80
+ const original = compilation.getAsset(result.name);
81
+ const newFilename = this.getNewFileName(this.opts.filename, result, compilation);
82
+ const willDelete = shouldDeleteOriginal(this.opts.deleteOriginalAssets, result.name);
83
+ if (!willDelete && original) {
84
+ compilation.updateAsset(result.name, (source) => source, (assetInfo) => {
85
+ assetInfo.related = {
86
+ ...assetInfo.related,
87
+ [this.relatedName]: newFilename,
88
+ };
89
+ return assetInfo;
90
+ });
91
+ }
92
+ compilation.emitAsset(newFilename, new compiler.rspack.sources.RawSource(result.buffer, false), {
93
+ compressed: true,
94
+ ...(this.canPreserveImmutable && original?.info?.immutable ? { immutable: true } : {}),
95
+ });
96
+ if (willDelete) {
97
+ filesAndRelatedToDelete.set(result.name, original?.info?.related);
98
+ }
99
+ }
100
+ if (filesAndRelatedToDelete.size > 0) {
101
+ for (const [filename, related] of filesAndRelatedToDelete) {
102
+ if (this.opts.deleteOriginalAssets === KEEP_SOURCE_MAP && related?.sourceMap) {
103
+ try {
104
+ compilation.updateAsset(filename, (source) => source, (info) => {
105
+ info.related = { ...related, sourceMap: undefined };
106
+ return info;
107
+ });
108
+ }
109
+ catch (err) {
110
+ compilation.warnings.push(new Error(`[compression-rspack-plugin] Could not detach source map for "${filename}": ${err instanceof Error ? err.message : String(err)}`));
111
+ }
112
+ }
113
+ compilation.deleteAsset(filename);
114
+ }
115
+ }
116
+ }
117
+ getNewFileName(filename, result, compilation) {
118
+ return typeof filename === "function"
119
+ ? filename({ filename: result.name })
120
+ : compilation.getPath(filename, { filename: result.name });
121
+ }
122
+ }
123
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAI7B,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EACL,kBAAkB,EAClB,SAAS,EACT,uBAAuB,EACvB,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAOtB,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,OAAO,uBAAuB;IAClC,MAAM,CAAU,WAAW,GAAG,yBAAyB,CAAC;IAEvC,IAAI,CAAkB;IACtB,QAAQ,CAA2C;IACnD,WAAW,CAAS;IACpB,oBAAoB,CAAU;IAE/C,YAAY,cAA8C,EAAE;QAC1D,QAAQ,CACN,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,gBAAgB,CAAmC,EAClF,WAAW,EACX;YACE,IAAI,EAAE,oBAAoB;YAC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AAS,EAAE,SAAS,EAAE,CAAC;4BACpD,OAAO,IAAI,CAAC;wBACd,CAAC,CACF,CAAC;oBACJ,CAAC;oBAAC,OAAO,GAAG,EAAE,CAAC;wBACb,WAAW,CAAC,QAAQ,CAAC,IAAI,CACvB,IAAI,KAAK,CACP,gEAAgE,QAAQ,MACtE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CACjD,EAAE,CACH,CACF,CAAC;oBACJ,CAAC;gBACH,CAAC;gBACD,WAAW,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;YACpC,CAAC;QACH,CAAC;IACH,CAAC;IAEO,cAAc,CACpB,QAA0B,EAC1B,MAAwB,EACxB,WAAwB;QAExB,OAAO,OAAO,QAAQ,KAAK,UAAU;YACnC,CAAC,CAAC,QAAQ,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;YACrC,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/D,CAAC"}
@@ -0,0 +1,113 @@
1
+ {
2
+ "type": "object",
3
+ "additionalProperties": false,
4
+ "definitions": {
5
+ "Rule": {
6
+ "description": "Filtering rule as regex or string.",
7
+ "anyOf": [
8
+ {
9
+ "instanceof": "RegExp",
10
+ "tsType": "RegExp"
11
+ },
12
+ {
13
+ "type": "string",
14
+ "minLength": 1
15
+ }
16
+ ]
17
+ },
18
+ "Rules": {
19
+ "description": "Filtering rules.",
20
+ "anyOf": [
21
+ {
22
+ "type": "array",
23
+ "items": {
24
+ "description": "A rule condition.",
25
+ "oneOf": [
26
+ {
27
+ "$ref": "#/definitions/Rule"
28
+ }
29
+ ]
30
+ }
31
+ },
32
+ {
33
+ "$ref": "#/definitions/Rule"
34
+ }
35
+ ]
36
+ }
37
+ },
38
+ "properties": {
39
+ "test": {
40
+ "description": "Include all assets that pass test assertion.",
41
+ "oneOf": [
42
+ {
43
+ "$ref": "#/definitions/Rules"
44
+ }
45
+ ]
46
+ },
47
+ "include": {
48
+ "description": "Include all assets matching any of these conditions.",
49
+ "oneOf": [
50
+ {
51
+ "$ref": "#/definitions/Rules"
52
+ }
53
+ ]
54
+ },
55
+ "exclude": {
56
+ "description": "Exclude all assets matching any of these conditions.",
57
+ "oneOf": [
58
+ {
59
+ "$ref": "#/definitions/Rules"
60
+ }
61
+ ]
62
+ },
63
+ "algorithm": {
64
+ "description": "The compression algorithm/function.",
65
+ "anyOf": [
66
+ {
67
+ "type": "string"
68
+ },
69
+ {
70
+ "instanceof": "Function"
71
+ }
72
+ ]
73
+ },
74
+ "compressionOptions": {
75
+ "description": "Compression options for algorithm.",
76
+ "additionalProperties": true,
77
+ "type": "object"
78
+ },
79
+ "threshold": {
80
+ "description": "Only assets bigger than this size are processed. In bytes.",
81
+ "type": "number"
82
+ },
83
+ "minRatio": {
84
+ "description": "Only assets that compress better than this ratio are processed.",
85
+ "type": "number"
86
+ },
87
+ "deleteOriginalAssets": {
88
+ "description": "Whether to delete the original assets or not.",
89
+ "anyOf": [
90
+ {
91
+ "type": "boolean"
92
+ },
93
+ {
94
+ "instanceof": "Function"
95
+ },
96
+ {
97
+ "enum": ["keep-source-map"]
98
+ }
99
+ ]
100
+ },
101
+ "filename": {
102
+ "description": "The target asset filename.",
103
+ "anyOf": [
104
+ {
105
+ "type": "string"
106
+ },
107
+ {
108
+ "instanceof": "Function"
109
+ }
110
+ ]
111
+ }
112
+ }
113
+ }
@@ -0,0 +1,34 @@
1
+ import type { Compilation } from "@rspack/core";
2
+ export interface CompressedResult {
3
+ name: string;
4
+ buffer: Buffer;
5
+ algorithm: string;
6
+ originalSize: number;
7
+ compressedSize: number;
8
+ }
9
+ export type AlgorithmFn = (input: Buffer, options: Record<string, unknown>, callback: (err: Error | null, result: Buffer) => void) => void;
10
+ export type Algorithm = string | AlgorithmFn;
11
+ export declare const KEEP_SOURCE_MAP = "keep-source-map";
12
+ export type DeleteOriginalAssets = boolean | typeof KEEP_SOURCE_MAP | ((name: string) => boolean);
13
+ export type FilenameTemplate = string | ((pathData: {
14
+ filename: string;
15
+ }) => string);
16
+ export type CompressorFn = (eligible: Array<{
17
+ name: string;
18
+ buffer: Buffer;
19
+ }>, compilation: Compilation) => Promise<CompressedResult[]>;
20
+ export type Rule = RegExp | string;
21
+ export type Rules = Rule[] | Rule;
22
+ export interface CompressionRspackPluginOptions {
23
+ algorithm?: Algorithm;
24
+ compressionOptions?: Record<string, unknown>;
25
+ filename?: FilenameTemplate;
26
+ test?: Rules;
27
+ include?: Rules;
28
+ exclude?: Rules;
29
+ threshold?: number;
30
+ minRatio?: number;
31
+ deleteOriginalAssets?: DeleteOriginalAssets;
32
+ }
33
+ export type ResolvedOptions = Required<Pick<CompressionRspackPluginOptions, "algorithm" | "compressionOptions" | "filename" | "threshold" | "minRatio" | "deleteOriginalAssets">> & Pick<CompressionRspackPluginOptions, "test" | "include" | "exclude">;
34
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,MAAM,WAAW,GAAG,CACxB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,QAAQ,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,KAClD,IAAI,CAAC;AAEV,MAAM,MAAM,SAAS,GAAG,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,SAAS,CAAC,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export const KEEP_SOURCE_MAP = "keep-source-map";
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAkBA,MAAM,CAAC,MAAM,eAAe,GAAG,iBAAiB,CAAC"}
package/index.cjs ADDED
@@ -0,0 +1,579 @@
1
+ // prettier-ignore
2
+ /* eslint-disable */
3
+ // @ts-nocheck
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const { readFileSync } = require('node:fs')
7
+ let nativeBinding = null
8
+ const loadErrors = []
9
+
10
+ const isMusl = () => {
11
+ let musl = false
12
+ if (process.platform === 'linux') {
13
+ musl = isMuslFromFilesystem()
14
+ if (musl === null) {
15
+ musl = isMuslFromReport()
16
+ }
17
+ if (musl === null) {
18
+ musl = isMuslFromChildProcess()
19
+ }
20
+ }
21
+ return musl
22
+ }
23
+
24
+ const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')
25
+
26
+ const isMuslFromFilesystem = () => {
27
+ try {
28
+ return readFileSync('/usr/bin/ldd', 'utf-8').includes('musl')
29
+ } catch {
30
+ return null
31
+ }
32
+ }
33
+
34
+ const isMuslFromReport = () => {
35
+ let report = null
36
+ if (typeof process.report?.getReport === 'function') {
37
+ process.report.excludeNetwork = true
38
+ report = process.report.getReport()
39
+ }
40
+ if (!report) {
41
+ return null
42
+ }
43
+ if (report.header && report.header.glibcVersionRuntime) {
44
+ return false
45
+ }
46
+ if (Array.isArray(report.sharedObjects)) {
47
+ if (report.sharedObjects.some(isFileMusl)) {
48
+ return true
49
+ }
50
+ }
51
+ return false
52
+ }
53
+
54
+ const isMuslFromChildProcess = () => {
55
+ try {
56
+ return require('child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
57
+ } catch (e) {
58
+ // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false
59
+ return false
60
+ }
61
+ }
62
+
63
+ function requireNative() {
64
+ if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
65
+ try {
66
+ return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH);
67
+ } catch (err) {
68
+ loadErrors.push(err)
69
+ }
70
+ } else if (process.platform === 'android') {
71
+ if (process.arch === 'arm64') {
72
+ try {
73
+ return require('./compression-rspack-plugin.android-arm64.node')
74
+ } catch (e) {
75
+ loadErrors.push(e)
76
+ }
77
+ try {
78
+ const binding = require('compression-rspack-plugin-android-arm64')
79
+ const bindingPackageVersion = require('compression-rspack-plugin-android-arm64/package.json').version
80
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
81
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
82
+ }
83
+ return binding
84
+ } catch (e) {
85
+ loadErrors.push(e)
86
+ }
87
+ } else if (process.arch === 'arm') {
88
+ try {
89
+ return require('./compression-rspack-plugin.android-arm-eabi.node')
90
+ } catch (e) {
91
+ loadErrors.push(e)
92
+ }
93
+ try {
94
+ const binding = require('compression-rspack-plugin-android-arm-eabi')
95
+ const bindingPackageVersion = require('compression-rspack-plugin-android-arm-eabi/package.json').version
96
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
97
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
98
+ }
99
+ return binding
100
+ } catch (e) {
101
+ loadErrors.push(e)
102
+ }
103
+ } else {
104
+ loadErrors.push(new Error(`Unsupported architecture on Android ${process.arch}`))
105
+ }
106
+ } else if (process.platform === 'win32') {
107
+ if (process.arch === 'x64') {
108
+ if (process.config?.variables?.shlib_suffix === 'dll.a' || process.config?.variables?.node_target_type === 'shared_library') {
109
+ try {
110
+ return require('./compression-rspack-plugin.win32-x64-gnu.node')
111
+ } catch (e) {
112
+ loadErrors.push(e)
113
+ }
114
+ try {
115
+ const binding = require('compression-rspack-plugin-win32-x64-gnu')
116
+ const bindingPackageVersion = require('compression-rspack-plugin-win32-x64-gnu/package.json').version
117
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
118
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
119
+ }
120
+ return binding
121
+ } catch (e) {
122
+ loadErrors.push(e)
123
+ }
124
+ } else {
125
+ try {
126
+ return require('./compression-rspack-plugin.win32-x64-msvc.node')
127
+ } catch (e) {
128
+ loadErrors.push(e)
129
+ }
130
+ try {
131
+ const binding = require('compression-rspack-plugin-win32-x64-msvc')
132
+ const bindingPackageVersion = require('compression-rspack-plugin-win32-x64-msvc/package.json').version
133
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
134
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
135
+ }
136
+ return binding
137
+ } catch (e) {
138
+ loadErrors.push(e)
139
+ }
140
+ }
141
+ } else if (process.arch === 'ia32') {
142
+ try {
143
+ return require('./compression-rspack-plugin.win32-ia32-msvc.node')
144
+ } catch (e) {
145
+ loadErrors.push(e)
146
+ }
147
+ try {
148
+ const binding = require('compression-rspack-plugin-win32-ia32-msvc')
149
+ const bindingPackageVersion = require('compression-rspack-plugin-win32-ia32-msvc/package.json').version
150
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
151
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
152
+ }
153
+ return binding
154
+ } catch (e) {
155
+ loadErrors.push(e)
156
+ }
157
+ } else if (process.arch === 'arm64') {
158
+ try {
159
+ return require('./compression-rspack-plugin.win32-arm64-msvc.node')
160
+ } catch (e) {
161
+ loadErrors.push(e)
162
+ }
163
+ try {
164
+ const binding = require('compression-rspack-plugin-win32-arm64-msvc')
165
+ const bindingPackageVersion = require('compression-rspack-plugin-win32-arm64-msvc/package.json').version
166
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
167
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
168
+ }
169
+ return binding
170
+ } catch (e) {
171
+ loadErrors.push(e)
172
+ }
173
+ } else {
174
+ loadErrors.push(new Error(`Unsupported architecture on Windows: ${process.arch}`))
175
+ }
176
+ } else if (process.platform === 'darwin') {
177
+ try {
178
+ return require('./compression-rspack-plugin.darwin-universal.node')
179
+ } catch (e) {
180
+ loadErrors.push(e)
181
+ }
182
+ try {
183
+ const binding = require('compression-rspack-plugin-darwin-universal')
184
+ const bindingPackageVersion = require('compression-rspack-plugin-darwin-universal/package.json').version
185
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
186
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
187
+ }
188
+ return binding
189
+ } catch (e) {
190
+ loadErrors.push(e)
191
+ }
192
+ if (process.arch === 'x64') {
193
+ try {
194
+ return require('./compression-rspack-plugin.darwin-x64.node')
195
+ } catch (e) {
196
+ loadErrors.push(e)
197
+ }
198
+ try {
199
+ const binding = require('compression-rspack-plugin-darwin-x64')
200
+ const bindingPackageVersion = require('compression-rspack-plugin-darwin-x64/package.json').version
201
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
202
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
203
+ }
204
+ return binding
205
+ } catch (e) {
206
+ loadErrors.push(e)
207
+ }
208
+ } else if (process.arch === 'arm64') {
209
+ try {
210
+ return require('./compression-rspack-plugin.darwin-arm64.node')
211
+ } catch (e) {
212
+ loadErrors.push(e)
213
+ }
214
+ try {
215
+ const binding = require('compression-rspack-plugin-darwin-arm64')
216
+ const bindingPackageVersion = require('compression-rspack-plugin-darwin-arm64/package.json').version
217
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
218
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
219
+ }
220
+ return binding
221
+ } catch (e) {
222
+ loadErrors.push(e)
223
+ }
224
+ } else {
225
+ loadErrors.push(new Error(`Unsupported architecture on macOS: ${process.arch}`))
226
+ }
227
+ } else if (process.platform === 'freebsd') {
228
+ if (process.arch === 'x64') {
229
+ try {
230
+ return require('./compression-rspack-plugin.freebsd-x64.node')
231
+ } catch (e) {
232
+ loadErrors.push(e)
233
+ }
234
+ try {
235
+ const binding = require('compression-rspack-plugin-freebsd-x64')
236
+ const bindingPackageVersion = require('compression-rspack-plugin-freebsd-x64/package.json').version
237
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
238
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
239
+ }
240
+ return binding
241
+ } catch (e) {
242
+ loadErrors.push(e)
243
+ }
244
+ } else if (process.arch === 'arm64') {
245
+ try {
246
+ return require('./compression-rspack-plugin.freebsd-arm64.node')
247
+ } catch (e) {
248
+ loadErrors.push(e)
249
+ }
250
+ try {
251
+ const binding = require('compression-rspack-plugin-freebsd-arm64')
252
+ const bindingPackageVersion = require('compression-rspack-plugin-freebsd-arm64/package.json').version
253
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
254
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
255
+ }
256
+ return binding
257
+ } catch (e) {
258
+ loadErrors.push(e)
259
+ }
260
+ } else {
261
+ loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`))
262
+ }
263
+ } else if (process.platform === 'linux') {
264
+ if (process.arch === 'x64') {
265
+ if (isMusl()) {
266
+ try {
267
+ return require('./compression-rspack-plugin.linux-x64-musl.node')
268
+ } catch (e) {
269
+ loadErrors.push(e)
270
+ }
271
+ try {
272
+ const binding = require('compression-rspack-plugin-linux-x64-musl')
273
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-x64-musl/package.json').version
274
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
275
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
276
+ }
277
+ return binding
278
+ } catch (e) {
279
+ loadErrors.push(e)
280
+ }
281
+ } else {
282
+ try {
283
+ return require('./compression-rspack-plugin.linux-x64-gnu.node')
284
+ } catch (e) {
285
+ loadErrors.push(e)
286
+ }
287
+ try {
288
+ const binding = require('compression-rspack-plugin-linux-x64-gnu')
289
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-x64-gnu/package.json').version
290
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
291
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
292
+ }
293
+ return binding
294
+ } catch (e) {
295
+ loadErrors.push(e)
296
+ }
297
+ }
298
+ } else if (process.arch === 'arm64') {
299
+ if (isMusl()) {
300
+ try {
301
+ return require('./compression-rspack-plugin.linux-arm64-musl.node')
302
+ } catch (e) {
303
+ loadErrors.push(e)
304
+ }
305
+ try {
306
+ const binding = require('compression-rspack-plugin-linux-arm64-musl')
307
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-arm64-musl/package.json').version
308
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
309
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
310
+ }
311
+ return binding
312
+ } catch (e) {
313
+ loadErrors.push(e)
314
+ }
315
+ } else {
316
+ try {
317
+ return require('./compression-rspack-plugin.linux-arm64-gnu.node')
318
+ } catch (e) {
319
+ loadErrors.push(e)
320
+ }
321
+ try {
322
+ const binding = require('compression-rspack-plugin-linux-arm64-gnu')
323
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-arm64-gnu/package.json').version
324
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
325
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
326
+ }
327
+ return binding
328
+ } catch (e) {
329
+ loadErrors.push(e)
330
+ }
331
+ }
332
+ } else if (process.arch === 'arm') {
333
+ if (isMusl()) {
334
+ try {
335
+ return require('./compression-rspack-plugin.linux-arm-musleabihf.node')
336
+ } catch (e) {
337
+ loadErrors.push(e)
338
+ }
339
+ try {
340
+ const binding = require('compression-rspack-plugin-linux-arm-musleabihf')
341
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-arm-musleabihf/package.json').version
342
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
343
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
344
+ }
345
+ return binding
346
+ } catch (e) {
347
+ loadErrors.push(e)
348
+ }
349
+ } else {
350
+ try {
351
+ return require('./compression-rspack-plugin.linux-arm-gnueabihf.node')
352
+ } catch (e) {
353
+ loadErrors.push(e)
354
+ }
355
+ try {
356
+ const binding = require('compression-rspack-plugin-linux-arm-gnueabihf')
357
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-arm-gnueabihf/package.json').version
358
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
359
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
360
+ }
361
+ return binding
362
+ } catch (e) {
363
+ loadErrors.push(e)
364
+ }
365
+ }
366
+ } else if (process.arch === 'loong64') {
367
+ if (isMusl()) {
368
+ try {
369
+ return require('./compression-rspack-plugin.linux-loong64-musl.node')
370
+ } catch (e) {
371
+ loadErrors.push(e)
372
+ }
373
+ try {
374
+ const binding = require('compression-rspack-plugin-linux-loong64-musl')
375
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-loong64-musl/package.json').version
376
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
377
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
378
+ }
379
+ return binding
380
+ } catch (e) {
381
+ loadErrors.push(e)
382
+ }
383
+ } else {
384
+ try {
385
+ return require('./compression-rspack-plugin.linux-loong64-gnu.node')
386
+ } catch (e) {
387
+ loadErrors.push(e)
388
+ }
389
+ try {
390
+ const binding = require('compression-rspack-plugin-linux-loong64-gnu')
391
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-loong64-gnu/package.json').version
392
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
393
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
394
+ }
395
+ return binding
396
+ } catch (e) {
397
+ loadErrors.push(e)
398
+ }
399
+ }
400
+ } else if (process.arch === 'riscv64') {
401
+ if (isMusl()) {
402
+ try {
403
+ return require('./compression-rspack-plugin.linux-riscv64-musl.node')
404
+ } catch (e) {
405
+ loadErrors.push(e)
406
+ }
407
+ try {
408
+ const binding = require('compression-rspack-plugin-linux-riscv64-musl')
409
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-riscv64-musl/package.json').version
410
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
411
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
412
+ }
413
+ return binding
414
+ } catch (e) {
415
+ loadErrors.push(e)
416
+ }
417
+ } else {
418
+ try {
419
+ return require('./compression-rspack-plugin.linux-riscv64-gnu.node')
420
+ } catch (e) {
421
+ loadErrors.push(e)
422
+ }
423
+ try {
424
+ const binding = require('compression-rspack-plugin-linux-riscv64-gnu')
425
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-riscv64-gnu/package.json').version
426
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
427
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
428
+ }
429
+ return binding
430
+ } catch (e) {
431
+ loadErrors.push(e)
432
+ }
433
+ }
434
+ } else if (process.arch === 'ppc64') {
435
+ try {
436
+ return require('./compression-rspack-plugin.linux-ppc64-gnu.node')
437
+ } catch (e) {
438
+ loadErrors.push(e)
439
+ }
440
+ try {
441
+ const binding = require('compression-rspack-plugin-linux-ppc64-gnu')
442
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-ppc64-gnu/package.json').version
443
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
444
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
445
+ }
446
+ return binding
447
+ } catch (e) {
448
+ loadErrors.push(e)
449
+ }
450
+ } else if (process.arch === 's390x') {
451
+ try {
452
+ return require('./compression-rspack-plugin.linux-s390x-gnu.node')
453
+ } catch (e) {
454
+ loadErrors.push(e)
455
+ }
456
+ try {
457
+ const binding = require('compression-rspack-plugin-linux-s390x-gnu')
458
+ const bindingPackageVersion = require('compression-rspack-plugin-linux-s390x-gnu/package.json').version
459
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
460
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
461
+ }
462
+ return binding
463
+ } catch (e) {
464
+ loadErrors.push(e)
465
+ }
466
+ } else {
467
+ loadErrors.push(new Error(`Unsupported architecture on Linux: ${process.arch}`))
468
+ }
469
+ } else if (process.platform === 'openharmony') {
470
+ if (process.arch === 'arm64') {
471
+ try {
472
+ return require('./compression-rspack-plugin.openharmony-arm64.node')
473
+ } catch (e) {
474
+ loadErrors.push(e)
475
+ }
476
+ try {
477
+ const binding = require('compression-rspack-plugin-openharmony-arm64')
478
+ const bindingPackageVersion = require('compression-rspack-plugin-openharmony-arm64/package.json').version
479
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
480
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
481
+ }
482
+ return binding
483
+ } catch (e) {
484
+ loadErrors.push(e)
485
+ }
486
+ } else if (process.arch === 'x64') {
487
+ try {
488
+ return require('./compression-rspack-plugin.openharmony-x64.node')
489
+ } catch (e) {
490
+ loadErrors.push(e)
491
+ }
492
+ try {
493
+ const binding = require('compression-rspack-plugin-openharmony-x64')
494
+ const bindingPackageVersion = require('compression-rspack-plugin-openharmony-x64/package.json').version
495
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
496
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
497
+ }
498
+ return binding
499
+ } catch (e) {
500
+ loadErrors.push(e)
501
+ }
502
+ } else if (process.arch === 'arm') {
503
+ try {
504
+ return require('./compression-rspack-plugin.openharmony-arm.node')
505
+ } catch (e) {
506
+ loadErrors.push(e)
507
+ }
508
+ try {
509
+ const binding = require('compression-rspack-plugin-openharmony-arm')
510
+ const bindingPackageVersion = require('compression-rspack-plugin-openharmony-arm/package.json').version
511
+ if (bindingPackageVersion !== '0.1.0' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
512
+ throw new Error(`Native binding package version mismatch, expected 0.1.0 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
513
+ }
514
+ return binding
515
+ } catch (e) {
516
+ loadErrors.push(e)
517
+ }
518
+ } else {
519
+ loadErrors.push(new Error(`Unsupported architecture on OpenHarmony: ${process.arch}`))
520
+ }
521
+ } else {
522
+ loadErrors.push(new Error(`Unsupported OS: ${process.platform}, architecture: ${process.arch}`))
523
+ }
524
+ }
525
+
526
+ nativeBinding = requireNative()
527
+
528
+ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
529
+ let wasiBinding = null
530
+ let wasiBindingError = null
531
+ try {
532
+ wasiBinding = require('./compression-rspack-plugin.wasi.cjs')
533
+ nativeBinding = wasiBinding
534
+ } catch (err) {
535
+ if (process.env.NAPI_RS_FORCE_WASI) {
536
+ wasiBindingError = err
537
+ }
538
+ }
539
+ if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
540
+ try {
541
+ wasiBinding = require('compression-rspack-plugin-wasm32-wasi')
542
+ nativeBinding = wasiBinding
543
+ } catch (err) {
544
+ if (process.env.NAPI_RS_FORCE_WASI) {
545
+ if (!wasiBindingError) {
546
+ wasiBindingError = err
547
+ } else {
548
+ wasiBindingError.cause = err
549
+ }
550
+ loadErrors.push(err)
551
+ }
552
+ }
553
+ }
554
+ if (process.env.NAPI_RS_FORCE_WASI === 'error' && !wasiBinding) {
555
+ const error = new Error('WASI binding not found and NAPI_RS_FORCE_WASI is set to error')
556
+ error.cause = wasiBindingError
557
+ throw error
558
+ }
559
+ }
560
+
561
+ if (!nativeBinding) {
562
+ if (loadErrors.length > 0) {
563
+ throw new Error(
564
+ `Cannot find native binding. ` +
565
+ `npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +
566
+ 'Please try `npm i` again after removing both package-lock.json and node_modules directory.',
567
+ {
568
+ cause: loadErrors.reduce((err, cur) => {
569
+ cur.cause = err
570
+ return cur
571
+ }),
572
+ },
573
+ )
574
+ }
575
+ throw new Error(`Failed to load native binding`)
576
+ }
577
+
578
+ module.exports = nativeBinding
579
+ module.exports.compressAssets = nativeBinding.compressAssets
package/index.d.cts ADDED
@@ -0,0 +1,30 @@
1
+ /* auto-generated by NAPI-RS */
2
+ /* eslint-disable */
3
+ export interface AssetInput {
4
+ filename: string
5
+ content: Buffer
6
+ }
7
+
8
+ /**
9
+ * Compress multiple assets in parallel using Rust-native gzip and brotli.
10
+ *
11
+ * All assets are compressed concurrently across all available CPU cores via rayon.
12
+ * A single FFI boundary crossing: JS sends all assets in, Rust returns all compressed
13
+ * variants out.
14
+ */
15
+ export declare function compressAssets(assets: Array<AssetInput>, options?: CompressOptions | undefined | null): Array<CompressedAsset>
16
+
17
+ export interface CompressedAsset {
18
+ filename: string
19
+ content: Buffer
20
+ algorithm: string
21
+ originalSize: number
22
+ compressedSize: number
23
+ }
24
+
25
+ export interface CompressOptions {
26
+ gzip?: boolean
27
+ brotli?: boolean
28
+ gzipLevel?: number
29
+ brotliQuality?: number
30
+ }
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "compression-rspack-plugin",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Rust-native parallel compression plugin for Rspack. Drop-in replacement for compression-webpack-plugin with gzip, brotli, deflate, and deflateRaw — all parallelized across CPU cores via Rust + rayon.",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist/index.*",
16
+ "dist/types.*",
17
+ "dist/helpers.*",
18
+ "dist/compressors/",
19
+ "dist/options.json",
20
+ "index.cjs",
21
+ "index.d.cts"
22
+ ],
23
+ "napi": {
24
+ "binaryName": "compression-rspack-plugin",
25
+ "targets": [
26
+ "aarch64-apple-darwin",
27
+ "x86_64-apple-darwin",
28
+ "x86_64-unknown-linux-gnu",
29
+ "aarch64-unknown-linux-gnu",
30
+ "x86_64-unknown-linux-musl"
31
+ ]
32
+ },
33
+ "scripts": {
34
+ "build:native": "napi build --platform --release",
35
+ "build:ts": "tsc -p tsconfig.build.json && cp lib/options.json dist/options.json",
36
+ "build": "pnpm build:native && pnpm build:ts",
37
+ "clean": "rm -rf dist",
38
+ "test:rust": "cargo test --lib",
39
+ "pretest": "pnpm build:ts",
40
+ "test": "NODE_OPTIONS='--experimental-vm-modules' jest",
41
+ "test:update": "NODE_OPTIONS='--experimental-vm-modules' jest -u",
42
+ "typecheck": "tsc --noEmit",
43
+ "lint": "biome check .",
44
+ "lint:fix": "biome check --write .",
45
+ "lint:rs": "cargo fmt --check && cargo clippy --lib -- -D warnings",
46
+ "prepublishOnly": "pnpm clean && pnpm build"
47
+ },
48
+ "keywords": [
49
+ "rspack",
50
+ "rspack-plugin",
51
+ "compression",
52
+ "gzip",
53
+ "brotli",
54
+ "deflate",
55
+ "rust",
56
+ "napi",
57
+ "parallel",
58
+ "performance"
59
+ ],
60
+ "author": "ramon-villain",
61
+ "license": "MIT",
62
+ "repository": {
63
+ "type": "git",
64
+ "url": "https://github.com/ramon-villain/compression-rspack-plugin.git"
65
+ },
66
+ "packageManager": "pnpm@10.8.1",
67
+ "engines": {
68
+ "node": ">=24"
69
+ },
70
+ "peerDependencies": {
71
+ "@rspack/core": ">=1.0.0"
72
+ },
73
+ "optionalDependencies": {
74
+ "compression-rspack-plugin-darwin-arm64": "0.1.0",
75
+ "compression-rspack-plugin-darwin-x64": "0.1.0",
76
+ "compression-rspack-plugin-linux-x64-gnu": "0.1.0",
77
+ "compression-rspack-plugin-linux-arm64-gnu": "0.1.0",
78
+ "compression-rspack-plugin-linux-x64-musl": "0.1.0"
79
+ },
80
+ "dependencies": {
81
+ "p-limit": "7.3.0",
82
+ "schema-utils": "4.3.2"
83
+ },
84
+ "devDependencies": {
85
+ "@biomejs/biome": "2.4.9",
86
+ "@napi-rs/cli": "3.0.0-alpha.86",
87
+ "@rspack/core": "2.0.0-beta.2",
88
+ "@types/node": "24.11.0",
89
+ "browserslist-load-config": "1.0.1",
90
+ "jest": "30.0.0-alpha.6",
91
+ "memfs": "4.17.0",
92
+ "typescript": "6.0.2",
93
+ "webpack-stats-plugin": "1.1.3"
94
+ }
95
+ }