Fix mangle.json indentation style not preserved (#999)

This commit is contained in:
Marvin Hagemeister 2022-09-18 09:27:43 +02:00 committed by GitHub
parent 124c9c607c
commit 22187fba8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
'microbundle': patch
---
Fix indentation style in `mangle.json` not preserved

View File

@ -408,9 +408,11 @@ function createConfig(options, entry, format, writeMeta) {
let endsWithNewLine = false;
let nameCacheIndentTabs = false;
function loadNameCache() {
try {
const data = fs.readFileSync(getNameCachePath(), 'utf8');
nameCacheIndentTabs = /^\t+/gm.test(data);
endsWithNewLine = data.endsWith(EOL);
nameCache = JSON.parse(data);
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
@ -628,7 +630,11 @@ function createConfig(options, entry, format, writeMeta) {
writeBundle() {
if (writeMeta && nameCache) {
let filename = getNameCachePath();
let json = JSON.stringify(nameCache, null, 2);
let json = JSON.stringify(
nameCache,
null,
nameCacheIndentTabs ? '\t' : 2,
);
if (endsWithNewLine) json += EOL;
fs.writeFile(filename, json, () => {});
}