This is a barebone example of an application written in Scala.js.
To get started, first make the Scala.js compiler, library and sbt plugin available locally by publishing them locally. This is very easy, just follow the instructions in the Scala.js readme.
Then, copy the Scala.js runtime into the js/
subdirectory of this project.
You will find it in the subdirectory target/
of Scala.js, it is named
scalajs-runtime.js
. Optionally, also copy the source map
scalajs-runtime.js.map
.
Now, you are good to go. Open sbt
in this example project, and issue the
task package-js
. This creates the file target/example.js
. You can now
open index-dev.html
in your favorite Web browser!
During development, it is useful to use ~package-js
in sbt, so that each
time you save a source file, a compilation of the project is triggered.
Hence only a refresh of your Web page is needed to see the effects of your
changes.
Note that Scala.js and sbt do not play well enough together for incremental
compilation to work. So package-js
will recompile all your source files
everytime.
Instead of running package-js
, you can also run optimize-js
to generate
a much more compact version of the JavaScript code. While index-dev.html
refers to the JavaScript emitted by package-js
, index.html
refers to the
optimized JavaScript emitted by optimize-js
.
The optimization phase is performed by the Advanced Optimizations of the Google Closure Compiler, which make strong assumptions about the code being compiled.
All the code generated by Scala.js respects these assumptions. But if you
modify startup.js
, make sure that you comply with them if you want to be
able to use optimize-js
.
In a bigger application, startup.js
or an equivalent file will typically
export selected APIs from the code generated by Scala.js. The exported
symbols can then be used from other JavaScript code that is not being
processed by Closure.
You have probably forgotten to execute package-js
from the sbt prompt in
Scala.js.
You have probably forgotten to execute publish-local
from the sbt prompt in
Scala.js.