How to require deep module content (commonjs)?

interop

#1

Hi all,

I am trying to create external object with the following reason code:

[@bs.new] [@bs.module "winston.transports"] external newConsole: config => t = "Console";

when this is transpiled to js, I get:

var WinstonTransports = require("winston.transports");

which does not work at all, such thing can’t be required. The code I need on js should be like:

var Transports = require("winston").transports;

Is it possible to use external stuff from deeper inside external modules?


#2

Yes. There is bs.scope:

[@bs.new][@bs.module "winston"][@bs.scope "transports"] external newConsole: config => t = "Console";

You can also use parentheses to scope deeper, like

[@bs.val] [@bs.scope ("window", "location", "ancestorOrigins")] external length : int = "length";

See: https://bucklescript.github.io/docs/en/bind-to-global-values#global-modules


#3

Thanks fham! I somehow failed to miss the bs.scope instruction even though it is clearly documented. Search fatigue perhaps on my part, an index of all bs instructions in one place would be nice. At least I imagine that it would help in recognizing which instructions one is not yet familiar with. Thanks again.