rsbuild-plugin-compress 1.0.0 → 1.0.2

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 ADDED
@@ -0,0 +1,25 @@
1
+ # rsbuild-plugin-compress
2
+
3
+ rsbuild-plugin-compress like compression-webpack-plugin
4
+
5
+ ```typescript
6
+ import { pluginCompress } from 'rsbuild-plugin-compress';
7
+ import { defineConfig } from '@rsbuild/core';
8
+
9
+ // 配置参数
10
+ interface CompressPluginOptions {
11
+ enableGzip?: boolean;
12
+ enableBrotli?: boolean;
13
+ test?: RegExp;
14
+ threshold?: number;
15
+ }
16
+
17
+ export default defineConfig({
18
+ plugins: [
19
+ pluginCompress({
20
+ enableGzip: true,
21
+ enableBrotli: false,
22
+ }),
23
+ ],
24
+ });
25
+ ```
package/compress.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ export interface CompressPluginOptions {
3
+ enableGzip?: boolean;
4
+ enableBrotli?: boolean;
5
+ test?: RegExp;
6
+ threshold?: number;
7
+ }
8
+ export declare const pluginCompress: (options?: CompressPluginOptions) => RsbuildPlugin;
package/package.json CHANGED
@@ -1,27 +1,30 @@
1
1
  {
2
2
  "name": "rsbuild-plugin-compress",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",
7
7
  "type": "module",
8
8
  "scripts": {
9
- "build": "tsdown"
9
+ "build": "tsc",
10
+ "prepublishOnly": "npm run build"
10
11
  },
11
12
  "devDependencies": {
12
13
  "@rsbuild/core": "^1.7.3",
13
14
  "@rspack/core": "^1.7.6",
14
15
  "@types/node": "^25.3.2",
15
- "tsdown": "^0.20.3",
16
16
  "typescript": "^5.9.3"
17
17
  },
18
18
  "exports": {
19
- ".": "./compress.mjs",
19
+ ".": {
20
+ "types": "./compress.d.ts",
21
+ "import": "./compress.js"
22
+ },
20
23
  "./package.json": "./package.json"
21
24
  },
22
25
  "files": [
23
- "compress.mjs",
24
- "compress.worker.js"
26
+ "compress.worker.js",
27
+ "compress.d.ts"
25
28
  ],
26
29
  "publishConfig": {
27
30
  "access": "public",
package/compress.mjs DELETED
@@ -1,59 +0,0 @@
1
- import { Worker } from "node:worker_threads";
2
- import * as os from "node:os";
3
- import * as path from "node:path";
4
- import * as fs from "node:fs";
5
-
6
- //#region compress.ts
7
- const pluginCompress = (options = {}) => {
8
- const { enableGzip = true, enableBrotli = true, test = /\.(js|css|html|svg)$/, threshold = 1024 } = options;
9
- const maxWorkers = Math.max(1, os.cpus().length - 1);
10
- const queue = [];
11
- return {
12
- name: "rsbuild-plugin-compress",
13
- setup(api) {
14
- if (api.context.action !== "build") {
15
- api.logger.info(`rsbuild-plugin-compress: ${api.context.action} mode, skip compression.`);
16
- return;
17
- } else api.logger.info(`rsbuild-plugin-compress: ${api.context.action} mode, start compression.`);
18
- api.modifyRsbuildConfig((config) => {});
19
- api.modifyRspackConfig((config) => {
20
- if (!config.plugins) config.plugins = [];
21
- config.plugins.push({ apply(compiler) {
22
- compiler.hooks.afterEmit.tapPromise("rsbuild-plugin-compress", async (compilation) => {
23
- const outDir = compiler.outputPath;
24
- for (const filename of Object.keys(compilation.assets)) {
25
- if (!test.test(filename)) continue;
26
- const asset = compilation.getAsset(filename);
27
- if (!asset) continue;
28
- const source = asset?.source?.source?.()?.toString("utf-8");
29
- if (!source || source.length < threshold) continue;
30
- const worker = new Worker(path.resolve(__dirname, "./compress.worker.js"), { workerData: {
31
- source,
32
- enableGzip,
33
- enableBrotli
34
- } });
35
- const job = new Promise((resolve, reject) => {
36
- worker.on("message", async (result) => {
37
- const filePath = path.join(outDir, filename);
38
- if (result.gzip) await fs.promises.writeFile(`${filePath}.gz`, result.gzip);
39
- if (result.brotli) await fs.promises.writeFile(`${filePath}.br`, result.brotli);
40
- resolve();
41
- });
42
- worker.on("error", reject);
43
- });
44
- queue.push(job);
45
- while (queue.length >= maxWorkers) {
46
- await Promise.race(queue);
47
- queue.splice(0, 1);
48
- }
49
- }
50
- await Promise.all(queue);
51
- });
52
- } });
53
- });
54
- }
55
- };
56
- };
57
-
58
- //#endregion
59
- export { pluginCompress };