mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-09-07 15:15:49 +08:00
chore: minify json files (@fehmer) (#6404)
This commit is contained in:
parent
b1d75fb077
commit
d181406e31
1 changed files with 64 additions and 0 deletions
|
@ -10,6 +10,7 @@ import { checker } from "vite-plugin-checker";
|
|||
import { writeFileSync } from "fs";
|
||||
// eslint-disable-next-line import/no-unresolved
|
||||
import UnpluginInjectPreload from "unplugin-inject-preload/vite";
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
|
||||
function pad(numbers, maxLength, fillString) {
|
||||
return numbers.map((number) =>
|
||||
|
@ -169,6 +170,69 @@ export default {
|
|||
],
|
||||
injectTo: "head-prepend",
|
||||
}),
|
||||
{
|
||||
name: "minify-json",
|
||||
apply: "build",
|
||||
generateBundle() {
|
||||
let totalOriginalSize = 0;
|
||||
let totalMinifiedSize = 0;
|
||||
|
||||
const minifyJsonFiles = (dir) => {
|
||||
readdirSync(dir).forEach((file) => {
|
||||
const sourcePath = path.join(dir, file);
|
||||
const stat = statSync(sourcePath);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
minifyJsonFiles(sourcePath);
|
||||
} else if (path.extname(file) === ".json") {
|
||||
const originalContent = readFileSync(sourcePath, "utf8");
|
||||
const originalSize = Buffer.byteLength(originalContent, "utf8");
|
||||
const minifiedContent = JSON.stringify(
|
||||
JSON.parse(originalContent)
|
||||
);
|
||||
const minifiedSize = Buffer.byteLength(minifiedContent, "utf8");
|
||||
|
||||
totalOriginalSize += originalSize;
|
||||
totalMinifiedSize += minifiedSize;
|
||||
|
||||
const savings =
|
||||
((originalSize - minifiedSize) / originalSize) * 100;
|
||||
|
||||
writeFileSync(sourcePath, minifiedContent);
|
||||
|
||||
console.log(
|
||||
`\x1b[0m \x1b[36m${sourcePath}\x1b[0m | ` +
|
||||
`\x1b[90mOriginal: ${originalSize} bytes\x1b[0m | ` +
|
||||
`\x1b[90mMinified: ${minifiedSize} bytes\x1b[0m | ` +
|
||||
`\x1b[32mSavings: ${savings.toFixed(2)}%\x1b[0m`
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
console.log("\n\x1b[1mMinifying JSON files...\x1b[0m\n");
|
||||
|
||||
minifyJsonFiles("./dist");
|
||||
|
||||
const totalSavings =
|
||||
((totalOriginalSize - totalMinifiedSize) / totalOriginalSize) * 100;
|
||||
|
||||
console.log(
|
||||
`\n\x1b[1mMinification Summary:\x1b[0m\n` +
|
||||
` \x1b[90mTotal original size: ${(
|
||||
totalOriginalSize /
|
||||
1024 /
|
||||
1024
|
||||
).toFixed(2)} mB\x1b[0m\n` +
|
||||
` \x1b[90mTotal minified size: ${(
|
||||
totalMinifiedSize /
|
||||
1024 /
|
||||
1024
|
||||
).toFixed(2)} mB\x1b[0m\n` +
|
||||
` \x1b[32mTotal savings: ${totalSavings.toFixed(2)}%\x1b[0m\n`
|
||||
);
|
||||
},
|
||||
},
|
||||
],
|
||||
build: {
|
||||
emptyOutDir: true,
|
||||
|
|
Loading…
Add table
Reference in a new issue