rsbuild-plugin-compress 1.0.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 +21 -0
- package/compress.mjs +59 -0
- package/compress.worker.js +28 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 yeser
|
|
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/compress.mjs
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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 };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// build-plugins/compress.worker.ts
|
|
2
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import zlib from 'node:zlib';
|
|
5
|
+
|
|
6
|
+
const gzip = promisify(zlib.gzip);
|
|
7
|
+
const brotliCompress = promisify(zlib.brotliCompress);
|
|
8
|
+
|
|
9
|
+
async function run() {
|
|
10
|
+
const { source, enableGzip, enableBrotli } = workerData;
|
|
11
|
+
const result = {};
|
|
12
|
+
|
|
13
|
+
if (enableGzip) {
|
|
14
|
+
result.gzip = await gzip(source, { level: 9 });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (enableBrotli) {
|
|
18
|
+
result.brotli = await brotliCompress(source, {
|
|
19
|
+
params: {
|
|
20
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
parentPort?.postMessage(result);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
run();
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rsbuild-plugin-compress",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsdown"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@rsbuild/core": "^1.7.3",
|
|
13
|
+
"@rspack/core": "^1.7.6",
|
|
14
|
+
"@types/node": "^25.3.2",
|
|
15
|
+
"tsdown": "^0.20.3",
|
|
16
|
+
"typescript": "^5.9.3"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./compress.mjs",
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"compress.mjs",
|
|
24
|
+
"compress.worker.js"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org"
|
|
29
|
+
}
|
|
30
|
+
}
|