Skip to content

Commit

Permalink
fix(objects): 优化 load json5 在 cjs 模式下的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jan 8, 2024
1 parent 9224709 commit 50aba5f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/common/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ export async function tryLoadJSON5() {
// @ts-ignore
if (globalThis.JSON5) JSON5 = globalThis.JSON5;
else {
const t = await import('json5');
JSON5 = (t.default || t) as never;
if (typeof require === 'function') JSON5 = require('json5');
else {
const t = await import('json5');
JSON5 = (t.default || t) as never;
}
}
} catch {
// quit
Expand Down
3 changes: 2 additions & 1 deletion src/node/LiteStorage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { homedir } from 'node:os';
import { dirname, resolve } from 'node:path';
import { fs } from './fs-system';
import { assign, safeJsonParse, safeStringify } from '../common/objects';
import { assign, safeJsonParse, safeStringify, tryLoadJSON5 } from '../common/objects';

export interface LSCache<T> {
version: string;
Expand Down Expand Up @@ -98,6 +98,7 @@ export class LiteStorage<T extends object = Record<string, unknown>> {
const TOML = await import('@iarna/toml');
localCache = JSON.parse(JSON.stringify(TOML.default.parse(content)));
} else {
await tryLoadJSON5();
localCache = safeJsonParse<never>(content, true) as LSCache<T>;
}

Expand Down

0 comments on commit 50aba5f

Please sign in to comment.