-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add cache for known plugins We have to call `require.resolve(…)` ~10 times per call to `parse(…)`. These calls generally take microseconds however if you call `prettier.format(…)` several thousand times the times add up. It also adds up for embedded documents which call `parse(…)` a large number of times. This adds a cache for these calls because generally only a few will ever be installed and Node.js will not cache misses internally. * Update src/plugins.ts Co-authored-by: Robin Malfait <malfait.robin@gmail.com> * Update changelog --------- Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
- Loading branch information
1 parent
f69f739
commit ee50b9b
Showing
4 changed files
with
58 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { createRequire as req } from 'node:module' | ||
import { expiringMap } from './expiring-map' | ||
|
||
const localRequire = req(import.meta.url) | ||
|
||
// This is a long-lived cache for resolved modules whether they exist or not | ||
// Because we're compatible with a large number of plugins, we need to check | ||
// for the existence of a module before attempting to import it. This cache | ||
// is used to mitigate the cost of that check because Node.js does not cache | ||
// failed module resolutions making repeated checks very expensive. | ||
const resolveCache = expiringMap<string, string | null>(30_000) | ||
|
||
export function resolveIn(id: string, paths: string[]) { | ||
return localRequire.resolve(id, { | ||
paths, | ||
}) | ||
} | ||
|
||
export function maybeResolve(name: string) { | ||
let modpath = resolveCache.get(name) | ||
|
||
if (modpath === undefined) { | ||
modpath = freshMaybeResolve(name) | ||
resolveCache.set(name, modpath) | ||
} | ||
|
||
return modpath | ||
} | ||
|
||
export async function loadIfExists<T>(name: string): Promise<T | null> { | ||
let modpath = maybeResolve(name) | ||
|
||
if (modpath) { | ||
let mod = await import(name) | ||
return mod.default ?? mod | ||
} | ||
|
||
return null | ||
} | ||
|
||
function freshMaybeResolve(name: string) { | ||
try { | ||
return localRequire.resolve(name) | ||
} catch (err) { | ||
return null | ||
} | ||
} |