Skip to content

Commit

Permalink
⚡️ minify all resourcepack JSON files in build
Browse files Browse the repository at this point in the history
- significantly cuts down on build size
  • Loading branch information
TheAfroOfDoom committed Oct 23, 2024
1 parent 0ce46a9 commit ecac8a1
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions package-scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const chalk = require('chalk');
const { copy, emptyDir, pathExists, readJson, writeJson } = require('fs-extra');
const { glob } = require('glob');
const parseArgs = require('minimist');
const { rimraf } = require('rimraf');

Expand Down Expand Up @@ -134,6 +135,7 @@ const getSummitDatapackPaths = () => {

const getSummitResourcepackPaths = () => {
const postProcessors = [];
const finalPostProcessors = [];

// Not `minecraft/sounds.json` since we just use that to disable ambient sounds
const minecraftPaths = prefixPaths('minecraft/', ['atlases', 'models']);
Expand Down Expand Up @@ -347,7 +349,14 @@ const getSummitResourcepackPaths = () => {
...assetsPaths,
]);

return { paths: resourcepackPaths, postProcessors };
const minifyJsons = async ({ compiledPath }) => {
for (const path of await glob(`${compiledPath}/**/*.json`)) {
await writeJson(path, await readJson(path));
}
};
finalPostProcessors.push(minifyJsons);

return { paths: resourcepackPaths, postProcessors, finalPostProcessors };
};

const LOG_LEVEL = {
Expand Down Expand Up @@ -437,7 +446,7 @@ const compile = async ({

await emptyDir(compiledPath);

const { paths, postProcessors } = compilePaths();
const { paths, postProcessors, finalPostProcessors = [] } = compilePaths();
if (args.verbose) {
verbose(chalk.bold(`${logColor(packType)} compile paths:`));
for (const src of paths) {
Expand All @@ -462,11 +471,17 @@ const compile = async ({
const checkmark = '\u{2705}';
info(`Finished copying ${paths.length} paths ${checkmark}`);

if (postProcessors.length > 0) {
info(`Running ${postProcessors.length} post-processors`);
const totalProcessors = postProcessors.length + finalPostProcessors.length;
if (totalProcessors > 0) {
info(`Running ${totalProcessors} post-processors`);
await Promise.all(
postProcessors.map((postProcessor) => postProcessor({ compiledPath })),
);
await Promise.all(
finalPostProcessors.map((postProcessor) =>
postProcessor({ compiledPath }),
),
);
info(`Finished post-processing ${checkmark}`);
}
};
Expand Down

0 comments on commit ecac8a1

Please sign in to comment.