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

Support Yarn PnP resolver out of the box #253

Closed
koshic opened this issue Oct 14, 2023 · 11 comments · Fixed by #255
Closed

Support Yarn PnP resolver out of the box #253

koshic opened this issue Oct 14, 2023 · 11 comments · Fixed by #255
Assignees

Comments

@koshic
Copy link
Collaborator

koshic commented Oct 14, 2023

Due to default resolver (resolvewithplus) doesn't support PnP, users can use custom resolvers to work in PnP environment - https://github.com/iambumblehead/esmock/wiki#call-esmock-yarn-pnp. It's a small effort for one test, but what if existing project contains 1000+ tests?

We can implement PnP detection in esmock, and use our own PnP resolver instead of resolvewithplus (esmockArgs.js):

import resolvewithplus from 'resolvewithplus'

const resolver = process.versions.pnp === undefined ? resolvewithplus : (await import("./pnp-resolver.js")).default

arg[4] = { resolver, ...arg[4], ...optsextra}

It's an additional magic inside esmock, but I think we can have such magic for popular tools like Babel or Yarn.

@iambumblehead
Copy link
Owner

what do pnp-resolver.js sources look like?

@koshic
Copy link
Collaborator Author

koshic commented Oct 14, 2023

what do pnp-resolver.js sources look like?

https://github.com/iambumblehead/esmock/wiki#call-esmock-yarn-pnp - it's enough

@iambumblehead
Copy link
Owner

what do these sources look like? import pnpapi from "pnpapi"; I searched and found this https://www.npmjs.com/package/pnpapi?activeTab=code but there are no sources listed there

@koshic
Copy link
Collaborator Author

koshic commented Oct 14, 2023

I've updated esmock wiki recently, so resolver source is here:
image

Or did you mean something else? That code works fine in my projects (complicated TS + PnP projects).

@koshic
Copy link
Collaborator Author

koshic commented Oct 14, 2023

@iambumblehead
Copy link
Owner

Oh interesting, so esmock could support yarn pnp without adding any new dependencies...

If you make a PR that is good and if you don't I will make one over the next day or two. Imo a single unit test would be sufficient to start and a special ci job could be added for that.

@koshic
Copy link
Collaborator Author

koshic commented Oct 14, 2023

Yep, I'll start right now.

@iambumblehead
Copy link
Owner

iambumblehead commented Oct 14, 2023

Not sure if this would work. Returns user-defined resolver, then tries yarn resolver finally returns resolvewithplus

esmockReslover.js

import resolvewithplus from 'resolvewithplus'

export default async opts => opts.resolver
 || (await import('./pnp-resolver.js').catch(() => {})).default
 || resolvewithplus

@koshic
Copy link
Collaborator Author

koshic commented Oct 14, 2023

PnP detection can be done via process.versions.pnp, which is faster and cleaner than trying to import non-existen'pnpapi' module for classic setup.

What about order - yeah, we can skip 'getResolver' call if user-defined one already present in ops. Currently it not so important, but we can use dynamic load for resolvewithplus to avoid useless module load (and creating a lot of objects inside) via static import.

@koshic
Copy link
Collaborator Author

koshic commented Oct 14, 2023

#255

@iambumblehead
Copy link
Owner

okay its looking good at the PR 👍

@koshic koshic self-assigned this Oct 14, 2023
@koshic koshic linked a pull request Oct 14, 2023 that will close this issue
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 a pull request may close this issue.

2 participants