Tor first loads drop from ~25 MB raw to ~5 MB on the wire; prod build writes .wasm.br sidecars and Alpine nginx serves them under /assets with long-lived cache.
33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
import { brotliCompressSync, constants as zlibConstants } from 'node:zlib';
|
|
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const distAssetsDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../dist/assets');
|
|
|
|
const brotliOptions = {
|
|
params: {
|
|
[zlibConstants.BROTLI_PARAM_QUALITY]: 11,
|
|
[zlibConstants.BROTLI_PARAM_LGWIN]: 24
|
|
}
|
|
};
|
|
|
|
const wasmFiles = readdirSync(distAssetsDir).filter(name => name.endsWith('.wasm'));
|
|
|
|
if (wasmFiles.length === 0) {
|
|
console.error('optimize-dist-wasm: no .wasm files in dist/assets');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('optimize-dist-wasm: brotli sidecars for', wasmFiles.length, 'file(s)');
|
|
|
|
for (const fileName of wasmFiles) {
|
|
const wasmPath = path.join(distAssetsDir, fileName);
|
|
console.log('optimize-dist-wasm: brotli', wasmPath);
|
|
|
|
const compressed = brotliCompressSync(readFileSync(wasmPath), brotliOptions);
|
|
console.log('optimize-dist-wasm: compressed', compressed.length);
|
|
|
|
writeFileSync(`${wasmPath}.br`, compressed);
|
|
}
|