Skip to content

Commit

Permalink
Add zstd compressor
Browse files Browse the repository at this point in the history
Fixes parcel-bundler#10014

Signed-off-by: solonovamax <[email protected]>
  • Loading branch information
solonovamax committed Dec 25, 2024
1 parent 393f497 commit 6e4bc19
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 19 deletions.
26 changes: 26 additions & 0 deletions packages/compressors/zstd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@parcel/compressor-zstd",
"version": "2.13.3",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/parcel"
},
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"main": "lib/ZstdCompressor.js",
"source": "src/ZstdCompressor.js",
"engines": {
"node": ">= 16.0.0",
"parcel": "^2.13.3"
},
"dependencies": {
"@mongodb-js/zstd": "^2.0.0",
"@parcel/plugin": "2.13.3"
}
}
26 changes: 26 additions & 0 deletions packages/compressors/zstd/src/ZstdCompressor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @flow
import {Compressor} from '@parcel/plugin';
import {compress} from '@mongodb-js/zstd';
import {Transform} from 'stream';

export default (new Compressor({
compress({options, stream}) {
if (options.mode !== 'production') {
return null;
}

return {
stream: stream.pipe(
new Transform({
transform(chunk, encoding, cb) {
compress(chunk, 19).then(
compressed => cb(null, compressed),
error => cb(error, null),
);
},
}),
),
type: 'zst',
};
},
}): Compressor);
3 changes: 2 additions & 1 deletion packages/core/integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"tailwindcss": "^3.0.2",
"tempy": "^0.3.0",
"wasm-sourcemap": "^1.0.0",
"ws": "^8.18.0"
"ws": "^8.18.0",
"@mongodb-js/zstd": "^2.0.0"
}
}
28 changes: 18 additions & 10 deletions packages/core/integration-tests/test/compressors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,44 @@ import assert from 'assert';
import path from 'path';
import zlib from 'zlib';
import {bundle, outputFS, distDir} from '@parcel/test-utils';
import {decompress} from '@mongodb-js/zstd';

describe('compressors', function () {
it('should not compress output with gzip and brotli in development', async function () {
it('should not compress output with gzip, brotli, or zstd in development', async function () {
await bundle(path.join(__dirname, 'integration/compressors/index.js'));

let output = await outputFS.readdir(distDir);
assert.deepEqual(output.sort(), ['index.js', 'index.js.map']);
});

it('should compress output with gzip and brotli', async function () {
it('should compress output with gzip, brotli, and zstd', async function () {
await bundle(path.join(__dirname, 'integration/compressors/index.js'), {
mode: 'production',
});

let output = await outputFS.readdir(distDir);
assert.deepEqual(output.sort(), [
'index.js',
'index.js.br',
'index.js.gz',
'index.js.map',
'index.js.map.br',
'index.js.map.gz',
]);
assert.deepEqual(
output.sort(),
[
'index.js',
'index.js.br',
'index.js.gz',
'index.js.zst',
'index.js.map',
'index.js.map.br',
'index.js.map.gz',
'index.js.map.zst',
].sort(),
);

let raw = await outputFS.readFile(path.join(distDir, 'index.js'));
let gz = await outputFS.readFile(path.join(distDir, 'index.js.gz'));
let br = await outputFS.readFile(path.join(distDir, 'index.js.br'));
let zstd = await outputFS.readFile(path.join(distDir, 'index.js.zst'));

assert(zlib.gunzipSync(gz).equals(raw));
assert(zlib.brotliDecompressSync(br).equals(raw));
assert((await decompress(zstd)).equals(raw));
});

it('should be able to disable raw output', async function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": ["@parcel/config-default"],
"compressors": {
"*": ["...", "@parcel/compressor-gzip", "@parcel/compressor-brotli"]
"*": ["...", "@parcel/compressor-gzip", "@parcel/compressor-brotli", "@parcel/compressor-zstd"]
}
}
Loading

0 comments on commit 6e4bc19

Please sign in to comment.