Ouch. I’d probably just do if ([%raw "require.main === module"]) { ... }
myself 
I think you can cut it down a bit by doing this instead though:
switch (require, _module) {
| (Some(require'), Some(_module')) =>
require'##main === Js.Undefined.return(_module')
| _ => Js.Exn.raiseError("Illegal state")
};
Or even
switch (require) {
| Some(require') =>
require'##main === Js.Undefined.fromOption(_module)
| _ => Js.Exn.raiseError("Illegal state")
};
Unless it’s important to fail if not all assumptions hold. I’d also probably just return false
instead of raising an error if require
is undefined
, since it still seems to hold that it is not the main
module in that case.
And using Js.Option.andThen
, this could be my final answer:
let isMain = (_module: option(Node.node_module)) =>
[%node require] |> Js.Option.andThen([@bs] r => Js.undefinedToOption(r##main)) === _module;
(I’m not saying this last one is a good example, btw, just proving to myself that it can be a one-liner :D)