-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[Feature] Support custom export resolution strategy #2815
Comments
I'll look in more details later, but note that Now to answer the next two questions that you might ask:
|
@arcanis Following your advice, I reimplemented the pnp resolution plugin for esbuild using The main change is to use Then I forked the Therefore, if we want to build the support for browser-like resolution in pnpapi core, I think we should add an optional field like |
Thanks for your work @SamChou19815! What do you think would it take to get your changes merged into @yarnpkg/esbuild-plugin-pnp? |
Thanks for reminding me! Yes, it definitely makes sense to fix that in the plugin, at least until ESBuild provides us way to call into its default resolver to leverage its logic (which is for example how it works in Webpack - the PnP code just handles the bare import resolution, and the native Webpack resolver then continue on its own). We wouldn't duplicate the code though, so I think the ideal way would be to add a |
Conditions would be a great start, but wouldn't account for edge-cases where older or not-yet-updated versions of packages have a "browser" field but no "exports" field. (e.g. [email protected], which is still depended on by certain packages) |
Describe the user story
Recently yarn landed the support for exports in package.json. Currently it hardcodes the resolution to support cjs only:
berry/packages/yarnpkg-pnp/sources/loader/makeApi.ts
Lines 233 to 239 in 30e1d3c
I understand that ESM support is not ready yet (#638). However, the ability to resolve to esm module is still important, since Yarn pnp will also be used to resolve files, and the execution might be handled by something else (e.g. browser). For example, esbuild has a platform flag that can be set to
browser
, and it is currently not respected by Yarn esbuild pnp plugin. Yarn will always use themain
field, and in the presence ofexports
, use therequire
one, so I have to fork it and do extra work to make it support browser field:https://github.com/SamChou19815/website/blob/ca0f70ff61778107a6cdc456d65b3ba712bdb57b/packages/esbuild-scripts/esbuild/esbuild-pnp-plugin.ts#L41-L53
It would be nice if pnpapi could expose an additional flag to customize the behavior.
Describe the solution you'd like
In the opts argument of
resolveRequest
and , we can add another optional flag calledplatform
that defaults tonode
.If the field
platform
isnode
, then we keep the old behavior. If the fieldplatform
isbrowser
, then we try to look into main fields in this order:['browser', 'module', 'main']
withoutexports
and make the argument passed toresolve.exports
to be{ browser: true, require: false }
.Describe the drawbacks of your solution
It will increase the complexity of the pnpapi, and the
browser
field, although recognized by resolve.exports, is not part of the node standard.Describe alternatives you've considered
Add extra postprocessing step after pnpapi resolution, like the hacky one I used in https://github.com/SamChou19815/website/blob/ca0f70ff61778107a6cdc456d65b3ba712bdb57b/packages/esbuild-scripts/esbuild/esbuild-pnp-plugin.ts#L41-L53.
The text was updated successfully, but these errors were encountered: