Add JSON minification handling in worker

Add isJson helper and handle .json files specially: attempt to minify by JSON.parse+JSON.stringify and write result; on parse error fall back to writing the original file and mark the result as failed. Update result flags to reflect whether the file was minified, skipped, failed, or failed-skip so callers can distinguish outcomes. This ensures JSON assets are compacted and errors are handled gracefully.
This commit is contained in:
DosX 2026-05-09 16:29:00 +03:00
commit ee3b2d03cf

View file

@ -21,6 +21,10 @@ function shouldMinify(filePath) {
return ext === ".sg" || ext === "";
}
function isJson(filePath) {
return path.extname(filePath).toLowerCase() === ".json";
}
/**
* Universal safe JavaScript parser
* Skips strings, regular expressions and comments
@ -354,6 +358,22 @@ try {
fs.mkdirSync(path.dirname(dstFile), { recursive: true });
const wasWritten = writeIfChanged(dstFile, text);
result.success = false;
result.type = wasWritten ? 'failed' : 'failed-skip';
result.error = e.message;
}
} else if (isJson(srcFile)) {
try {
const minified = JSON.stringify(JSON.parse(text));
fs.mkdirSync(path.dirname(dstFile), { recursive: true });
const wasWritten = writeIfChanged(dstFile, minified);
result.success = true;
result.type = wasWritten ? 'minified' : 'skipped';
} catch (e) {
fs.mkdirSync(path.dirname(dstFile), { recursive: true });
const wasWritten = writeIfChanged(dstFile, text);
result.success = false;
result.type = wasWritten ? 'failed' : 'failed-skip';
result.error = e.message;