├── .gitignore ├── .travis.yml ├── README.md ├── build.sbt ├── index-fastopt.html ├── index.html ├── project ├── build.properties └── build.sbt └── src ├── main └── scala │ └── example │ └── ScalaJSExample.scala └── test └── scala └── example └── ScalaJSExampleTest.scala /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .cache 3 | .classpath 4 | .project 5 | .settings/ 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: scala 2 | script: 3 | - sbt ++$TRAVIS_SCALA_VERSION test 'set scalaJSStage in Global := FullOptStage' test 4 | scala: 5 | - 2.10.6 6 | - 2.11.8 7 | jdk: 8 | - oraclejdk8 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Barebone application written in Scala.js -- not maintained 2 | 3 | **This repository is not maintained anymore. 4 | Consider the [scala-js-tutorial](https://github.com/scala-js/scalajs-tutorial) repository instead, along with [the actual tutorial](https://www.scala-js.org/tutorial/basic/).** 5 | 6 | This is a barebone example of an application written in 7 | [Scala.js](http://www.scala-js.org/). 8 | 9 | ## Get started 10 | 11 | To get started, open `sbt` in this example project, and execute the task 12 | `fastOptJS`. This creates the file `target/scala-2.12/example-fastopt.js`. 13 | You can now open `index-fastopt.html` in your favorite Web browser! 14 | 15 | During development, it is useful to use `~fastOptJS` in sbt, so that each 16 | time you save a source file, a compilation of the project is triggered. 17 | Hence only a refresh of your Web page is needed to see the effects of your 18 | changes. 19 | 20 | ## Run the tests 21 | 22 | To run the test suite, execute the task `test`. If you have installed 23 | [Node.js](http://nodejs.org/), you can use that runtime to run the tests, 24 | which is faster: 25 | 26 | > set scalaJSStage in Global := FastOptStage 27 | > test 28 | 29 | ## The fully optimized version 30 | 31 | For ultimate code size reduction, use `fullOptJS`. This will take several 32 | seconds to execute, so typically you only use this for the final, production 33 | version of your application. While `index-fastopt.html` refers to the 34 | JavaScript emitted by `fastOptJS`, `index.html` refers to the optimized 35 | JavaScript emitted by `fullOptJS`. 36 | 37 | If Node.js is installed, the tests can also be run in their fully optimized 38 | version with: 39 | 40 | > set scalaJSStage in Global := FullOptStage 41 | > test 42 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | // Turn this project into a Scala.js project by importing these settings 2 | enablePlugins(ScalaJSPlugin) 3 | 4 | name := "Example" 5 | 6 | version := "0.1-SNAPSHOT" 7 | 8 | scalaVersion := "2.12.1" 9 | 10 | scalaJSUseMainModuleInitializer := true 11 | 12 | testFrameworks += new TestFramework("utest.runner.Framework") 13 | 14 | libraryDependencies ++= Seq( 15 | "org.scala-js" %%% "scalajs-dom" % "0.9.1", 16 | "com.lihaoyi" %%% "utest" % "0.4.5" % "test" 17 | ) 18 | -------------------------------------------------------------------------------- /index-fastopt.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Example Scala.js application 5 | 6 | 7 | 8 | 9 |

Example Scala.js application - fast-optimized version

10 | 11 |

After having compiled and fast-optimized properly the code for the application 12 | (using `sbt fastOptJS`), you should see "It works" below. 13 | See README.md for detailed explanations.

14 | 15 |
16 |
17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Example Scala.js application 5 | 6 | 7 | 8 | 9 |

Example Scala.js application - full-optimized version

10 | 11 |

After having compiled and full-optimized properly the code for the application 12 | (using `sbt fullOptJS`), you should see "It works" below. 13 | See README.md for detailed explanations.

14 | 15 |
16 |
17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.15 2 | -------------------------------------------------------------------------------- /project/build.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.15") 2 | -------------------------------------------------------------------------------- /src/main/scala/example/ScalaJSExample.scala: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | import scala.scalajs.js 4 | import js.annotation.JSExport 5 | import org.scalajs.dom 6 | 7 | @js.native 8 | trait EventName extends js.Object { 9 | type EventType <: dom.Event 10 | } 11 | 12 | object EventName { 13 | def apply[T <: dom.Event](name: String): EventName { type EventType = T } = 14 | name.asInstanceOf[EventName { type EventType = T }] 15 | 16 | val onmousedown = apply[dom.MouseEvent]("onmousedown") 17 | } 18 | 19 | @js.native 20 | trait ElementExt extends js.Object { 21 | def addEventListener(name: EventName)( 22 | f: js.Function1[name.EventType, _]): Unit 23 | } 24 | 25 | object ScalaJSExample extends js.JSApp { 26 | def main(): Unit = { 27 | val paragraph = dom.document.createElement("p") 28 | paragraph.innerHTML = "It works!" 29 | dom.document.getElementById("playground").appendChild(paragraph) 30 | 31 | val p = paragraph.asInstanceOf[ElementExt] 32 | } 33 | 34 | /** Computes the square of an integer. 35 | * This demonstrates unit testing. 36 | */ 37 | def square(x: Int): Int = x*x 38 | } 39 | -------------------------------------------------------------------------------- /src/test/scala/example/ScalaJSExampleTest.scala: -------------------------------------------------------------------------------- 1 | package example 2 | 3 | import utest._ 4 | 5 | object ScalaJSExampleTest extends TestSuite { 6 | 7 | import ScalaJSExample._ 8 | 9 | def tests = TestSuite { 10 | 'ScalaJSExample { 11 | assert(square(0) == 0) 12 | assert(square(4) == 16) 13 | assert(square(-5) == 25) 14 | } 15 | } 16 | } 17 | --------------------------------------------------------------------------------