@import BookData._ @p We've by this point done a bunch of work in the browser: we've made a small game that runs in the web browser on the HTML5 canvas, and we've made a number of small web-apps that interact with HTML and 3rd party web-services. However, there's a whole other side to the Scala.js ecosystem: the command line interace, or CLI. @p Even though the goal of Scala.js is to get your code running in peoples' browsers, it still is useful to be familiar with the things that you can do in the command-line. It is much easier to write-code-print-results in the command line without having to set up a HTML page and opening a browser to test it, and this extends to things like unit test suites, which are almost exclusively run in the command-line using @code{sbt ~test} to keep re-running them when the code changes. @p The Scala.js command line is where you go to do things to your Scala.js code. Although Scala.js comes with @lnk("standalone executables", "http://www.scala-js.org/downloads.html") that can be used from any shell (sh, bash, zsh, etc.) the primary supported way of setting up, compiling and testing your Scala.js applications is through @lnk("SBT", "http://www.scala-sbt.org/"): Scala's primary build tool. @p You've already used fragments of the Scala.js CLI in earlier chapters of this book: @code{~fastOptJS} is what you used for development, @code{fullOptJS} for publishing. Apart from these, Scala.js allows you to execute code via @lnk.misc.Rhino/@lnk.misc.Nodejs/@lnk.misc.PhantomJS from the command-line, as well as running test-suites under the same conditions. This chapter will go deeper into the various things you can do with the Scala.js command line: @ul @li @code{compile}: converting code from Scala source into not-yet-executable Scala.js IR @li @code{package}: bundling up our Scala.js IR into a @code{.jar} file, for publishing or distribution as a library @li @code{fastOptJS}: aggregating our Scala.js IR and converting it to a @code{.js} executable. @li @code{fullOptJS}: aggregating our Scala.js IR and converting it to a smaller, faster @code{.js} executable. @li @code{run}: run your compiled Scala.js code as Javascript in Rhino, Node.js or Phantom.js @li @code{test}: run your compiled Scala.js code as a test suite in Rhino, Node.js or Phantom.js @p Now, let's open up your SBT console in your Scala.js app @hl.bash > @p And let's get started! @sect{Commands} @p The most fundamental thing you can do in the Scala.js CLI is to compile your code. Let's go through the various mechanisms of "compiling" things: @sect{The compile Command} @hl.bash > compile @p Just as you can @code{compile} Scala-JVM projects, you can also @code{compile} Scala.js projects. Like compiling Scala-JVM projects, this leaves a bunch of @code{.class} files in your @code{target} directory. Unlike Scala-JVM projects, this also leaves a bunch of @code{.sjsir} files, which correspond to your Scala.js output files: @hl.bash classes └── example ├── Point$.class ├── Point$.sjsir ├── Point.class ├── Point.sjsir ├── ScalaJSExample$$anonfun$main$1.class ├── ScalaJSExample$$anonfun$run$1.class ├── ScalaJSExample$.class ├── ScalaJSExample$.sjsir └── ScalaJSExample.class @p However, unlike on Scala-JVM, you cannot directly run the @code{.sjsir} files spat out by the Scala.js compiler. These files are an Intermediate Representation, which needs to go through the next step in the compilation pipeline before being turned into Javascript. @sect{The package Command} @hl.bash > package @p Also like on Scala-JVM, Scala.js also supports the @code{package} command. This command generates a @code{.jar} like it does in Scala-JVM, except this version appends this weird @code{_sjs0.6} suffix. @hl.bash target/scala-2.11/ └── example_sjs0.6_2.11-0.1-SNAPSHOT.jar @p The purpose of this suffix is to link the compiled @code{.jar} file to the version of Scala.js used to compile it. This allows you to make sure that you don't accidentally depend on a version of a jar that is incompatible with your current version. @p Again, unlike Scala-JVM, these @code{.jar} files are not directly executable: the @code{.sjsir} files need further processing to turn into runnable Javascript. Instead, their sole purpose is to hold bundles of @code{.sjsir} files to be published and depended-upon: they can be @code{publishLocal}ed to be used by other projects on your computer, or @code{publishSigned}ed to @lnk("Maven Central", "http://search.maven.org/"), just like any Scala-JVM project. @sect{The fastOptJS Command} @hl.bash > fastOptJS @p @code{fastOptJS} is a command we've used in earlier chapters. It basically runs the @sect.ref{Fast Optimization} stage of the compilation pipeline. This results in a moderately-sized executable, which you can then load in the browser with a @hl.html{