Skip to content

Commit 1608fe8

Browse files
committed
Cache module versions to speed up plugin
Many module versions are looked up repeatedly
1 parent f0d221c commit 1608fe8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

packages/esbuild-plugin-node/src/plugin.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ function isBuiltIn(path: string, extractedModule: ExtractedModule): boolean {
208208
);
209209
}
210210

211+
const moduleVersionByPackageJsonPath = new Map<string, string>();
212+
211213
async function getModuleVersion({
212214
extractedModule,
213215
resolveDir,
@@ -217,17 +219,20 @@ async function getModuleVersion({
217219
resolveDir: string;
218220
build: PluginBuild;
219221
}) {
220-
const { path: packageJsonPath } = await build.resolve(
221-
`${extractedModule.package}/package.json`,
222-
{
223-
resolveDir,
224-
kind: 'require-resolve',
225-
}
226-
);
222+
const path = `${extractedModule.package}/package.json`;
223+
const contents = moduleVersionByPackageJsonPath.get(path);
224+
if (contents) return contents;
225+
226+
const { path: packageJsonPath } = await build.resolve(path, {
227+
resolveDir,
228+
kind: 'require-resolve',
229+
});
227230
if (!packageJsonPath) return;
228231

229232
const packageJsonContents = await readFile(packageJsonPath);
230-
return JSON.parse(packageJsonContents.toString()).version;
233+
const moduleVersion = JSON.parse(packageJsonContents.toString()).version;
234+
moduleVersionByPackageJsonPath.set(path, moduleVersion);
235+
return moduleVersion;
231236
}
232237

233238
function getInstrumentation({

0 commit comments

Comments
 (0)