From 3cfdf706afa37c8e77187090c6b0d641e3b0c53a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 26 Sep 2022 11:34:51 +0100 Subject: [PATCH 1/2] perf: cache package.json + tsconfig.json results --- src/index.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5366eff..a2e7b90 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ export * from './types' export * from './utils' export type ResolveOptions = _ResolveOptions & FindFileOptions +export type ReadOptions = { cache?: boolean } export function definePackageJSON (pkg: PackageJson): PackageJson { return pkg @@ -18,21 +19,33 @@ export function defineTSConfig (tsconfig: TSConfig): TSConfig { return tsconfig } -export async function readPackageJSON (id?: string, opts: ResolveOptions = {}): Promise { +const FileCache = new Map>() + +export async function readPackageJSON (id?: string, opts: ResolveOptions & ReadOptions = {}): Promise { const resolvedPath = await resolvePackageJSON(id, opts) + if (opts.cache && FileCache.has(resolvedPath)) { + return FileCache.get(resolvedPath)! + } const blob = await fsp.readFile(resolvedPath, 'utf-8') - return JSON.parse(blob) as PackageJson + const parsed = JSON.parse(blob) as PackageJson + FileCache.set(resolvedPath, parsed) + return parsed } export async function writePackageJSON (path: string, pkg: PackageJson): Promise { await fsp.writeFile(path, JSON.stringify(pkg, null, 2)) } -export async function readTSConfig (id?: string, opts: ResolveOptions = {}): Promise { +export async function readTSConfig (id?: string, opts: ResolveOptions & ReadOptions = {}): Promise { const resolvedPath = await resolveTSConfig(id, opts) + if (opts.cache && FileCache.has(resolvedPath)) { + return FileCache.get(resolvedPath)! + } const blob = await fsp.readFile(resolvedPath, 'utf-8') const jsonc = await import('jsonc-parser') - return jsonc.parse(blob) as TSConfig + const parsed = jsonc.parse(blob) as TSConfig + FileCache.set(resolvedPath, parsed) + return parsed } export async function writeTSConfig (path: string, tsconfig: TSConfig): Promise { From f62cf5e9824eda2939b66dd1d0a8fdf592ca2806 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 26 Sep 2022 14:01:07 +0100 Subject: [PATCH 2/2] feat: support passing user cache --- src/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index a2e7b90..40bc0ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ export * from './types' export * from './utils' export type ResolveOptions = _ResolveOptions & FindFileOptions -export type ReadOptions = { cache?: boolean } +export type ReadOptions = { cache?: boolean | Map> } export function definePackageJSON (pkg: PackageJson): PackageJson { return pkg @@ -23,12 +23,13 @@ const FileCache = new Map>() export async function readPackageJSON (id?: string, opts: ResolveOptions & ReadOptions = {}): Promise { const resolvedPath = await resolvePackageJSON(id, opts) - if (opts.cache && FileCache.has(resolvedPath)) { - return FileCache.get(resolvedPath)! + const cache = opts.cache && typeof opts.cache !== 'boolean' ? opts.cache : FileCache + if (opts.cache && cache.has(resolvedPath)) { + return cache.get(resolvedPath)! } const blob = await fsp.readFile(resolvedPath, 'utf-8') const parsed = JSON.parse(blob) as PackageJson - FileCache.set(resolvedPath, parsed) + cache.set(resolvedPath, parsed) return parsed } @@ -38,13 +39,14 @@ export async function writePackageJSON (path: string, pkg: PackageJson): Promise export async function readTSConfig (id?: string, opts: ResolveOptions & ReadOptions = {}): Promise { const resolvedPath = await resolveTSConfig(id, opts) - if (opts.cache && FileCache.has(resolvedPath)) { - return FileCache.get(resolvedPath)! + const cache = opts.cache && typeof opts.cache !== 'boolean' ? opts.cache : FileCache + if (opts.cache && cache.has(resolvedPath)) { + return cache.get(resolvedPath)! } const blob = await fsp.readFile(resolvedPath, 'utf-8') const jsonc = await import('jsonc-parser') const parsed = jsonc.parse(blob) as TSConfig - FileCache.set(resolvedPath, parsed) + cache.set(resolvedPath, parsed) return parsed }