esm: implement import.meta.main
Boolean value to check if a module is run as entry point of the current process.
ES module authors need a way to determine if their code is run as CLI or as a dependency. nodejs/node#49440 have some proposal, I am implementing import.meta.main
because:
- it is arguably similar to the CJS pattern:
- if (module === require.main) {
+ if (import.meta.main) {
- Deno has already implemented it, if we don't differ that is certainly easier for users to write code that runs everywhere.
- It was suggested originally in the
import.meta
proposal.
Fixes: https://github.com/nodejs/node/issues/49440
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
documentation is changed or added -
commit message follows commit guidelines