Using @bs.send with @bs.new


#1

This is the JS output that I wanted to create.

const fabric = require("fabric").fabric;

let canvas = new fabric.Canvas("c");

however I can’t figure out the proper way to do the bindings.

module Canvas = {
  type t

  @bs.module("fabric") external fabric: fabric = "fabric"

  @bs.new @bs.send external makeCanvas: (fabric, string) => t = "Canvas"
}

This doesn’t work because I can’t use @bs.new with @bs.send

I could use @bs.scope("fabric") but I don’t think that is the right way to do it because looking at output, fabric is the result of require.

  @bs.new @bs.scope("fabric") external makeCanvas: string => t = "Canvas"

fabric will be undefined here.


#2

In this case, you can try chaining @scope and @module with @new:

module Canvas = {
  type t

  @scope("fabric") @module("fabric") @new  external make: string => t = "Canvas"
}

let fabric = Canvas.make("c")

it yields the following output:

var Fabric = require("fabric");

var fabric = new (Fabric.fabric.Canvas)("c");

Note, that you do not need the bs. prefix since ReScript 8.3.
Also note that ReScript-specific questions should be asked in the ReScript forum instead.


#3

Thanks. That works. I will post in ReScript next time, I didn’t know there is rescript specific forum.