Rescript-compiler: An option to use any js command without doing %raw

Created on 13 Oct 2018  路  8Comments  路  Source: rescript-lang/rescript-compiler

When I'm writing OCaml/ReasonML code, I always hate to write Js. to interop with JS. In Clojurescript, I can do (console/log "Hello, world!") (for example) to use console.log function in JS. Even (asdasd/sdgsdgf "sdjhsd" 5) turns into asdasd.sdgsdgf("sdjhsd",(5)); Why can't we make it work like in Clojurescript? For example

let rec fact n =
  if n = 1 then 1 else fact(n - 1)
;;

console.log(fac 5)

Most helpful comment

You could add a binding:

[@bs.val] external require: string => unit = "require";
require("something.js");

All 8 comments

You can always just open Js at the top of the file?

For example when i want to use require

open Js
let x = require("something.js")

This gives an error because there is no function named require.
I want to directly use JS functions. Also, JS just implements few things.

I realized I can use [%raw ""]. Thanks.

So, is there a way to do it without %raw directly?

You could add a binding:

[@bs.val] external require: string => unit = "require";
require("something.js");

When I add a binding, I should add a binding for each command in js and this is not productive.

@midnio I am not sure I get your point, can you be more specific

@bobzhang

console.log "Hello!"
console.log(Math.pow 7 3)

Just make this work.

Was this page helpful?
0 / 5 - 0 ratings