mirror of
https://github.com/horsicq/Detect-It-Easy.git
synced 2026-06-24 01:54:08 +00:00
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:
parent
76524a8a6a
commit
ee3b2d03cf
1 changed files with 20 additions and 0 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue