-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compress script to apply gzip to assets not packed by webpack
Signed-off-by: 1000TurquoisePogs <[email protected]>
- Loading branch information
1 parent
157d4b3
commit d49a563
Showing
3 changed files
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { execSync } from 'node:child_process'; | ||
|
||
const GZIP_SIZE_MIN = 50000; //50 KB | ||
const GZIP_ATTEMPT_MAX = 1000000000; //1 GB. Don't try compressing large files, these likely are incompressible binary. | ||
const KEEP_RATIO = 0.85; // Items not compressed below this will not be kept as compressed | ||
|
||
//const COMPRESSIBLE_TYPES = ['.js', '.css', '.html', '.json', '.xml', '.xlf', | ||
|
||
function recurse(directory) { | ||
const listing = fs.readdirSync(directory, {withFileTypes: true}); | ||
listing.forEach((dirent)=> { | ||
let itemPath = path.join(directory, dirent.name); | ||
if (dirent.isDirectory()) { | ||
recurse(itemPath); | ||
} else { | ||
let stat = fs.statSync(itemPath); | ||
if ((stat.size >= GZIP_SIZE_MIN) && (stat.size <= GZIP_ATTEMPT_MAX)) { | ||
execSync('gzip -k '+dirent.name, {cwd: directory}); | ||
let itemPathGz = path.join(directory, dirent.name+'.gz'); | ||
let statGz = fs.statSync(itemPathGz); | ||
//keep only one file: compressed file is kept if meaningfully smaller. | ||
if ((stat.size*KEEP_RATIO) < statGz.size) { | ||
fs.unlinkSync(itemPathGz); | ||
} else { | ||
fs.unlinkSync(itemPath); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
try { | ||
const listing = fs.readdirSync('web'); | ||
} catch (e) { | ||
console.log('Could not read web directory, run build first.'); | ||
process.exit(1); | ||
} | ||
|
||
recurse('web'); |
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