How to require deep module content (commonjs)?



Hi all,

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

[] [@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?


Yes. There is bs.scope:

[][@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";



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.