Skip to content

Conversation

@ha1fstack
Copy link

@ha1fstack ha1fstack commented Dec 11, 2025

closes #155

Problem

Yarn PnP can not resolve arbitary qualified resolutions (or packages) whose are not declared explicitly.

However this plugin relies on on-the-fly generated __mf__virtual package inside physical node_modules folder - so it was not compatible

Tries

I first tried to use Vite's virtual module (in-memory) instead of real fs. But it did not work well since this plugin seems to be relying on (the intend is not documented so this is a guess) Vite's dependency pre-bundling - but virtual modules cannot be pre-bundled.

Solution

The root cause was not fs, so I focused on the qualified / unqualified resolution part. Instead of making the generated code as a package (with package.json and importing it with name), using unqualified resolution (real path) + creating alias to that path to keep things simple solved the issue.

Also fixed the problematic searchPackageVersion util too since the packages needs to be resolved from the host package (the service) root not the vite-mf's package root.

Comment on lines +16 to 22
const targetRoot = rootDir || process.cwd();
const nodeModulesPath = join(targetRoot, 'node_modules');
if (!existsSync(nodeModulesPath)) {
mkdirSync(nodeModulesPath, { recursive: true });
}
cachedNodeModulesDir = nodeModulesPath;
return cachedNodeModulesDir;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using vite project's root instead of traversing for existing folder because pnp doesn't generate node_modules and this seems more natural

Comment on lines +93 to +97
const virtualDirPath = VirtualModule.getVirtualModuleDirPath();
(config.resolve as any).alias.push({
find: new RegExp(`^${virtualDir}/`),
replacement: `${virtualDirPath}/`,
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using alias to map path to __mf__virtual

function searchPackageVersion(sharedName: string): string | undefined {
try {
const sharedPath = require.resolve(sharedName);
const sharedPath = require.resolve(sharedName, { paths: [process.cwd()] });
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use resolved config root instead of cwd but this seems fine for now

@ha1fstack ha1fstack changed the title feat: pnp support feat: Yarn PnP support Dec 11, 2025
@ha1fstack ha1fstack marked this pull request as ready for review December 11, 2025 07:01
Copy link
Collaborator

@gioboa gioboa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @ha1fstack
Unfortunately, in these days I'm quite busy and I can't review the PR as fast as I usually do.
I'm going to look at it in the next days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Yarn support: Yarn PnP can't find __mf__virtual

2 participants