-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTM-100: Pre-compress static resources
- Loading branch information
1 parent
19ee382
commit 3c0c424
Showing
4 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
const chalk = require('chalk'); | ||
|
||
const EXTENSIONS = ['.js', '.css', '.svg', '.map']; | ||
const COMPRESS_SIZE_THRESHOLD = 1024; | ||
|
||
// recursive option requires Node v20 | ||
const files = fs.readdirSync(path.resolve(__dirname, '../dist/app/'), { recursive: true, withFileTypes: true }) | ||
.filter(entry => entry.isFile()) | ||
.filter(entry => EXTENSIONS.includes(path.extname(entry.name))) | ||
.map(entry => ({ | ||
filename: path.join(entry.path, entry.name), | ||
displayName: path.join(entry.path, entry.name).split('/dist/')[1], | ||
})); | ||
|
||
const displayNamePad = files.reduce((previous, current) => Math.max(previous, current.displayName.length), 0) - 2; | ||
|
||
let bundleTotal = 0; | ||
let compressedTotal = 0; | ||
files.forEach(entry => { | ||
const { size } = fs.statSync(entry.filename); | ||
if (size > COMPRESS_SIZE_THRESHOLD) { | ||
process.stdout.write(`Compressing ${entry.displayName.padEnd(displayNamePad, ' ')} size ${(size + "").padStart(7, ' ')}, ratio: `); | ||
|
||
const contents = fs.readFileSync(entry.filename); | ||
|
||
const gzipped = zlib.gzipSync(contents, { level: 9 }); | ||
fs.writeFileSync(entry.filename + ".gz", gzipped); | ||
|
||
const ratio = (gzipped.length / size).toFixed(2); | ||
process.stdout.write(` ${chalk.bold(ratio)}\n`); | ||
|
||
if (entry.displayName.startsWith('app/en/') && path.extname(entry.filename) !== '.map') { | ||
bundleTotal += size; | ||
compressedTotal += gzipped.length; | ||
} | ||
} | ||
}); | ||
|
||
console.log(`Total single bundle ratio: ${chalk.green(chalk.bold((compressedTotal / bundleTotal).toFixed(2)))}, saved ${chalk.bold(((bundleTotal - compressedTotal) / 1024).toFixed(0))} KiB`); | ||
|
This file contains 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