module: add module.entrypoint - #64903
Conversation
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #64903 +/- ##
==========================================
+ Coverage 90.13% 90.30% +0.17%
==========================================
Files 746 759 +13
Lines 242893 247629 +4736
Branches 45769 46683 +914
==========================================
+ Hits 218935 223631 +4696
- Misses 15444 15459 +15
- Partials 8514 8539 +25
🚀 New features to boost your workflow:
|
|
@mcollina lint? |
Add `module.entrypoint`, a property exposing the resolved URL of the entry point of the current thread. Unlike `require.main`, it works regardless of whether the entry point is a CommonJS or an ECMAScript module, and inside worker threads it reflects the entry point of the worker itself, matching the semantics of `require.main` and `import.meta.main`. Refs: nodejs#51840 Refs: nodejs#64800 Signed-off-by: Matteo Collina <hello@matteocollina.com>
b6075b5 to
d5b61b2
Compare
| function fixtureURL(...args) { | ||
| // The entrypoint is fully resolved, so account for symlinks in the path | ||
| // to the fixtures directory. | ||
| return pathToFileURL(realpathSync(fixtures.path('module-entrypoint', ...args))).href; | ||
| } |
There was a problem hiding this comment.
Why not use fixtures.fileURL?
| import { entrypoint } from 'node:module'; | ||
|
|
||
| if (import.meta.url === entrypoint) { | ||
| console.log('This module is the entry point of the current thread'); | ||
| } |
There was a problem hiding this comment.
That's not a great example, import.meta.main would be a much better choice here – and if we can't find a good example, do we actually need that API?
There was a problem hiding this comment.
because import.meta.main only returns true if it's the current one, but don't report us what's the actual "entrypoint" of the current thread/module chain. So it's not easily discoverable.
The same can be said for #64800.
If you want I can remove the example.
There was a problem hiding this comment.
Yeah I think the example either needs to include import.meta.main, and/or process.mainModule (to explain the diffs and show some actual use-cases) otherwise it looks like we're adding a less elegant way to do something that's already possible
Address review feedback from aduh95. Signed-off-by: Matteo Collina <hello@matteocollina.com>
Adds
module.entrypointtonode:module: the resolved URL string of the entry point of the current thread.The semantics follow
require.mainandimport.meta.main(thread-scoped): inside a worker thread it is the URL of the script the worker was started with, not the entry point of the process. This complements #64800, which exposes the process-wide entry point inherited by workers.undefinedfor--eval, the REPL, STDIN input, andeval: trueworkers; set to thedata:URL for data URL workers.--preserve-symlinks-main), so it matchesrequire.main.filename/ the entry point'simport.meta.urlrather thanprocess.argv[1].Refs: #51840
Refs: #64800