Search code examples
scala.jsscalatags

Is there a way to run pure scala code in a script tag


Using the scalatags script tag I know I can define the following:

script("console.log('Running javascript code')")

But is there a way I can rather pass pure scala code instead? So something like the following:

script(println("Running scala code now"))

Solution

  • No, that is not possible.

    You can achieve the same effect in a different way, by exporting a top-level function with the Scala code you want to run, then generate a script that calls that function:

    object Exports {
      @JSExportTopLevel("dynamicScriptCode")
      def code(): Unit =
        println("Running Scala code now")
    }
    
    ...
    
    script("dynamicScriptCode();")