1

I have a cross build project with the following structure:

<project root>
 +- jvm
 |   +- src/main/scala/main.Scala
 +- js
 |   +- src/main/scala/main.Scala
 +- shared
     +- src/main/scala/myLibrary.scala
     +- src/main/resources/myjsfile.js

Now in my js main.Scala file, I want to read in myjsfile.js.

I know in a standard Scala JVM project I can do the following:

  val is = getClass.getClassLoader.getResourceAsStream("myjsfile.js")
  val str = scala.io.Source.fromInputStream(is).mkString

Now my question is, how can I do this with my current folder structure?

If I run the following main.scala (js version):

package example

object Main extends App {
  
  val is = getClass.getClassLoader.getResourceAsStream("")
  val str = scala.io.Source.fromInputStream(is).mkString

  println(str)
}

I get the following error:

org.scalajs.jsenv.ExternalJSRun$ClosedException: Termination was requested by user
[error]     at org.scalajs.jsenv.ExternalJSRun$$anon$1.run(ExternalJSRun.scala:184)
[error] stack trace is suppressed; run last fooJS / Compile / run for the full output
[error] (fooJS / Compile / run) org.scalajs.jsenv.ExternalJSRun$ClosedException: Termination was requested by user
[error] Total time: 3 s, completed 25 Jun 2020, 10:56:05
[IJ]sbt:root> 

How can I overcome this error?

finite_diffidence
  • 893
  • 2
  • 11
  • 20
  • Out of curiosity, why does `myjsfile.js` need to be in the `shared` project? Does the `jvm` project need it? – randbw Jun 25 '20 at 10:55
  • Maybe I am doing it wrong: So I am creating a package for the JVM. In the shared folder I have all the main jvm code, with class definitions. I want Scalajs to use these class definitions and run some tests. Scalajs will essentially just test the JSON output of some functions I wrote which reside in the shared folder. I use scalajs for tests as it has a facade for a js library that has all the functionality I need. – finite_diffidence Jun 25 '20 at 11:10
  • from what you've said, I think `myjsfile.js` should be located in the `js` project's resource folder – randbw Jun 25 '20 at 13:18

0 Answers0