-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Yarn PnP compatibility? #10
Comments
Can you make a tiny example repo that showcases this, using a modern yarn? I am not a yarn (pnp) user myself. That would likely help me. |
@wooorm For sure - thank you for looking into this. Here's a minimal reproduction (and the README.md describes reproduction steps) https://github.com/bencmbrook/import-meta-resolve-pnp-repro |
Thanks. Running |
Hm, must be some environment differences - let me dig in on that. Can you confirm |
Yes, 3.3.0. Node 19 or 16.15, then running |
Managed to reproduce. It does say when running
That’s likely it. yarn doesn’t support ESM well. |
Hmm yes that could be it, but I don't think it's because the target package, |
I don’t particularly mean that remark itself is esm. More that this project loads files the way Node.js loads files, when in ESM. It can load CJS fine. But yarn PNP is doing magic things to hook into require calls, to not need ‘node_modules’, those hooks are different on the ESM side |
Ah, yeah I'm tracking. I just pushed up another test, using the native If you pull the changes, |
Digging deeper here... The source of the import-meta-resolve/lib/resolve.js Lines 1019 to 1022 in ef50015
Oddly, the reference implementation also does this? https://github.com/nodejs/node/blob/3c040348fe9c49687a796fbeea3fe7a9bc1251ed/lib/internal/modules/esm/resolve.js#L874-L875 This might be Yarn's magic? https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-pnp/sources/esm-loader/hooks/resolve.ts |
There may have landed new code to support ESM loaders in the upstream I am wondering if Yarn PnP can be solved in your actual use case though. Here’s what Yarn says about Yarn + ESM. My conclusion is different though: Don‘t use Yarn if it doesn’t support ESM/Node’s resolving algorithm. |
As I understand it (I backported recent changes just now), loaders are implemented above I don’t think there’s anything to do in this project. |
Yeah, I think I'm just out of luck with this combo. Thanks again for looking into this.
My use-case is specifically getting the remark VSCode extension working in a PnP repo, so I have little control over the Node flags. But I might be able to |
I'm gonna close this out. Thanks @wooorm ! |
It's the other way around - import {register} from 'module';
// export async function resolve() {
// return {shortCircuit: true, url: 'file:///lol'};
// }
register('data:application/javascript;charset=utf-8,export%20async%20function%20resolve%28%29%20%7B%0D%0A%20%20return%20%7BshortCircuit%3A%20true%2C%20url%3A%20%27file%3A%2F%2F%2Flol%27%7D%3B%0D%0A%7D');
console.log(import.meta.resolve('foo')); Prints
In other words, |
Then where is the code in Node that does that? I know that it’s still done by the function at
Where is the API in Node to find which loaders are currently turned on? I don’t think it exists. I don’t think this can be done. |
That's tricky indeed - Node.js recently changed the way hooks are registered, so you can't even rely on extracting the With that said, shouldn't the ponyfill delegate to the real |
It is, readme says no loaders & no env variables, no lots of things. There are tons of (experimental) env variable APIs that change how resolving happens. Those are interesting, but there are also cases where you (as a package author) don’t want the user’s configuration to change what you resolve. That’s my primary use case. And I believe most people who currently use this’s use case too. This package also allows to pass conditions for example (and doesn’t inherit the users conditions). To illustrate, it could be used to find a browser version first, and fall back otherwise. Such use cases would not work if the users conditions are used. So if loaders were to be supported, they should be configurable. Inferring the current loaders/conditions etc could be added too.
Then there would be significant differences between when the actual |
I believe the issue described here (remarkjs/remark-language-server#6 (comment)) is a result of an assumption that packages are stored in a
node_modules
directory.I tried running
resolve
against my project and got the following errorwhere
test.mjs
isremark
is installed.yarn/cache/...
rather thannode_modules
A more thorough example:
test2.mjs
Yields:
The text was updated successfully, but these errors were encountered: