Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve node_modules resolver #219

Merged
merged 5 commits into from Aug 24, 2021
Merged

Improve node_modules resolver #219

merged 5 commits into from Aug 24, 2021

Conversation

calebeby
Copy link
Member

@calebeby calebeby commented Aug 19, 2021

This PR finishes some edge-ish cases with resolution:

  1. The resolver should look in node_modules folders other than the main node_modules folder. This is important for if you have multiple versions of a package. Also, it is best not to rely on the hoisting feature of npm/yarn that is not guaranteed.
  2. The resolver should support symlinks in node_modules. Now it does.

For #1, here is an example:

node_modules
  preact (10.1.0)
  some-library
    preact (8.0.0)

Before this PR, PT only looks at node_modules/preact. This is bad for two reasons, one is that node_modules/preact isn't guaranteed to exist unless we have it as a direct dep, and also, it will always return 10.1.0, even though when importing from some-library, it should return 8.0.0.

This PR fixes that, so it will find the nearest node_modules to the importer that has the requested package.

Both of these changes are necessary for pnpm support. I tested it out by using pnpm with https://github.com/cloudfour/pleasantest-example-modal, linking my local PT in, and making sure the tess pass.

I added tests for #1 but not for #2. #2 can be tested manually:

Testing pnpm

In this repo:

git pull
git checkout good-resolver
npm i
npm run build
npm i -g pnpm
pnpm link --global

Clone https://github.com/cloudfour/pleasantest-example-modal

pnpm i
pnpm link --global pleasantest
npm run test

Closes #176

@calebeby calebeby marked this pull request as ready for review August 20, 2021 00:04
@calebeby calebeby changed the title Make the resolver good Improve node_modules resolver Aug 23, 2021
Copy link
Member

@Paul-Hebert Paul-Hebert left a comment

Choose a reason for hiding this comment

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

Code looks good and works as advertised 👍 🚢

@@ -67,30 +67,30 @@ export const jsMiddleware = async ({
try {
// Normalized path starting with slash
const path = posix.normalize(req.path);
const params = new URLSearchParams(req.query as Record<string, string>);
Copy link
Member

Choose a reason for hiding this comment

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

I wanted to flag the warnings above. They don't seem critical and I don't see them as a merge blocker but wanted to make sure you were aware.

(For some reason GitHub wouldn't let me comment directly on them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks 👍

I'm not sure whether at this point there is a good opportunity to factor out part of this function. For now I think I'll leave it as-is but I'll keep my eyes out for refactoring opportunities 👀

Copy link
Member

Choose a reason for hiding this comment

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

Cool, works for me 👍

@calebeby calebeby merged commit f0ee064 into main Aug 24, 2021
@calebeby calebeby deleted the good-resolver branch August 24, 2021 14:52
@github-actions github-actions bot mentioned this pull request Aug 24, 2021
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.

Test out resolution with pnpm
2 participants