Last active
June 18, 2019 19:07
-
-
Save heyimalex/db6e734d1c4e0ac50e0dc8f2abeeae4e to your computer and use it in GitHub Desktop.
CRA build with gzip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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