Skip to content

Commit

Permalink
perf(node-modules): cache install state (#2576)
Browse files Browse the repository at this point in the history
* perf(node-modules): cache install state

* Update NodeModulesLinker.ts

Co-authored-by: Maël Nison <nison.mael@gmail.com>
  • Loading branch information
merceyz and arcanis committed Mar 8, 2021
1 parent 9d1734d commit efd890a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .yarn/versions/0b00790b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-node-modules": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
12 changes: 10 additions & 2 deletions packages/plugin-node-modules/sources/NodeModulesLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type BinSymlinkMap = Map<PortablePath, Map<Filename, PortablePath>>;
type LoadManifest = (locator: LocatorKey, installLocation: PortablePath) => Promise<Pick<Manifest, 'bin'>>;

export class NodeModulesLinker implements Linker {
private installStateCache: Map<string, Promise<InstallState | null>> = new Map();

supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
return opts.project.configuration.get(`nodeLinker`) === `node-modules`;
}
Expand All @@ -33,7 +35,10 @@ export class NodeModulesLinker implements Linker {
if (workspace)
return workspace.cwd;

const installState = await findInstallState(opts.project, {unrollAliases: true});
const installState = await miscUtils.getFactoryWithDefault(this.installStateCache, opts.project.cwd, async () => {
return await findInstallState(opts.project, {unrollAliases: true});
});

if (installState === null)
throw new UsageError(`Couldn't find the node_modules state file - running an install might help (findPackageLocation)`);

Expand All @@ -48,7 +53,10 @@ export class NodeModulesLinker implements Linker {
}

async findPackageLocator(location: PortablePath, opts: LinkOptions) {
const installState = await findInstallState(opts.project, {unrollAliases: true});
const installState = await miscUtils.getFactoryWithDefault(this.installStateCache, opts.project.cwd, async () => {
return await findInstallState(opts.project, {unrollAliases: true});
});

if (installState === null)
return null;

Expand Down

0 comments on commit efd890a

Please sign in to comment.