Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quick-wasps-itch.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
"microbundle": patch
---

feat: :sparkles: Closes #497: preserve trailing newline in mangle.json
13 changes: 11 additions & 2 deletions src/index.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@ import {
} from './lib/option-normalization';
import { getConfigFromPkgJson, getName } from './lib/package-info';
import { shouldCssModules, cssModulesConfig } from './lib/css-modules';
import { EOL } from 'os';

// Extensions to use when resolving modules
const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.es6', '.es', '.mjs'];
Expand DownExpand Up@@ -397,9 +398,13 @@ function createConfig(options, entry, format, writeMeta) {
const externalTest =
external.length === 0 ? id => false : id => externalPredicate.test(id);

let endsWithNewLine = false;

function loadNameCache() {
try {
nameCache = JSON.parse(fs.readFileSync(getNameCachePath(), 'utf8'));
const data = fs.readFileSync(getNameCachePath(), 'utf8');
endsWithNewLine = data.endsWith(EOL);
nameCache = JSON.parse(data);
// mangle.json can contain a "minify" field, same format as the pkg.mangle:
if (nameCache.minify) {
minifyOptions = Object.assign(
Expand DownExpand Up@@ -609,7 +614,11 @@ function createConfig(options, entry, format, writeMeta) {
if (writeMeta && nameCache) {
fs.writeFile(
getNameCachePath(),
JSON.stringify(nameCache, null, 2),
JSON.stringify(
endsWithNewLine ? `${nameCache}${EOL}` : nameCache,
null,
2,
),
() => {},
);
}
Expand Down