update CoffeeScript loader for incoming hook consolidation#2
update CoffeeScript loader for incoming hook consolidation#2GeoffreyBooth merged 14 commits intomasterfrom
Conversation
|
@GeoffreyBooth I think the trick of appending |
* importing shout.coffee (cjs) is an empty object
| fileURLToPath(new URL('./loader.js', import.meta.url).href), | ||
| fileURLToPath(new URL('./fixtures/esm-and-commonjs-imports.coffee', import.meta.url).href), |
There was a problem hiding this comment.
Why are these changes necessary?
There was a problem hiding this comment.
I don’t understand; are you saying that these changes were accidental?
There was a problem hiding this comment.
Oh, sorry you asked "why". This is a bit of an edge-case, but when the cwd is not the node-loaders project root, this fails.
For instance, in my local:
~/Workspace/
…/
nodejs/
node-loaders/
I cd into the nodejs dir, which has my pr-compiled node executable, and run:
~/Workspace/…/nodejs $> ./node \
--experimental-loader \
../node-loaders/coffeescript-loader/loader.js \
../node-loaders/coffeescript-loader/test.jsWithout that extra bit of code, the files are not found.
There was a problem hiding this comment.
Okay, that’s fine then. I think the most likely real-world case would be where the loader is in node_modules for the current project. Like say I add loader.js to CoffeeScript, then you’d run something like:
node --experimental-loader ./node_modules/coffeescript/loader.js app.coffeeSo whatever we put here should work for this case as well.
There was a problem hiding this comment.
Mm, I think the rule for this would be:
- refs to things internal to the loader module would need to be relative to the loader module (eg what you see above with
import.meta.url) - refs to things external to the loader module would need to be relative to the caller (
process.cwd()).
There was a problem hiding this comment.
This breaks the tests when running on Windows with GitBash.
There was a problem hiding this comment.
Since I wrote that code, I’ve learned that the arguments here should be URLs, not paths. That might explain why they were broken in Windows.
| ok(stdout.includes('HELLO FROM ESM'), 'ESM import transpiles'); | ||
| ok(stdout.includes('Hello from CommonJS!'), 'CommonJS import transpiles'); | ||
|
|
||
| // this relies on a named export from CommonJS, which is not supposed to work |
There was a problem hiding this comment.
So is this a TODO? Could we spell out more clearly what is supposed to happen, and mention that this is a bug/unimplemented feature?
There was a problem hiding this comment.
I'm honestly not sure what is supposed to happen—I expect this to work now and before. But it doesn't work then or now. I'm afraid to open a can of worms investigating this, but if it's supposed to work, I (biting my tongue so hard) think it should maybe block nodejs/node#37468 (depending on how pervasive the usage of this is).
What currently happens is the require.extension works, the transformed source disappears into the CJS loader (I confirmed it does get passed it), and then an empty object comes out the other side.
There was a problem hiding this comment.
There’s no need to block a PR because it doesn’t fix an existing bug, especially not for an experimental feature. I think all we need to do at this point is add a comment here explaining something like “known issue: blah blah” with the clearest explanation you can write, and we’ll come back to it later. I just don’t want to forget about this or have this code in here and someone like you comes along and thinks that it should be working, or that it did work in some older version of Node but got broken somehow more recently.
… round * handle package.json files without "type" field
|
Okay, so I did some debugging, and I think we have some kind of red-herring here: the cjs deps from coffeescript files do not hit ESMLoader at all. I added logging to To be doubly sure I wasn't missing anything, I added copious logging to module translators' commonjs strategy, and there is nothing whatsoever for files imported by So I think we can confidently say whatever is happening is not ESMLoader's fault. |
That’s fine, but we still need some way for users to write a loader that affects both ESM and CommonJS files. Like what I was trying to do with the CoffeeScript loader defining To take another example, say you wanted to create a YAML loader. As a user of such a loader, I would expect that both |
|
From the debugging I did, I did not see any way to get it to work as things currently are, but it may have just needed more investigation and/or more knowledge of the CJSLoader. Regarding (future) updates to the CJSLoader, that sounds likely needed, but I remember someone (Bradley?) saying 2–3 team meetings ago that something like this would require changes to the CJSLoader, but previous efforts failed in a fiery blaze because there was too much disagreement. I would certainly like it to be fixed, but if that's true, maybe we shouldn't promise the issue will be addressed? |
I think a comment can make whatever promises it wants. No one will hold that comment responsible 😄 To the broader point, though, I think we should tap Bradley (and Guy, if he has the time) to get their wisdom about what the issue is with supporting Anyway I think let’s add a comment and move on for now and get this PR landed, and after that maybe we can create an issue on nodejs/loaders spelling out the problem as detailed as we can, and one of the others can hopefully tell us where to look in the code for what needs to be changed to fix it. |
|
Sounds good to me :) I added such a comment to the CoffeeScript loader test file (lines 30–35). Does that suffice, or were you thinking to add the note elsewhere more conspicuous (like the ESM docs)? |
|
Ah, okay, so I got the download from Bradley. The gist of it is what we thought: this part of the two APIs are not integrated. It is possible for the user to facilitate the integration by returning @bmeck does that accurately represent what would need to be done from a high level? |
I think wherever the docs currently discuss |
|
@GeoffreyBooth to be clear it works as expected by my view XD. CJS hooks don't cleanly integrate against stuff. |
No description provided.