Skip to content

Instantly share code, notes, and snippets.

@heyimalex
Last active June 18, 2019 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heyimalex/db6e734d1c4e0ac50e0dc8f2abeeae4e to your computer and use it in GitHub Desktop.
Save heyimalex/db6e734d1c4e0ac50e0dc8f2abeeae4e to your computer and use it in GitHub Desktop.
CRA build with gzip
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const async = require("async");
const zlib = require("zlib");
function gzipFile(src, callback) {
console.log(`Compressing ${src}`);
const dst = `${src}.gz`;
fs.createReadStream(src)
.on("error", callback)
.pipe(
zlib.createGzip({
level: zlib.constants.Z_BEST_COMPRESSION
})
)
.on("error", callback)
.pipe(fs.createWriteStream(dst))
.on("error", callback)
.on("close", () => {
const srcSize = fs.statSync(src).size;
const dstSize = fs.statSync(dst).size;
console.log(` Saved ${srcSize - dstSize} bytes`);
console.log(` ${Math.floor((100 * dstSize) / srcSize)}% of original`);
callback();
});
}
function compressBuildArtifacts(callback) {
const assets = glob.sync("./build/**/*.@(js|css|html)");
async.eachSeries(assets, gzipFile, callback);
}
compressBuildArtifacts(err => {
if (err) {
throw err;
}
console.log("Compression completed sucessfully");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment