feat(es/loader): add opt-in Yarn PnP resolver (refs #3247) - #11823
feat(es/loader): add opt-in Yarn PnP resolver (refs #3247)#11823Mukunda Rao Katta (MukundaKatta) wants to merge 2 commits into
Conversation
Adds a `pnp` cargo feature on `swc_ecma_loader` that enables a new `PnpResolver`. The resolver reads `.pnp.data.json` (the JSON manifest emitted by Yarn when `pnpEnableInlining: false`) so resolution works without executing `.pnp.cjs`. - New module `crates/swc_ecma_loader/src/resolvers/pnp.rs`. - Feature `pnp` gates the module and only pulls `serde_json`. - `PnpResolver::discover` walks parent dirs for the manifest. - Implements the `Resolve` trait for bare specifiers; relative/absolute imports and node builtins are passed through so callers can compose with `NodeModulesResolver`. - Unit tests cover specifier splitting, manifest discovery, dependency resolution and pass-through cases. Refs swc-project#3247.
🦋 Changeset detectedLatest commit: 863aac4 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub. |
|
Warning Review the following alerts detected in dependencies. According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.
|
Merging this PR will not alter performance
Comparing Footnotes
|
Binary Sizes
Commit: ece9042 |
Summary
Refs #3247. Adds an opt-in Yarn Plug'n'Play resolver to
swc_ecma_loader. Addresses the maintainer's blocker (running.pnp.cjsJS in a Rust host) by reading the JSON manifest variant.pnp.data.json, which Yarn emits whenpnpEnableInlining: falseis set.Files changed
crates/swc_ecma_loader/src/resolvers/pnp.rs(new, 539 lines incl. 6 unit tests)crates/swc_ecma_loader/src/resolvers/mod.rs— module gatecrates/swc_ecma_loader/Cargo.toml— new opt-inpnpfeature; only addsserde_json;tempfileas dev-depCargo.lock— auto-updated for tempfile dev-depPublic API
PnpResolver::new(path)andPnpResolver::discover(start_dir)resolve_bare(from, specifier)Resolvetrait implBare specifiers (incl. scoped) handled. Relative/absolute/node-builtin specifiers pass through (returns
Ok(None)) so callers compose withNodeModulesResolver. Manifest cached inArc.Scope notes
Resolver intentionally stops at the package directory + subpath and lets the existing node resolver handle main/exports — avoids logic duplication. Maintainers may prefer a fuller
package.json["exports"]/zip-fs walk; happy to extend if so.Test plan
cargo fmt -p swc_ecma_loader --checkcleancargo clippy -p swc_ecma_loader --features pnp --all-targets -- -D warningscleancargo test -p swc_ecma_loader --features pnppasses 6/6 (split_specifier, discover hit/miss, resolve_bare hit, relative/builtin pass-through, unknown-module pass-through)