Skip to content

Commit 118bded

Browse files
authored
fix(pnp): report loaded modules in watch mode (#4934)
1 parent b56e39d commit 118bded

File tree

8 files changed

+69
-8
lines changed

8 files changed

+69
-8
lines changed

.gitattributes

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* text=auto eol=lf
55

66
# Mark generated files as binary to prevent git from trying to merge them
7-
/.pnp.* binary linguist-generated
8-
packages/yarnpkg-pnp/sources/hook.js binary linguist-generated
9-
packages/yarnpkg-core/sources/worker-zip/index.js binary linguist-generated
10-
packages/yarnpkg-libzip/sources/libzipAsync.js binary linguist-generated
11-
packages/yarnpkg-libzip/sources/libzipSync.js binary linguist-generated
7+
/.pnp.* binary linguist-generated
8+
packages/yarnpkg-pnp/sources/esm-loader/built-loader.js binary linguist-generated
9+
packages/yarnpkg-pnp/sources/hook.js binary linguist-generated
10+
packages/yarnpkg-core/sources/worker-zip/index.js binary linguist-generated
11+
packages/yarnpkg-libzip/sources/libzipAsync.js binary linguist-generated
12+
packages/yarnpkg-libzip/sources/libzipSync.js binary linguist-generated
1213

1314
# Set the language for these files to json5 to ensure GitHub doesn't show the comments as errors
1415
/.vscode/*.json linguist-language=JSON5

.pnp.cjs

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.yarn/versions/cd516e64.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-pnp": patch
4+
"@yarnpkg/pnp": patch
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-init"
12+
- "@yarnpkg/plugin-interactive-tools"
13+
- "@yarnpkg/plugin-nm"
14+
- "@yarnpkg/plugin-npm-cli"
15+
- "@yarnpkg/plugin-pack"
16+
- "@yarnpkg/plugin-patch"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"
25+
- "@yarnpkg/nm"
26+
- "@yarnpkg/pnpify"
27+
- "@yarnpkg/sdks"

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The following changes only affect people writing Yarn plugins:
7676
- The patched filesystem now supports `fchown`.
7777
- PnP now handles private import mappings.
7878
- Updates the PnP compatibility layer for TypeScript v4.8.4 and v4.9.1-beta.
79+
- PnP now reports loaded modules when in watch mode.
7980

8081
### Shell
8182

packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-pnp/sources/esm-loader/hooks/load.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {VirtualFS, npath} from '@yarnpkg/fslib';
12
import fs from 'fs';
2-
import {fileURLToPath} from 'url';
3+
import {fileURLToPath, pathToFileURL} from 'url';
34

45
import {HAS_JSON_IMPORT_ASSERTION_REQUIREMENT} from '../loaderFlags';
56
import * as loaderUtils from '../loaderUtils';
@@ -31,6 +32,20 @@ export async function load(
3132
throw err;
3233
}
3334

35+
// https://github.com/nodejs/node/pull/44366/files#diff-f6796082f599554ec3a29c47cf026cb24fc5104884f2632e472c05fe622d778bR477-R479
36+
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
37+
// At the time of writing Node.js reports all loaded URLs itself so
38+
// we technically only need to do this for virtual files but in the
39+
// event that ever changes we report everything.
40+
process.send({
41+
'watch:import': pathToFileURL(
42+
npath.fromPortablePath(
43+
VirtualFS.resolveVirtual(npath.toPortablePath(filePath)),
44+
),
45+
).href,
46+
});
47+
}
48+
3449
return {
3550
format,
3651
source: await fs.promises.readFile(filePath, `utf8`),

packages/yarnpkg-pnp/sources/hook.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/yarnpkg-pnp/sources/loader/applyPatch.ts

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ declare global {
2727
}
2828
}
2929

30+
// https://github.com/nodejs/node/pull/44366
31+
const shouldReportRequiredModules = process.env.WATCH_REPORT_DEPENDENCIES;
32+
function reportModuleToWatchMode(filename: NativePath) {
33+
if (shouldReportRequiredModules && process.send) {
34+
process.send({'watch:require': npath.fromPortablePath(VirtualFS.resolveVirtual(npath.toPortablePath(filename)))});
35+
}
36+
}
37+
3038
export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
3139
/**
3240
* The cache that will be used for all accesses occurring outside of a PnP context.
@@ -167,6 +175,8 @@ export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
167175
const module = new Module(modulePath, parent ?? undefined) as PatchedModule;
168176
module.pnpApiPath = moduleApiPath;
169177

178+
reportModuleToWatchMode(modulePath);
179+
170180
entry.cache[modulePath] = module;
171181

172182
// The main module is exposed as a global variable

0 commit comments

Comments
 (0)