├── .github └── workflows │ └── ci.yml ├── .gitignore ├── README.md ├── build.sbt ├── js └── src │ └── main │ └── scala │ └── example │ └── Main.scala ├── jvm └── src │ └── main │ └── scala │ └── example │ └── Main.scala ├── project ├── build.properties └── plugins.sbt └── shared └── src └── main └── scala └── example └── MyLibrary.scala /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | branches: 8 | - main 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | fail-fast: false 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: olafurpg/setup-scala@v10 17 | with: 18 | java-version: "adopt@1.8" 19 | - uses: coursier/cache-action@v5 20 | - name: Test JVM 21 | run: sbt fooJVM/run 22 | - name: Test JS 23 | run: sbt fooJS/run 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scala.js cross compile example 2 | 3 | This is an example on how to cross compile code to Scala.js and Scala JVM. 4 | 5 | To try it out, launch sbt and type: 6 | 7 | sbt> fooJS/run 8 | sbt> fooJVM/run 9 | 10 | More information can be found in the 11 | [Scala.js documentation](https://www.scala-js.org/doc/project/cross-build.html) and in the [sbt-crossproject](https://github.com/portable-scala/sbt-crossproject) plugin documentation. 12 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | ThisBuild / scalaVersion := "2.13.10" 2 | 3 | lazy val root = project.in(file(".")). 4 | aggregate(foo.js, foo.jvm). 5 | settings( 6 | publish := {}, 7 | publishLocal := {}, 8 | ) 9 | 10 | lazy val foo = crossProject(JSPlatform, JVMPlatform).in(file(".")). 11 | settings( 12 | name := "foo", 13 | version := "0.1-SNAPSHOT", 14 | ). 15 | jvmSettings( 16 | // Add JVM-specific settings here 17 | ). 18 | jsSettings( 19 | // Add JS-specific settings here 20 | scalaJSUseMainModuleInitializer := true, 21 | ) 22 | -------------------------------------------------------------------------------- /js/src/main/scala/example/Main.scala: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | object Main { 4 | def main(args: Array[String]): Unit = { 5 | val lib = new MyLibrary 6 | println(lib.sq(2)) 7 | 8 | println(s"Using Scala.js version ${System.getProperty("java.vm.version")}") 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /jvm/src/main/scala/example/Main.scala: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | object Main { 4 | def main(args: Array[String]): Unit = { 5 | val lib = new MyLibrary 6 | println(lib.sq(2)) 7 | 8 | println(s"Using a JVM version ${System.getProperty("java.vm.version")}") 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.8.2 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.0") 2 | addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0") 3 | -------------------------------------------------------------------------------- /shared/src/main/scala/example/MyLibrary.scala: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | class MyLibrary { 4 | def sq(x: Int): Int = x * x 5 | } 6 | --------------------------------------------------------------------------------