feat: Closes #497: preserve trailing newline in mangle.json (#882)

Co-authored-by: Leah <github.leah@hrmny.sh>
This commit is contained in:
Mihir Kumar 2021-10-14 20:57:44 +05:30 committed by GitHub
parent 26f382a989
commit 2a0ca8843f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
"microbundle": patch
---
feat: :sparkles: Closes #497: preserve trailing newline in mangle.json

View File

@ -30,6 +30,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'];
@ -403,9 +404,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(
@ -618,7 +623,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,
),
() => {},
);
}