[Optimization] Minify json files in build directory (after beta rebase) (#3214)

* add and use minify-json vite plugin

it minifies all the json files in the given directorties (currently `/dist/images` and `/dist/battle-anim`). That way we can work with the beautified jsons but the users retrieve the minified ones.

* bump vite to v5 to fix typedoc issue

https://github.com/vitejs/vite/issues/15714
This commit is contained in:
flx-sta 2024-07-29 16:13:36 -07:00 committed by GitHub
parent b573076789
commit d3e5a68f10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1122 additions and 2239 deletions

3297
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -37,7 +37,7 @@
"typedoc": "^0.26.4",
"typescript": "^5.5.3",
"typescript-eslint": "^7.10.0",
"vite": "^4.5.0",
"vite": "^5.3.4",
"vite-plugin-fs": "^0.4.4",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.4.0",

View File

@ -0,0 +1,56 @@
import path from "path";
import fs from "fs";
import { type Plugin as VitePlugin } from "vite";
/**
* Crawl a directory (recursively if wanted) for json files and minifies found ones.
* @param dir the directory to crawl
* @param recursive if true, will crawl subdirectories
*/
function applyToDir(dir: string, recursive?: boolean) {
const files = fs.readdirSync(dir).filter((file) => !/^\..*/.test(file));
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory() && recursive) {
applyToDir(filePath, recursive); // only if recursive is true
} else if (path.extname(file) === ".json") {
const contents = fs.readFileSync(filePath, "utf8");
const minifiedContent = JSON.stringify(JSON.parse(contents));
fs.writeFileSync(filePath, minifiedContent, "utf8");
}
}
}
/**
* Plugin to mnify json files in the build folder after the bundling is done.
* @param basePath base path/es starting inside the build dir (e.g. will always start with "/dist" if dist is the build dir)
* @param recursive if true, will crawl subdirectories
*/
export function minifyJsonPlugin(basePath: string | string[], recursive?: boolean): VitePlugin {
let buildDir = "dist"; // Default build dir
return {
name: "flx-minify-json",
configResolved(config) {
buildDir = config.build.outDir; // Read the build output directory from Vite config
},
async closeBundle() {
console.log("Minifying JSON files...");
const basePathes = Array.isArray(basePath) ? basePath : [basePath];
basePathes.forEach((basePath) => {
const baseDir = path.resolve(buildDir, basePath);
if (fs.existsSync(baseDir)) {
applyToDir(baseDir, recursive);
} else {
console.error(`Path ${baseDir} does not exist!`);
}
});
console.log("Finished minifying JSON files!");
},
};
}

View File

@ -1,8 +1,12 @@
import { defineConfig, loadEnv } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import { minifyJsonPlugin } from "./src/plugins/vite/vite-minify-json-plugin";
export const defaultConfig = {
plugins: [tsconfigPaths() as any],
plugins: [
tsconfigPaths() as any,
minifyJsonPlugin(["images", "battle-anims"], true)
],
clearScreen: false,
build: {
minify: 'esbuild' as const,