From 22187fba8a2d404a9f3dc5db357e243cd45e8479 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 18 Sep 2022 09:27:43 +0200 Subject: [PATCH] Fix `mangle.json` indentation style not preserved (#999) --- .changeset/gold-berries-march.md | 5 +++++ src/index.js | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changeset/gold-berries-march.md diff --git a/.changeset/gold-berries-march.md b/.changeset/gold-berries-march.md new file mode 100644 index 00000000..a96679e4 --- /dev/null +++ b/.changeset/gold-berries-march.md @@ -0,0 +1,5 @@ +--- +'microbundle': patch +--- + +Fix indentation style in `mangle.json` not preserved diff --git a/src/index.js b/src/index.js index ddf90f2d..99e6a8fc 100644 --- a/src/index.js +++ b/src/index.js @@ -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, () => {}); }