Skip to content

Commit

Permalink
Fix mangle.json indentation style not preserved (#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister authored Sep 18, 2022
1 parent 124c9c6 commit 22187fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/gold-berries-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Fix indentation style in `mangle.json` not preserved
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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, () => {});
}
Expand Down

0 comments on commit 22187fb

Please sign in to comment.