Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions .pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .yarn/versions/02da7597.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions packages/yarnpkg-pnp/sources/loader/nodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import fs from 'fs';
import path from 'path';

import {WATCH_MODE_MESSAGE_USES_ARRAYS} from '../esm-loader/loaderFlags';
import type {PackageJson} from '../types';

const packageJsonCache = new Map<NativePath, PackageJson | null>();

// https://github.com/nodejs/node/blob/e817ba70f56c4bfd5d4a68dce8b165142312e7b6/lib/internal/modules/cjs/loader.js#L315-L330
export function readPackageScope(checkPath: NativePath) {
Expand All @@ -26,13 +29,23 @@ export function readPackageScope(checkPath: NativePath) {
}

// https://github.com/nodejs/node/blob/e817ba70f56c4bfd5d4a68dce8b165142312e7b6/lib/internal/modules/cjs/loader.js#L284-L313
// Package manifest lookups should be cached: https://github.com/yarnpkg/berry/issues/7258
// Only the `type` field is used by the consumers, so we save on memory.
export function readPackage(requestPath: NativePath) {
const jsonPath = npath.resolve(requestPath, `package.json`);

if (!fs.existsSync(jsonPath))
return null;
const cached = packageJsonCache.get(jsonPath);
if (cached !== undefined)
return cached;

let data: PackageJson | null = null;
if (fs.existsSync(jsonPath)) {
const {type} = JSON.parse(fs.readFileSync(jsonPath, `utf8`)) as PackageJson;
data = {type};
}

return JSON.parse(fs.readFileSync(jsonPath, `utf8`));
packageJsonCache.set(jsonPath, data);
return data;
}

// https://github.com/nodejs/node/blob/972d9218559877f7fff4bb6086afacac8933f8d1/lib/internal/errors.js#L1450-L1478
Expand Down
4 changes: 4 additions & 0 deletions packages/yarnpkg-pnp/sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export type DependencyTarget =
export type PackageInformation<P extends Path> = {packageLocation: P, packageDependencies: Map<string, DependencyTarget>, packagePeers: Set<string>, linkType: LinkType, discardFromLookup: boolean};
export type PackageInformationData<P extends Path> = {packageLocation: P, packageDependencies: Array<[string, DependencyTarget]>, packagePeers?: Array<string>, linkType: LinkType, discardFromLookup?: boolean};

export type PackageJson = {
type?: `commonjs` | `module`;
};

export type PackageStore = Map<string | null, PackageInformation<PortablePath>>;
export type PackageStoreData = Array<[string | null, PackageInformationData<PortablePath>]>;

Expand Down
51 changes: 51 additions & 0 deletions packages/yarnpkg-pnp/tests/nodeUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';
import fs from 'fs';

import {readPackageScope} from '../sources/loader/nodeUtils';

describe(`nodeUtils`, () => {
it(`should cache present and missing package manifests`, () => {
xfs.mktempSync(tmpDir => {
const packageDir = ppath.join(tmpDir, `package`);
const moduleDir = ppath.join(packageDir, `lib/level-1/level-2`);
const packageJson = ppath.join(packageDir, Filename.manifest);

xfs.mkdirpSync(moduleDir);
xfs.writeJsonSync(packageJson, {
name: `test-package`,
description: `This field isn't needed to determine the module format`,
type: `module`,
});

const existsSync = jest.spyOn(fs, `existsSync`);
const readFileSync = jest.spyOn(fs, `readFileSync`);

try {
const firstScope = readPackageScope(npath.fromPortablePath(ppath.join(moduleDir, `first.js`)));
const secondScope = readPackageScope(npath.fromPortablePath(ppath.join(moduleDir, `second.js`)));

expect(firstScope).toEqual({
data: {type: `module`},
path: npath.fromPortablePath(packageDir),
});
expect(secondScope).toEqual(firstScope);

const checkedManifests = [
ppath.join(moduleDir, Filename.manifest),
ppath.join(ppath.dirname(moduleDir), Filename.manifest),
ppath.join(ppath.dirname(ppath.dirname(moduleDir)), Filename.manifest),
packageJson,
].map(manifestPath => npath.fromPortablePath(manifestPath));

for (const manifestPath of checkedManifests)
expect(existsSync.mock.calls.filter(([path]) => path === manifestPath)).toHaveLength(1);

const nativePackageJson = npath.fromPortablePath(packageJson);
expect(readFileSync.mock.calls.filter(([path]) => path === nativePackageJson)).toHaveLength(1);
} finally {
existsSync.mockRestore();
readFileSync.mockRestore();
}
});
});
});
Loading