Skip to content

Private package folders with Yarn PnP #6945

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

Closed
cpojer opened this issue Jan 21, 2019 · 5 comments
Closed

Private package folders with Yarn PnP #6945

cpojer opened this issue Jan 21, 2019 · 5 comments
Assignees
Labels

Comments

@cpojer
Copy link
Contributor

cpojer commented Jan 21, 2019

Currently, in the JavaScript ecosystem, any file from a package can be required even if it is considered as private API by the package author, for example:

require('jest-haste-map/build/HasteFS'); // Works!

There is no way to hide private API besides compiling a project into a single flat bundle and only exposing the public API. Very few people choose to do this, and it may not be ideal in terms of load times – it may make sense for some packages to have two or three top level modules instead of one.

With Yarn PnP controlling the resolution algorithm it is now possible to prevent access to certain files and folders from other locations. There are two ways to pick a private folder:

  • Either we standardize on a single folder name like private or similar, or
  • we allow package authors to make files private by listing private folders (or public ones if we'd prefer to use a whitelist) of a package in package.json.

This would have the effect that only a limited subset of a package is available to the outside, while the package code can still be distributed in a manageable way.

Let me know what you think about this – feel free to create an RFC based on this idea or even better – send a Pull Request with a proposed implementation :)

@arcanis
Copy link
Member

arcanis commented Jan 21, 2019

I think the best would be for the opposite: whitelisting instead of blacklisting.

While PnP did a lot to prevent useless I/O calls, some still are needed simply because of the extension resolution (lodash/foo -> lodash/foo.js) and folder resolution (lodash/fp -> lodash/fp/index.js). We could avoid it if we were keeping the full list of package files within the PnP file, but since it's a lot of data mostly useless I would prefer to avoid it.

But if we assume that packages explicitly describe their entry points, then the resolution can be done without any I/O - the algorithm would just have to check whether the resolution endpoints is meant to exist!

{
  "entrypoints": [
    "./fp/index.js",
    "./foo.js",
  ]
}

Or, if you don't want to allow access to anything else than the index (like in your case):

{
  "entrypoints": [
    "./index.js",
  ]
}

@cpojer
Copy link
Contributor Author

cpojer commented Jan 21, 2019

Yep, as stated in the original comment, a whitelist is fine with me too. Either way, the same result is accomplished so I'm totally happy with this :)

@suchipi
Copy link

suchipi commented Jan 21, 2019

This would be great, common pain point 👍

@ghost ghost assigned arcanis Jan 22, 2019
@ghost ghost added the triaged label Jan 22, 2019
@namuol
Copy link

namuol commented Feb 17, 2019

I have written code to manually detect and prevent this; it would be great to have baked-in support.

Regarding whitelist vs blacklist: I'm probably on the extreme side of this, but I would prefer that we disallow importing anything other than main by default.

@merceyz
Copy link
Member

merceyz commented Jan 2, 2021

Closing as this is solved by using the exports field now supported by node https://nodejs.org/api/packages.html#packages_exports

@merceyz merceyz closed this as completed Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants