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

resolve #58

Closed
AbhiPrasad opened this issue Jun 29, 2024 · 5 comments
Closed

resolve #58

AbhiPrasad opened this issue Jun 29, 2024 · 5 comments

Comments

@AbhiPrasad
Copy link
Contributor

AbhiPrasad commented Jun 29, 2024

We can replace resolve with built-ins starting Node 8.9.0 and above.

It pulls in a bunch of deps, and doesn’t support exports field and custom conditions (which is very common in modern JS apps).

image

For CJS you can use require.resolve. ESM is more tricky, you prob need https://www.npmjs.com/package/resolve.exports, but there is import.meta.resolve that was introduced with Node 20.6.0

Here's an example PR I opened up with rollup that does the ESM thing: rollup/plugins#1742 (I may have to change this to make it work with ESM, need more testing)

Here's an example PR (that tbh should be merged in to require-in-the-middle) that does the CJS port: elastic/require-in-the-middle#25

@AbhiPrasad
Copy link
Contributor Author

From @antfu on discord:

Resolve is a very complex problem to solve. require.resolve only resolve CJS conditions. For ESM and custom conditions, you might want https://www.npmjs.com/package/resolve.exports, but that still, wouldn't even be closer to a 1:1 replacement of resolve.

@benmccann
Copy link
Contributor

I don't think we can just suggest this one to be replaced. Sometimes you can, but very often projects are using APIs from resolve that are not available in require.resolve

@ljharb
Copy link

ljharb commented Jun 29, 2024

fwiw you can replace supports-preserve-symlinks-flag with export default typeof window === 'undefined' ? true : null; if it's node 6.2+ (and if you have separate node and browser files, one is just true and the other just null).

I think is-core-module you're already replacing, and path-parse can be replaced with export { parse } from 'path' in node >= 0.12.

Sadly import.meta.resolve is both flagged and broken in all sorts of ways, so it's likely not yet a viable option.

@AbhiPrasad
Copy link
Contributor Author

Sadly import.meta.resolve is both flagged and broken in all sorts of ways, so it's likely not yet a viable option.

Yes, unfortunately it's nowhere near robust enough (also one of the many reasons why I believe ESM is not prime-time yet, but that's another debate).

We probably need to push for a proposal like nodejs/node#51855, which would also help with many of the hacks that https://github.com/nodejs/import-in-the-middle and https://github.com/vercel/nft/ have to do.

thanks for the advice @ljharb!

I don't think we can just suggest this one to be replaced. Sometimes you can, but very often projects are using APIs from resolve that are not available in require.resolve

This feels reasonable, I'll close this then! And see if I can get anyone excited about helping expose some stuff in Node.js directly 😄.

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

No branches or pull requests

3 participants