summaryrefslogtreecommitdiff
path: root/book
diff options
context:
space:
mode:
Diffstat (limited to 'book')
-rwxr-xr-xbook/src/main/resources/css/side-menu.css338
-rw-r--r--book/src/main/resources/favicon.pngbin0 -> 988 bytes
-rw-r--r--book/src/main/resources/favicon.svg44
-rw-r--r--book/src/main/resources/images/Dropdown.pngbin0 -> 331377 bytes
-rw-r--r--book/src/main/resources/images/Hello World Console.pngbin0 -> 345956 bytes
-rw-r--r--book/src/main/resources/images/Hello World White.pngbin0 -> 245770 bytes
-rw-r--r--book/src/main/resources/images/Hello World.pngbin0 -> 265509 bytes
-rw-r--r--book/src/main/resources/images/IntelliJ Hello.pngbin0 -> 338093 bytes
-rw-r--r--book/src/main/resources/images/Scalatags Downloads.pngbin0 -> 89779 bytes
-rw-r--r--book/src/main/resources/images/javascript-the-good-parts-the-definitive-guide.jpgbin0 -> 53631 bytes
-rw-r--r--book/src/main/scala/book/Book.scala90
-rw-r--r--book/src/main/scala/book/BookData.scala45
-rw-r--r--book/src/main/scala/book/Main.scala66
-rw-r--r--book/src/main/scala/book/Utils.scala213
-rw-r--r--book/src/main/scalatex/book/Index.scalatex83
-rw-r--r--book/src/main/scalatex/book/Intro.scalatex182
-rw-r--r--book/src/main/scalatex/book/handson/CanvasApp.scalatex210
-rw-r--r--book/src/main/scalatex/book/handson/ClientServer.scalatex244
-rw-r--r--book/src/main/scalatex/book/handson/CommandLine.scalatex173
-rw-r--r--book/src/main/scalatex/book/handson/GettingStarted.scalatex326
-rw-r--r--book/src/main/scalatex/book/handson/PublishingModules.scalatex197
-rw-r--r--book/src/main/scalatex/book/handson/WebPage.scalatex267
-rw-r--r--book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex305
-rw-r--r--book/src/main/scalatex/book/indepth/CompilationPipeline.scalatex185
-rw-r--r--book/src/main/scalatex/book/indepth/DesignSpace.scalatex239
-rw-r--r--book/src/main/scalatex/book/indepth/JavaAPIs.scalatex44
-rw-r--r--book/src/main/scalatex/book/indepth/JavascriptInterop.scalatex1
-rw-r--r--book/src/main/scalatex/book/indepth/SemanticDifferences.scalatex247
28 files changed, 3499 insertions, 0 deletions
diff --git a/book/src/main/resources/css/side-menu.css b/book/src/main/resources/css/side-menu.css
new file mode 100755
index 0000000..b8e36c6
--- /dev/null
+++ b/book/src/main/resources/css/side-menu.css
@@ -0,0 +1,338 @@
+body {
+ color: #777;
+}
+*{
+ position: relative;
+}
+p{
+ text-align: justify;
+}
+#main{
+ position: fixed;
+ max-width: 100%;
+ right: 0px;
+ left: 0px;
+ height: 100%;
+ overflow-y: scroll
+}
+.pure-img-responsive {
+ max-width: 100%;
+ height: auto;
+}
+
+/*
+Add transition to containers so they can push in and out.
+*/
+#layout,
+#menu,
+.menu-link,{
+ -webkit-transition: all 0.2s ease-out;
+ -moz-transition: all 0.2s ease-out;
+ -ms-transition: all 0.2s ease-out;
+ -o-transition: all 0.2s ease-out;
+ transition: all 0.2s ease-out;
+}
+
+/*
+This is the parent `<div>` that contains the menu and the content area.
+*/
+#layout {
+ position: relative;
+ padding-left: 0;
+}
+ #layout.active {
+ position: relative;
+ left: 250px;
+ }
+ #layout.active #menu {
+ left: 250px;
+ width: 250px;
+ }
+
+ #layout.active .menu-link {
+ left: 250px;
+ }
+/*
+The content `<div>` is where all your content goes.
+*/
+.content {
+ margin: 0 auto;
+ padding: 0 2em;
+ max-width: 800px;
+ margin-bottom: 50px;
+ line-height: 1.6em;
+}
+
+.header {
+ margin: 0;
+ color: #333;
+ text-align: center;
+ padding: 2.5em 2em 0;
+ border-bottom: 1px solid #eee;
+ }
+ .header h1 {
+ margin: 0.2em 0;
+ font-size: 3em;
+ font-weight: 300;
+ }
+ .header h2 {
+ font-weight: 300;
+ color: #ccc;
+ padding: 0;
+ margin-top: 0;
+ }
+
+.content-subhead {
+ margin: 50px 0 20px 0;
+ font-weight: 300;
+ color: #888;
+}
+
+
+
+/*
+The `#menu` `<div>` is the parent `<div>` that contains the `.pure-menu` that
+appears on the left side of the page.
+*/
+
+#menu {
+ margin-left: -250px; /* "#menu" width */
+ width: 250px;
+ position: fixed;
+ top: 0;
+ left: 0;
+ bottom: 0;
+ z-index: 1000; /* so the menu or its navicon stays above all content */
+ background: #191818;
+ overflow-y: auto;
+ overflow-x: hidden;
+ -webkit-overflow-scrolling: touch;
+}
+ /*
+ All anchors inside the menu should be styled like this.
+ */
+ #menu a {
+ color: #999;
+ border: none;
+ padding: 0.6em 0 0.6em 0.6em;
+ }
+
+ /*
+ Remove all background/borders, since we are applying them to #menu.
+ */
+ #menu .pure-menu,
+ #menu .pure-menu ul {
+ border: none;
+ background: transparent;
+ }
+
+ /*
+ Add that light border to separate items into groups.
+ */
+ #menu .pure-menu ul,
+ #menu .pure-menu .menu-item-divided {
+ border-top: 1px solid #333;
+ }
+ /*
+ Change color of the anchor links on hover/focus.
+ */
+ #menu .pure-menu li a:hover,
+ #menu .pure-menu li a:focus {
+ background: #333;
+ }
+
+ /*
+ This styles the selected menu item `<li>`.
+ */
+ #menu .pure-menu-selected,
+ #menu .pure-menu-heading {
+ background: #1f8dd6;
+ }
+ /*
+ This styles a link within a selected menu item `<li>`.
+ */
+ #menu .pure-menu-selected {
+ color: #fff;
+ }
+
+ /*
+ This styles the menu heading.
+ */
+ #menu .pure-menu-heading {
+ font-size: 110%;
+ color: #fff;
+ margin: 0;
+ }
+
+/* -- Dynamic Button For Responsive Menu -------------------------------------*/
+
+/*
+The button to open/close the Menu is custom-made and not part of Pure. Here's
+how it works:
+*/
+
+/*
+`.menu-link` represents the responsive menu toggle that shows/hides on
+small screens.
+*/
+.menu-link {
+ position: fixed;
+ display: block; /* show this only on small screens */
+ top: 0;
+ left: 0; /* "#menu width" */
+ background: #000;
+ background: rgba(0,0,0,0.7);
+ font-size: 10px; /* change this value to increase/decrease button size */
+ z-index: 10;
+ width: 2em;
+ height: auto;
+ padding: 2.1em 1.6em;
+}
+
+ .menu-link:hover,
+ .menu-link:focus {
+ background: #000;
+ }
+
+ .menu-link span {
+ position: relative;
+ display: block;
+ }
+
+ .menu-link span,
+ .menu-link span:before,
+ .menu-link span:after {
+ background-color: #fff;
+ width: 100%;
+ height: 0.2em;
+ }
+
+ .menu-link span:before,
+ .menu-link span:after {
+ position: absolute;
+ margin-top: -0.6em;
+ content: " ";
+ }
+
+ .menu-link span:after {
+ margin-top: 0.6em;
+ }
+
+
+/* -- Responsive Styles (Media Queries) ------------------------------------- */
+
+/*
+Hides the menu at `48em`, but modify this based on your app's needs.
+*/
+@media (min-width: 72em) {
+
+ .header,
+ .content {
+ padding-left: 2em;
+ padding-right: 2em;
+ }
+
+ #layout {
+ padding-left: 250px; /* left col width "#menu" */
+ left: 0;
+ }
+ #main{
+ left: 250px;
+ }
+ #menu {
+ left: 250px;
+ }
+
+ .menu-link {
+ position: fixed;
+ left: 250px;
+ display: none;
+ }
+
+ #layout.active .menu-link {
+ left: 250px;
+ }
+}
+/* Custom Stuff */
+a:link {
+ color: #479;
+ text-decoration: none;
+}
+a:visited {
+ color: #858;
+ text-decoration: none;
+}
+a:hover {
+ text-decoration: underline;
+}
+a:active {
+ color: #000;
+ text-decoration: underline;
+}
+code{
+ color: #000;
+}
+.half-table{
+ width: 100%;
+}
+.half-table td{
+ width: 50%;
+}
+.half-table th{
+ width: 50%;
+}
+.header-link{
+ position: absolute;
+ right: 0px;
+}
+*:hover > .header-link:hover{
+ opacity: 1.0;
+}
+*:hover > .header-link:active{
+ opacity: 0.75;
+}
+*:hover > .header-link{
+ text-align: right;
+ text-decoration: none;
+ opacity: 0.5;
+}
+.header-link > i{
+ color: #777;
+}
+.header-link{
+ opacity: 0.05;
+}
+.collapsed.menu-item-list li.hide > ul{
+ opacity: 1;
+ overflow: hidden;
+ height: 0px;
+}
+.collapsed.menu-item-list li > ul{
+ opacity: 1;
+ overflow: hidden;
+}
+
+#menu .collapsed.menu-item-list li.selected > a{
+ border-left: 2px solid white;
+}
+
+.menu-item-list li > a{
+ -webkit-transition: background-color 0.2s ease-out;
+ -moz-transition: background-color 0.2s ease-out;
+ -ms-transition: background-color 0.2s ease-out;
+ -o-transition: background-color 0.2s ease-out;
+ transition: background-color 0.2s ease-out;
+}
+
+/*Override stuff from pure =/ it's not doing what I want*/
+.pure-menu li > ul{
+ position:relative;
+ visibility:visible;
+ left: 0px;
+ top: 0px;
+}
+
+/*Workaround for bug in highlight.js IDEA theme*/
+span.hljs-tag, span.hljs-symbol{
+ background: none;
+}
diff --git a/book/src/main/resources/favicon.png b/book/src/main/resources/favicon.png
new file mode 100644
index 0000000..ba64ad2
--- /dev/null
+++ b/book/src/main/resources/favicon.png
Binary files differ
diff --git a/book/src/main/resources/favicon.svg b/book/src/main/resources/favicon.svg
new file mode 100644
index 0000000..2f1181f
--- /dev/null
+++ b/book/src/main/resources/favicon.svg
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 17.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+ width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve">
+<line fill="none" stroke="#006400" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="34.833" y1="26.833" x2="41.833" y2="36"/>
+<line fill="none" stroke="#006400" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="48.833" y1="26.833" x2="41.833" y2="36"/>
+<line fill="none" stroke="#006400" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="48.833" y1="26.833" x2="55.833" y2="36"/>
+<line fill="none" stroke="#006400" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="62.833" y1="26.833" x2="55.833" y2="36"/>
+<line fill="none" stroke="#FF0000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="34.833" y1="7.833" x2="41.833" y2="17"/>
+<line fill="none" stroke="#FF0000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="48.833" y1="7.833" x2="41.833" y2="17"/>
+<line fill="none" stroke="#FF0000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="48.833" y1="7.833" x2="55.833" y2="17"/>
+<line fill="none" stroke="#FF0000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="62.833" y1="7.833" x2="55.833" y2="17"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="34.833" y1="46.833" x2="41.833" y2="56"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="48.833" y1="46.833" x2="41.833" y2="56"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="48.833" y1="46.833" x2="55.833" y2="56"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="62.833" y1="46.833" x2="55.833" y2="56"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="0" y1="9.5" x2="3" y2="9.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="0" y1="18.5" x2="3" y2="18.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="0" y1="36.5" x2="3" y2="36.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="0" y1="45.5" x2="3" y2="45.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="0" y1="54.5" x2="3" y2="54.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="7" y1="54.5" x2="10" y2="54.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="15" y1="54.5" x2="18" y2="54.5"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="24" y1="54.5" x2="27" y2="54.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="5" y1="45.5" x2="8" y2="45.5"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="5" y1="36.5" x2="8" y2="36.5"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="18" y1="36.5" x2="20" y2="36.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="12" y1="36.5" x2="16" y2="36.5"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="20" y1="9.5" x2="23" y2="9.5"/>
+<line fill="none" stroke="#0000FF" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="4" y1="9.5" x2="6" y2="9.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="7" y1="9.5" x2="10" y2="9.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="12" y1="9.5" x2="16" y2="9.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="18" y1="9.5" x2="20" y2="9.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="7" y1="18.5" x2="10" y2="18.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="7" y1="27.5" x2="10" y2="27.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="12" y1="45.5" x2="15" y2="45.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="22" y1="36.5" x2="25" y2="36.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="16" y1="45.5" x2="19" y2="45.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="15" y1="27.5" x2="18" y2="27.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="19" y1="27.5" x2="22" y2="27.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="0" y1="27.5" x2="3" y2="27.5"/>
+<line fill="none" stroke="#000000" stroke-width="3" stroke-linejoin="round" stroke-miterlimit="10" x1="24" y1="9.5" x2="27" y2="9.5"/>
+</svg>
diff --git a/book/src/main/resources/images/Dropdown.png b/book/src/main/resources/images/Dropdown.png
new file mode 100644
index 0000000..c28a60d
--- /dev/null
+++ b/book/src/main/resources/images/Dropdown.png
Binary files differ
diff --git a/book/src/main/resources/images/Hello World Console.png b/book/src/main/resources/images/Hello World Console.png
new file mode 100644
index 0000000..3b83de3
--- /dev/null
+++ b/book/src/main/resources/images/Hello World Console.png
Binary files differ
diff --git a/book/src/main/resources/images/Hello World White.png b/book/src/main/resources/images/Hello World White.png
new file mode 100644
index 0000000..f4d3774
--- /dev/null
+++ b/book/src/main/resources/images/Hello World White.png
Binary files differ
diff --git a/book/src/main/resources/images/Hello World.png b/book/src/main/resources/images/Hello World.png
new file mode 100644
index 0000000..d333b5f
--- /dev/null
+++ b/book/src/main/resources/images/Hello World.png
Binary files differ
diff --git a/book/src/main/resources/images/IntelliJ Hello.png b/book/src/main/resources/images/IntelliJ Hello.png
new file mode 100644
index 0000000..766df74
--- /dev/null
+++ b/book/src/main/resources/images/IntelliJ Hello.png
Binary files differ
diff --git a/book/src/main/resources/images/Scalatags Downloads.png b/book/src/main/resources/images/Scalatags Downloads.png
new file mode 100644
index 0000000..4ff118c
--- /dev/null
+++ b/book/src/main/resources/images/Scalatags Downloads.png
Binary files differ
diff --git a/book/src/main/resources/images/javascript-the-good-parts-the-definitive-guide.jpg b/book/src/main/resources/images/javascript-the-good-parts-the-definitive-guide.jpg
new file mode 100644
index 0000000..3c1a1c5
--- /dev/null
+++ b/book/src/main/resources/images/javascript-the-good-parts-the-definitive-guide.jpg
Binary files differ
diff --git a/book/src/main/scala/book/Book.scala b/book/src/main/scala/book/Book.scala
new file mode 100644
index 0000000..09be8ee
--- /dev/null
+++ b/book/src/main/scala/book/Book.scala
@@ -0,0 +1,90 @@
+package book
+import acyclic.file
+
+import scalatags.Text.tags2
+import scalatags.Text.all._
+
+/**
+ * Created by haoyi on 10/26/14.
+ */
+object Book {
+ val autoResources = Set(
+ "META-INF/resources/webjars/highlightjs/8.2-1/highlight.min.js",
+ "META-INF/resources/webjars/highlightjs/8.2-1/styles/idea.min.css",
+ "META-INF/resources/webjars/highlightjs/8.2-1/languages/scala.min.js",
+ "META-INF/resources/webjars/highlightjs/8.2-1/languages/javascript.min.js",
+ "META-INF/resources/webjars/highlightjs/8.2-1/languages/bash.min.js",
+ "META-INF/resources/webjars/highlightjs/8.2-1/languages/diff.min.js",
+ "META-INF/resources/webjars/highlightjs/8.2-1/languages/xml.min.js",
+ "META-INF/resources/webjars/pure/0.5.0/pure-min.css",
+ "META-INF/resources/webjars/pure/0.5.0/grids-responsive-min.css",
+ "META-INF/resources/webjars/font-awesome/4.2.0/fonts/FontAwesome.otf",
+ "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.eot",
+ "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.svg",
+ "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.ttf",
+ "META-INF/resources/webjars/font-awesome/4.2.0/fonts/fontawesome-webfont.woff",
+ "css/side-menu.css",
+ "example-opt.js",
+ "webpage/weather.js",
+ "favicon.svg",
+ "favicon.png"
+ )
+
+ val fontAwesomeCss =
+ "META-INF/resources/webjars/font-awesome/4.2.0/css/font-awesome.min.css"
+
+ val manualResources = Set(
+ "images/javascript-the-good-parts-the-definitive-guide.jpg",
+ "images/Hello World.png",
+ "images/Hello World White.png",
+ "images/Hello World Console.png",
+ "images/IntelliJ Hello.png",
+ "images/Dropdown.png",
+ "images/Scalatags Downloads.png",
+ fontAwesomeCss
+ )
+
+
+ val txt = Index()
+ val data = upickle.write(sect.structure)
+ val googleAnalytics =
+ """
+ |(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+ | (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+ | m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+ | })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+ |
+ | ga('create', 'UA-27464920-4', 'auto');
+ | ga('send', 'pageview');
+ """.stripMargin
+ val site = Seq(
+ raw("<!doctype html>"),
+ html(
+ head(
+ meta(charset:="utf-8"),
+ meta(name:="viewport", content:="width=device-width, initial-scale=1.0"),
+ link(rel:="shortcut icon", `type`:="image/png", href:="favicon.png"),
+ link(rel:="stylesheet", href:=fontAwesomeCss),
+ link(rel:="stylesheet", href:="styles.css"),
+ tags2.title("Hands-on Scala.js"),
+ script(src:="scripts.js"),
+ script(raw(googleAnalytics))
+ ),
+ body(
+ onload:=s"Controller().main($data)",
+ div(id:="layout")(
+ a(href:="#menu", id:="menuLink", cls:="menu-link")(
+ span
+ ),
+ div(id:="menu")
+
+ ),
+ div(id:="main",
+ div(id:="main-box")(
+ txt
+ )
+ )
+ )
+ )
+ ).render
+}
diff --git a/book/src/main/scala/book/BookData.scala b/book/src/main/scala/book/BookData.scala
new file mode 100644
index 0000000..d927b5f
--- /dev/null
+++ b/book/src/main/scala/book/BookData.scala
@@ -0,0 +1,45 @@
+package book
+
+import acyclic.file
+import scalatags.Text.TypedTag
+import scalatags.Text.all._
+object BookData {
+ lazy val javaAPIs = {
+ import java.io.File
+ def recursiveListFiles(f: File): Array[File] = {
+ val these = f.listFiles
+ these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
+ }
+
+ val roots = Seq(
+ "output/scala-js/javalanglib/src/main/scala",
+ "output/scala-js/javalib/src/main/scala"
+ )
+ for{
+ root <- roots
+ file <- recursiveListFiles(new File(root))
+ if file != null
+ if file.isFile
+ } yield{
+ val path = file.getPath
+ .drop(root.length + 1)
+ .dropRight(".scala".length)
+ val filename = path.replace('/', '.')
+ val docpath = s"https://docs.oracle.com/javase/7/docs/api/$path.html"
+ filename -> docpath
+ }
+ }
+ var counter = 0
+ def example(t: TypedTag[String], main: String) = {
+ val tagId = "example"+counter
+ counter += 1
+ Seq(
+ t(id:=tagId, display.block),
+ script(s"$main(document.getElementById('$tagId'))")
+ )
+ }
+ def split = div(cls:="pure-g")
+ def more = div(cls:="pure-u-1 pure-u-md-13-24")
+ def less = div(cls:="pure-u-1 pure-u-md-11-24")
+ def half = div(cls:="pure-u-1 pure-u-md-1-2")
+}
diff --git a/book/src/main/scala/book/Main.scala b/book/src/main/scala/book/Main.scala
new file mode 100644
index 0000000..cb8ef99
--- /dev/null
+++ b/book/src/main/scala/book/Main.scala
@@ -0,0 +1,66 @@
+package book
+import acyclic.file
+import java.io.InputStream
+import java.nio.file.{Paths, Files}
+
+
+
+object Main {
+ def write(txt: String, dest: String) = {
+ Paths.get(dest).toFile.getParentFile.mkdirs()
+ Files.deleteIfExists(Paths.get(dest))
+ Files.write(Paths.get(dest), txt.getBytes)
+ }
+ def copy(src: InputStream, dest: String) = {
+ Paths.get(dest).toFile.getParentFile.mkdirs()
+ Files.deleteIfExists(Paths.get(dest))
+ Files.copy(src, Paths.get(dest))
+ }
+
+ def main(args: Array[String]): Unit = {
+ println("Writing Book")
+
+ write(Book.site, "output/index.html")
+
+ val jsFiles = Book.autoResources.filter(_.endsWith(".js")).toSet
+ val cssFiles = Book.autoResources.filter(_.endsWith(".css")).toSet
+ val miscFiles = Book.autoResources -- cssFiles -- jsFiles
+
+ for(res <- Book.manualResources ++ miscFiles) {
+ copy(getClass.getResourceAsStream("/" + res), "output/" + res)
+ }
+
+ for((resources, dest) <- Seq(jsFiles -> "scripts.js", cssFiles -> "styles.css")) {
+ val blobs = for(res <- resources.iterator) yield {
+ io.Source.fromInputStream(getClass.getResourceAsStream("/"+res)).mkString
+ }
+
+ write(blobs.mkString("\n"), "output/"+dest)
+ }
+
+ val allNames = {
+ def rec(n: Tree[String]): Seq[String] = {
+ n.value +: n.children.flatMap(rec)
+ }
+ rec(sect.structure).toSet
+ }
+ val dupes = allNames.groupBy(x => x)
+ .values
+ .filter(_.size > 1)
+ .map(_.head)
+ .toSet
+
+ assert(dupes.size == 0, s"Duplicate names: $dupes")
+
+ val dangling = sect.usedRefs -- allNames
+
+ assert(dangling.size == 0, s"Dangling Refs: $dangling")
+
+ println("Writing Done")
+
+ // can be used to verify that no links are broken
+ // lnk.usedLinks
+ }
+
+
+}
diff --git a/book/src/main/scala/book/Utils.scala b/book/src/main/scala/book/Utils.scala
new file mode 100644
index 0000000..8c5c920
--- /dev/null
+++ b/book/src/main/scala/book/Utils.scala
@@ -0,0 +1,213 @@
+package book
+
+import acyclic.file
+import scala.collection.mutable
+import scalatags.Text.all._
+import scalatags.text.Builder
+
+case class pureTable(header: Frag*){
+ def apply(content: Frag*) = {
+ table(cls:="pure-table pure-table-horizontal half-table")(
+ thead(header),
+ tbody(content)
+ )
+ }
+}
+object sect{
+
+ var indent = 0
+
+ val headers = Seq[((String, String, Frag) => scalatags.Text.Tag, Option[Frag => Frag])](
+ ((h, s, l) => div(cls:="header")(
+ h1(h, l),
+ h2(s)
+ ), Some(f => div(cls:="content", f))),
+ ((h, s, l) => div(cls:="header")(
+ h1(id:=munge(h), h, l),
+ br
+ ), None),
+ (h1(_, _, _), None),
+ (h2(_, _, _), None),
+ (h3(_, _, _), None),
+ (h4(_, _, _), None),
+ (h5(_, _, _), None),
+ (h6(_, _, _), None)
+ )
+
+ var structure = Tree[String]("root", mutable.Buffer.empty)
+
+ val usedRefs = mutable.Set.empty[String]
+
+ def ref(s: String, txt: String = "") = {
+ usedRefs += s
+ a(if (txt == "") s else txt, href:=s"#${munge(s)}")
+ }
+
+ def munge(name: String) = {
+ name.replace(" ", "")
+ }
+}
+case class sect(name: String, subname: String = ""){
+ sect.indent += 1
+ val newNode = Tree[String](name, mutable.Buffer.empty)
+ val (headerWrap, contentWrap) = sect.headers(sect.indent-1)
+ sect.structure.children.append(newNode)
+ val prev = sect.structure
+ sect.structure = newNode
+ def apply(args: Frag*) = {
+ val wrappedContents = contentWrap.getOrElse((x: Frag) => x)(args)
+
+ val headingAnchor = a(
+ cls:="header-link",
+ href:=s"#${sect.munge(name)}",
+ " ",
+ i(cls:="fa fa-link")
+ )
+ val res = Seq[Frag](
+ headerWrap(name, subname, headingAnchor)(
+ cls:="content-subhead",
+ id:=sect.munge(name)
+ ),
+ wrappedContents
+ )
+ sect.indent -= 1
+ sect.structure = prev
+ res
+ }
+}
+case class Tree[T](value: T, children: mutable.Buffer[Tree[T]])
+object lnk{
+ val usedLinks = mutable.Set.empty[String]
+ def apply(name: String, url: String) = {
+ usedLinks.add(url)
+ a(name, href:=url)
+ }
+ object dom{
+ def mdnThing(name: String) = lnk(name, "https://developer.mozilla.org/en-US/docs/Web/API/" + name)
+ def mdnEvent(name: String) = lnk(name, "https://developer.mozilla.org/en-US/docs/Web/Events/" + name)
+ val CanvasRenderingContext2D = mdnThing("CanvasRenderingContext2D")
+ val HTMLCanvasElement = mdnThing("HTMLCanvasElement")
+ val Element = mdnThing("Element")
+ val HTMLElement = mdnThing("HTMLElement")
+ val HTMLInputElement = mdnThing("HTMLInputElement")
+ val HTMLSpanElement = mdnThing("HTMLSpanElement")
+ val XMLHttpRequest = mdnThing("XMLHttpRequest")
+ val getElementById = mdnThing("document.getElementById")
+ val setInterval = mdnThing("WindowTimers.setInterval")
+ val mousedown = mdnEvent("mousedown")
+ val mouseup = mdnEvent("mouseup")
+ val onclick = mdnEvent("onclick")
+ val onkeyup = mdnEvent("onkeyup")
+ val JSONparse = lnk("Json.parse", "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse")
+ }
+ object scala{
+ def apply(s: String) = {
+ lnk(s, "http://www.scala-lang.org/files/archive/nightly/docs/library/index.html#" + s)
+ }
+ }
+ object misc{
+ val IntelliJ = lnk("IntelliJ", "http://blog.jetbrains.com/scala/")
+ val Eclipse = lnk("Eclipse", "http://scala-ide.org/")
+ val Rhino = lnk("Rhino", "https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino")
+ val Nodejs = lnk("Node.js", "http://nodejs.org/")
+ val PhantomJS = lnk("PhantomJS", "http://phantomjs.org/")
+ val Play = lnk("Play", "https://www.playframework.com/")
+ val Scalatra = lnk("Scalatra", "http://www.scalatra.org/")
+ val ScalaTest = lnk("ScalaTest", "http://www.scalatest.org/")
+ val Scalate = lnk("Scalate", "https://github.com/scalate/scalate")
+ }
+ object github{
+ val Scalatags = lnk("Scalatags", "https://github.com/lihaoyi/scalatags")
+ val uPickle= lnk("uPickle", "https://github.com/lihaoyi/upickle")
+ val scalaPickling = lnk("scala-pickling", "https://github.com/scala/pickling")
+ }
+}
+
+object hl{
+ def highlight(snippet: Seq[String], lang: String) = {
+ val string = snippet.mkString
+ val lines = string.split("\n", -1)
+ if (lines.length == 1){
+ code(cls:=lang + " highlight-me", lines(0), padding:=0, display:="inline")
+ }else{
+ val minIndent = lines.map(_.takeWhile(_ == ' ').length)
+ .filter(_ > 0)
+ .min
+ val stripped = lines.map(_.drop(minIndent))
+ .dropWhile(_ == "")
+ .mkString("\n")
+
+ pre(code(cls:=lang + " highlight-me hljs", stripped))
+ }
+ }
+
+ def javascript(code: String*) = highlight(code, "javascript")
+ def scala(code: String*) = highlight(code, "scala")
+ def bash(code: String*) = highlight(code, "bash")
+ def diff(code: String*) = highlight(code, "diff")
+ def html(code: String*) = highlight(code, "xml")
+
+ val mappings = Seq(
+ "output/scala-js" -> "https://github.com/scala-js/scala-js",
+ "output/workbench-example-app" -> "https://github.com/lihaoyi/workbench-example-app",
+ "" -> "https://github.com/lihaoyi/hands-on-scala-js"
+ )
+ def ref(filepath: String, start: String = "", end: String = "\n") = {
+
+ val lang = filepath.split('.').last match {
+ case "js" => "javascript"
+ case "scala" => "scala"
+ case "sbt" => "scala"
+ case "sh" => "bash"
+ case "html" => "xml"
+ case x =>
+ println("??? " + x)
+ ???
+ }
+
+ val lines = io.Source.fromFile(filepath).getLines().toVector
+
+ def indent(line: String) = line.takeWhile(_.isWhitespace).length
+
+ val startLine = lines.indexWhere(_.contains(start))
+ if (startLine == -1){
+ throw new Exception("Can't find marker: " + start)
+ }
+ val whitespace = indent(lines(startLine))
+ val endLine = lines.indexWhere(
+ line => line.contains(end) || (indent(line) < whitespace && line.trim != ""),
+ startLine + 1
+ )
+ val sliced =
+ if (endLine == -1) lines.drop(startLine)
+ else lines.slice(startLine, endLine)
+
+ val blob = sliced.map(_.drop(whitespace)).mkString("\n")
+
+ val (prefix, url) =
+ mappings.iterator
+ .find{case (prefix, path) => filepath.startsWith(prefix)}
+ .get
+
+ val hash =
+ if (endLine == -1) ""
+ else s"#L$startLine-L$endLine"
+
+ val linkUrl =
+ s"$url/tree/master/${filepath.drop(prefix.length)}$hash"
+ pre(
+ code(cls:=lang + " highlight-me hljs", blob),
+ a(
+ cls:="header-link",
+ i(cls:="fa fa-link "),
+ position.absolute,
+ right:="0.5em",
+ bottom:="0.5em",
+ display.block,
+ fontSize:="24px",
+ href:=linkUrl,
+ target:="_blank"
+ )
+ )
+ }
+} \ No newline at end of file
diff --git a/book/src/main/scalatex/book/Index.scalatex b/book/src/main/scalatex/book/Index.scalatex
new file mode 100644
index 0000000..7a7e2b8
--- /dev/null
+++ b/book/src/main/scalatex/book/Index.scalatex
@@ -0,0 +1,83 @@
+@import BookData._
+
+@val firstHalfDescription = """
+is a set of tutorials that walks you through getting started with Scala.js. You'll build a range of small projects, from Making a Canvas App to Interactive Web Pages to Integrating Client-Server, and in the process will get a good overview of both Scala.js's use cases as well as the development experience
+"""
+@val secondHalfDescription = """
+is a set of detailed expositions on various parts of the Scala.js platform. Nothing in here is necessary for you to make your first demos, but as you dig deeper into the platform, you will likely need or want to care about these things so you can properly understand what's going on "under the hood"
+"""
+@sect("Hands-on Scala.js", "Writing client-side web applications in Scala")
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/Splash.scala", "var x")
+
+ @less
+ @BookData.example(canvas, "Splash().main")
+
+ @p
+ @lnk("Scala.js", "http://www.scala-js.org/") is a compiler that compiles Scala source code to equivalent Javascript code. That lets you write Scala code that you can run in a web browser, or other environments (Chrome plugins, Node.js, etc.) where Javascript is supported. This book is an introduction to Scala.js, which aims to get you from knowing-nothing about it to being relatively proficient.
+
+ @p
+ This book contains something for all levels of experience with Scala.js: absolute beginners can get started with the @sect.ref{Intro to Scala.js} and @sect.ref{Hands On} tutorial, people who have used it before can skip ahead to the later parts of the tutorial: @sect.ref{Making a Canvas App} or @sect.ref{Interactive Web Pages}. Intermediate users will find interest in the chapters on @sect.ref{Cross Publishing Libraries} with Scala.js or @sect.ref{Integrating Client-Server}, and even experienced users will find the @sect.ref{In Depth} documention useful. Feel free to explore the navigation bar on the left to find chapters of interest.
+
+ @p
+ Even if we do not require any familiarity of Scala.js, this book nonetheless assumes a good amount of background knowledge: of Scala, of Javascript, and of web development as a whole. In general, you will not need deep knowledge of any of these subjects, though if you are coming in entirely without knowledge of any one of them, you'll have to be willing to spend time Google-ing things and picking things up as we go along. Someone who comes in without previous web-dev experience may miss or not-notice many of the nice touches and benefits that Scala.js brings to the table, having never done web-dev any other way,
+
+ @p
+ Many of the code samples are taken from examples available on the book's @lnk("Github Page", "https://github.com/lihaoyi/scala-js-book"); for those code samples (e.g. the animation above), there is a @i(cls:="fa fa-link ") link in the bottom-right corner of the snippet that you can click on to go to the original code. These come in handy if you find you need additional context around the snippet, e.g. what imports you need for the code to work, or what the complete executable example looks like.
+
+ @p
+ This book is roughly divided into two sections:
+
+ @ul
+ @li
+ @sect.ref{Hands On} @firstHalfDescription
+ @li
+ @sect.ref{In Depth} @secondHalfDescription
+
+ @p
+ Feel free to jump ahead to either of them if you have some prior exposure to Scala.js. If not, it is best to start with the introduction...
+
+@sect{Intro to Scala.js}
+ @Intro()
+
+@sect("Hands On", "Writing your first Scala.js programs")
+ @p
+ This half of the book @firstHalfDescription
+
+ @sect{Getting Started}
+ @handson.GettingStarted()
+
+ @sect{Making a Canvas App}
+ @handson.CanvasApp()
+
+ @sect{Interactive Web Pages}
+ @handson.WebPage()
+
+ @sect{The Command Line}
+ @handson.CommandLine()
+
+ @sect{Cross Publishing Libraries}
+ @handson.PublishingModules()
+
+ @sect{Integrating Client-Server}
+ @handson.ClientServer()
+
+@sect("In Depth", "Exploring Scala.js")
+ @p
+ This half of the book @secondHalfDescription
+
+ @sect{Advanced Techniques}
+ @indepth.AdvancedTechniques()
+
+ @sect{Deviations from Scala-JVM}
+ @indepth.SemanticDifferences()
+
+ @sect{The Compilation Pipeline}
+ @indepth.CompilationPipeline()
+
+ @sect{Scala.js' Design Space}
+ @indepth.DesignSpace()
+
+ @sect{Java APIs}
+ @indepth.JavaAPIs() \ No newline at end of file
diff --git a/book/src/main/scalatex/book/Intro.scalatex b/book/src/main/scalatex/book/Intro.scalatex
new file mode 100644
index 0000000..5438cfb
--- /dev/null
+++ b/book/src/main/scalatex/book/Intro.scalatex
@@ -0,0 +1,182 @@
+@import BookData._
+@p
+ Scala.js compiles Scala code to equivalent, executable Javascript. Here's the compilation of a trivial hello-world example:
+
+@split
+ @half
+ @hl.scala
+ object Example extends js.JSApp{
+ def main() = {
+ var x = 0
+ while(x < 10) x += 3
+ println(x)
+ // 12
+ }
+ }
+
+ @half
+ @hl.javascript
+ ScalaJS.c.LExample$.prototype.main__V = (function() {
+ var x = 0;
+ while ((x < 10)) {
+ x = ((x + 3) | 0)
+ };
+ ScalaJS.m.s_Predef().println__O__V(x)
+ // 12
+ });
+
+@p
+ As you can see, both of the above programs do identical things: they'll count the variable @hl.scala{x} from @hl.scala{0}, @hl.scala{3}, @hl.scala{9}, and @hl.scala{12} before finally printing it out. It's just that the first is written in Scala and the second is in Javascript.
+
+@p
+ Traditionally, Scala has been a language which runs on the JVM. This eliminates it from consideration in many cases, e.g. when you need to build interactive web apps, the browser-client only runs Javascript. Even if your back-end is all written in Scala, you need to fall back to Javascript to run your client-side code, at a great loss in terms of toolability and maintainability. Scala.js lets you to develop web applications with the safety and toolability that comes with a statically typed language:
+
+@ul
+ @li
+ Typo-safety due to its compiler which catches many silly errors before the code is run
+ @li
+ In-editor support for autocomplete, error-highlighting, refactors, and intelligent navigation
+ @li
+ Moderate sized compiled executables, in the 100-400kb range
+ @li
+ Source-maps for ease of debugging
+
+@p
+ The value proposition is that due to the superior language and tooling, writing a web application in Scala.js will result in a codebase that is more flexible and robust than an equivalent application written in Javascript. The hope is that the benefits of using Scala.js will outweigh the additional (non-trivial) messiness of adding a whole new toolchain, as compared to directly writing raw Javascript.
+
+@p
+ I won't spend time on a detailed discussion on why Scala is good or why Javascript is bad; people's opinions on both sides can be found on the internet. The assumption is, going in, that you either already know and like Scala, or you are familiar with Javascript and are willing to try something new.
+
+@sect{About Javascript}
+ @p
+ Javascript is the language supported by web browsers, and is the only language available if you wish to write interactive web applications. As more and more activity moves online, the importance of web apps will only increase over time. Adobe Flash, Java Applets and Silverlight (which have historically allowed browser-client development in other languages) are all but dead: historically they have been the source of security vulnerabilities, none of them are available on the mobile browsers of Android or iOS or Windows8+. That leaves Javascript.
+
+ @sect{Javascript-the-language}
+ @p
+ Javascript is an OK language to do small-scale development: an animation here, an on-click transition there. There are a number of warts in the language, e.g. its verbosity, and a large amount of surprising behavior, but while your code-base doesn't extend past a few hundred lines of code, you often will not mind or care.
+
+ @p
+ However, Javascript is not an easy language to work in at scale: when your code-base extends to thousands, tens or hundreds of thousands of lines of code. The un-typed nature of the language, which is fine for small applications, becomes an issue when you are mainly working with code that you did not write.
+
+ @p
+ In a large code-base, finding out what methods or properties a variable has is often a long chase through dozens of files to see how it ended up being passed to the current function. Refactorings, which are OK when you can just test the code to see if it works, become dangerous when your code base is large enough that "just test all the code" would take hours. Language-warts which are slightly annoying in small programs become a minefield in large ones: it's only a matter of time before you hit one, often in code you did-not/cannot test, resulting in breakages in production.
+
+ @p
+ Apart from the inherent danger of the language, Javascript has another major problem: the language has left many things unspecified, yet at the same time provides the ability to emulate these things in a variety of ways. This means that rather than having a single way of e.g. defining a class and instantiating an object, there is a decade-long debate between a dozen different and equally-bad, hand-crafted alternatives. Large code-bases use third-party libraries, and most are guaranteed (purely due to how stastistics work) to do these basic things differently from your own code, making understanding these disparate code-bases (e.g. when something goes wrong) very difficult.
+
+ @p
+ To work in Javascript, you need the discipline to limit yourself to the sane subset of the language, avoiding all the pitfalls along the way:
+
+ @img(src:="images/javascript-the-good-parts-the-definitive-guide.jpg")
+
+ @p
+ Even if you manage to do so, what constitutes a pitfall and what constitutes a clever-language-feature changes yearly, making it difficult to maintain cohesiveness over time. This is compounded by the fact that refactoring is difficult, and so removing "unwanted" patterns from a large code-base a difficult (often multi-year) process.
+
+ @sect{Javascript-the-platform}
+ @p
+ However, even as Javascript-the-language sucks, Javascript-the-platform has some very nice properties that make it a good target for application developers:
+
+ @ul
+ @li
+ Zero-install distribution: just go to a URL and have the application downloaded and ready to use.
+ @li
+ Hyperlinks: being able to link to a particular page or item within a web app is a feature other platforms lack, and makes it much easier to cross-reference between different systems
+ @li
+ Sandboxed security: web applications are secure by default. No matter how sketchy the websites you visit, you can be sure that once you close the page, they're gone
+
+ @p
+ These features are all very nice to have, and together have made the web platform the success it is today. When you compare it to traditional applications, you can see the draw:
+ @ul
+ @li
+ Installing traditional desktop applications is usually a several-minute-long process. If something goes wrong in the installation, that often leaves a botched half-install on your computer which makes installing, uninstalling, or running the program impossible without manual surgery to excise the broken files.
+ @li
+ Desktop applications generally do not talk to each other at all. While on the web you can easily link a page to someone, trying to get someone to a particular screen in desktop software often involves a chain of screenshots with detailed instructions of which buttons to click at each stage.
+ @li
+ Desktop application security is non-existent. Install one rogue application and it can take over your computer, steal your credit card number, use your email for sending spam, and all sorts of other nasty things. Removing these for-good sometimes involves re-installing your entire operating system. Hence people are much more wary about only installing desktop software from people they "trust".
+
+ @p
+ In many ways, mobile App platforms like Android and iOS have closed the gap between "native" and "web" applications. Installing a new App may take 30 seconds, you can often deep-link to certain pages within an App, and Apps have a much tighter security model than desktop software does. Nevertheless, 30 seconds is still much longer than the 0.5 seconds it takes to open a web page, deep-linking in apps is not very prevalent, and the security model still often leaves space for rogue Apps to misbehave and steal data.
+
+ @hr
+ @p
+ Despite the problems with Javascript (and other tools like HTML an CSS, which have their own problems) the Web platform got a lot of things right, and the Desktop and Mobile platforms have a lot of catching up to do. If only we could improve upon the parts that aren't so great. This is where Scala.js comes in.
+
+@sect{About Scala.js}
+
+ @p
+ With Scala.js, you can cross compile your Scala code to a Javascript executable that can run on all major web browsers. You get all the benefits of the web platform in terms of deployability, security, and hyperlinking, with none of the problems of writing your software in Javascript. Scala.js provides a @sect.ref("The Language", "better language") to do your work in, but also provides some other goodies that have in-so-far never been seen in mainstream web development: @sect.ref("Sharing Code", "shared-code") and @sect.ref("Client-Server Integration", "client-server integration").
+
+ @sect{The Language}
+ @p
+ At a first approximation, Scala.js provides you a sane language to do development in the web browser. This saves you from an endless stream of Javascript warts like this one:
+
+ @hl.javascript
+ javascript> ["10", "10", "10", "10"].map(parseInt)
+ [10, NaN, 2, 3] // WTF
+
+ @hl.scala
+ scala> List("10", "10", "10", "10").map(parseInt)
+ List(10, 10, 10, 10) // Yay!
+
+
+ @p
+ Not only do you have an expressive language with static types, you also have great tooling with IDEs like IntelliJ and Eclipse, a rich library of standard collections, and many other modern conveniences that we take for granted but are curiously missing when working in the wild west of web development: the browser! You get all of the upside of developing for the web platform.
+
+ @p
+ While not useful for small applications, where most of the logic is gluing together external APIs, this comes in very useful in large applications where a lot of the complexity and room-for-error is entirely internal. With larger apps, you can no longer blame browser vendors for confusing APIs that make your code terrible: these confusing APIs only lurk in the peripherals around a larger, complex application. One thing you learn working in large-ish web client-side code-bases is that the bulk of the confusion and complexity is no-one's fault but your own, as a team.
+
+ @p
+ At this point, all of Google, Facebook, and Microsoft have all announced work on a typed variant of Javascript. These are not academic exercises: @lnk("Dart", "https://www.dartlang.org/")/@lnk("AtScript", "https://docs.google.com/document/d/11YUzC-1d0V1-Q3V0fQ7KSit97HnZoKVygDxpWzEYW0U/edit")/@lnk("Flow", "https://lobste.rs/s/fp9ibi/flow_facebook_s_new_javascript_type_checker")/@lnk("Typescript", "http://www.typescriptlang.org/") are all problems that solve a real need, that these large companies have all faced once they've grown beyond a certain size. Clearly, Javascript isn't cutting it anymore, and the convenience and "native-ness" of the language is more than made up for in the constant barrage of self-inflicted problems. Scala.js takes this idea and runs with it!
+
+ @sect{Sharing Code}
+ @p
+ Shared code is one of the holy-grails of web development. Traditionally the client-side code and server-side code has been written in separate languages: PHP or Perl or Python or Ruby or Java on the server, with only Javascript on the client. This means that algorithms were often implemented twice, constants copied-&-pasted, or awkward Ajax calls are made in an attempt to centralize the logic in one place (the server). With the advent of Node.js in the last few years, you can finally re-use the same code on the server as you can on the client, but with the cost of having all the previously client-only @sect.ref("Javascript-the-language", "problems with Javascript") now inflicted upon your server code base. Node.js expanded your range-of-options for writing shared client/server logic from "Write everything twice" to "Write everything twice, or write everything in Javascript". More options is always good, but it's not clear which of the two choices is more painful!
+
+ @p
+ Scala.js provides an alternative to this dilemma. With Scala.js, you can utilize the same libraries you use writing your Scala servers when writing your Scala web clients! On one end, you are sharing your templating language with @lnk("Scalatags", "https://github.com/lihaoyi/scalatags") or sharing your serialization logic with @lnk("uPickle", "https://github.com/lihaoyi/upickle"). At the other, you are sharing large, abstract libraries like @lnk("Scalaz", "https://github.com/japgolly/scalaz") or @lnk("Shapeless", "https://groups.google.com/forum/#!searchin/scala-js/shapeless/scala-js/5Sf2up0z3PU/9F9SYB0qHEcJ").
+
+ @p
+ Sharing code means several things:
+
+ @ul
+ @li
+ Not having to find two libraries to do a particular common task
+ @li
+ Not having to re-learn two different ways of doing the exact same thing
+ @li
+ Not needing to implement the same algorithms twice, for the times you can't find a good library to do what you want
+ @li
+ Not having to debug problems caused by subtle differences in the two implementations
+ @li
+ Not having to resort to awkward Ajax-calls or pre-computation to avoid duplicating logic between the client and server
+
+ @p
+ Shared code doesn't just mean sharing pre-made libraries between the client and server. You can easily @sect.ref("Cross Publishing Libraries", "publish your own libraries") that can be used on both Scala-JVM and Scala.js. This means that as a library author, you can at once target two completely different platforms, and (with some work) take advantage of the intricacies of each platform to optimize your library for each one. Take Scalatags as an example: as the first client-server Scala.js-ScalaJVM shared libraries, it enjoys a roughly even split of downloads from people using it on both platforms:
+
+ @img(src:="images/Scalatags Downloads.png", width:="100%")
+
+ @p
+ Shared code means that if you, as an application writer, want some logic to be available on both the client and server, you simply @sect.ref("A Client-Server Setup", "put it in a shared/ folder"), and that's the end of the discussion. No architectural patterns to follow, no clever techniques need to be involved. Shared logic, whether that means constants, functions, data structures, all the way to algorithms and entire libraries, can simply be placed in @code{shared/} and be instantly accessible from both your client-side web code and your server.
+
+ @p
+ Shared code has long been the holy-grail of web development. Even now, people speak of shared code as if it were a myth. With Scala.js, shared code is the simple, boring reality. And all this while, just as importantly, you don't need to re-write your large enterprise back-end systems in a language that doesn't scale well beyond 100s of lines of code.
+
+ @sect{Client-Server Integration}
+ @p
+ There is an endless supply of new platforms which have promised to change-the-way-we-do-web-development-forever. From old-timers like @lnk("Ur-Web", "http://www.impredicative.com/ur/"), to @lnk("GWT", "http://www.gwtproject.org/"), to Asana's @lnk("LunaScript", "https://asana.com/luna"), to more recently things like @lnk("Meteor.js", "https://www.meteor.com/").
+ @p
+ One common theme in all these platforms is that their main selling point is their tight, seamless client-server integration, to the point where you can just make method calls across the client-server boundary and the platform/language/compiler figures out what to do.
+ @p
+ With Scala.js and Scala-JVM, such conveniences like making method calls across the client-server boundary is the @sect.ref("Integrating Client-Server", "boring reality"). Not only are the calls transparent, they are also statically checked, so any mistake in the route name or the parameters it expects, or the result type it returns to you, will be caught by the compiler long before even manual testing. It becomes @sect.ref("What's Left?", "impossible to make a malformed Ajax call").
+ @p
+ There's a lot to be said for automating things using a computer. The entire field of software engineering is basically about automating tasks that were previously done manually: accounting, banking, making travel arrangements, and all that. However, in the world of web-development, there has always been one set of tasks that has traditionally be done manually: the task of ensuring the web-clients are properly synchronized with the web-servers. Communication between the two has always been a manual, tedious, error-prone process, and mistakes often end un-noticed until something breaks in production.
+ @p
+ With Scala.js, like the other experimental platforms that have come before us, we attempt to provide a way forward from this manual-tedium.
+
+ @hr
+
+ @p
+ In many ways, Scala.js all-at-once provides many of the traditional holy-grails of web development: People have always dreamed about doing web development in a sane, best-of-breed language that compiles to both client and server. Of not having to worry too hard about whether code goes on the client or on the server, and being able to move or share it if necessary. Of having a compiler that will verify and check that your entire system is correct.
+
+ @p
+ Scala.js provides all these things, and much more. If you're interested enough to want to make use of Scala.js, read on!
diff --git a/book/src/main/scalatex/book/handson/CanvasApp.scalatex b/book/src/main/scalatex/book/handson/CanvasApp.scalatex
new file mode 100644
index 0000000..230ae67
--- /dev/null
+++ b/book/src/main/scalatex/book/handson/CanvasApp.scalatex
@@ -0,0 +1,210 @@
+@import BookData._
+@p
+ By this point, you've already cloned and got your hands dirty fiddling around with the toy @lnk("workbench-example-app", "https://github.com/lihaoyi/workbench-example-app"). You have your editor set up, SBT installed, and have published the example application in a way you can host online for other people to see. Maybe you've even made some changes to the application to see what happens. Hopefully you're curious, and want to learn more.
+
+@p
+ In this section of the book, we will walk through making a small canvas application. This will expose you to important concepts like:
+
+@ul
+ @li
+ Taking input with Javascript event handlers
+ @li
+ Writing your application logic in Scala
+ @li
+ Using a timer to drive periodic actions
+
+@p
+ In general, while the previous chapter was mostly set-up and exploring the Scala.js project, this chapter will walk you through actually writing a non-trivial, self-contained Scala.js application. Throughout this chapter, we will only be making modifications to @code{ScalaJSExample.scala}; the rest of the project will remain unchanged.
+
+@sect{Making a Sketchpad using Mouse Input}
+
+ @p
+ To begin with, lets remove all the existing stuff in our @code{.scala} file and leave only the @hl.scala{object} and the @hl.scala{main} method. Let's start off with some necessary boilerplate:
+
+ @hl.ref("examples/demos/src/main/scala/canvasapp/ScratchPad.scala", "/*setup*/", end = "/*code*/")
+
+ @p
+ As described earlier, this code uses the @lnk.dom.getElementById} function to fish out the @code{canvas} element that we interested in from the DOM. It then gets a rendering context from that @code{canvas}, and sets the height and width of the canvas to completely fill its containing element. Lastly, it fills out the canvas light-gray, so that we can see it on the page.
+
+ @p
+ Next, let's set some event handlers on the canvas:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/canvasapp/ScratchPad.scala", "/*code*/")
+
+ @less
+ @BookData.example(canvas, "ScratchPad().main")
+
+ @p
+ This code sets up the @lnk.dom.mousedown and @lnk.dom.mouseup events to keep track of whether or not the mouse has currently been clicked. It then draws black squares any time you move the mouse while the button is down. This lets you basically click-and-drag to draw pictures on the canvas. Try it out!
+
+ @p
+ In general, you have access to all the DOM APIs through the @hl.scala{dom} package as well as through Javascript objects such as the @lnk.dom.HTMLCanvasElement. Setting the @code{onmouseXXX} callbacks is just one way of interacting with the DOM. With Scala.js, you also get a very handy autocomplete in the editor, which you can use to browse the various other APIs that are available for use:
+
+ @img(src:="images/Dropdown.png", maxWidth:="100%")
+
+ @p
+ Apart from mouse events, keyboard events, scroll events, input events, etc. are all usable from Scala.js as you'd expect. If you have problems getting this to work, feel free to click on the link @i(cls:="fa fa-link ") icon below the code snippet to see what the full code for the example looks like
+
+@sect{Making a Clock using setInterval}
+
+ @p
+ You've already seen this in the previous example, but @lnk.dom.setInterval can be used to schedule recurring, periodic events in your program. Common use cases include running the @lnk("event loop for a game", "http://gameprogrammingpatterns.com/game-loop.html"), making smooth animations, and other tasks of that sort which require some work to happen over a period of time.
+
+ @p
+ Again, we need roughly the same boilerplate as just now to set up the canvas:
+
+ @hl.ref("examples/demos/src/main/scala/canvasapp/Clock.scala", "/*setup*/", "/*code*/")
+
+ @p
+ The only thing unusual here is that I'm going to create a @hl.scala{linearGradient} in order to make the stopwatch look pretty. This is by no means necessary, and you could simply make the @hl.scala{fillStyle} @hl.scala{"black"} if you want to keep things simple.
+
+ @p
+ Once that's done, it's only a few lines of code to set up a nice, live clock:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/canvasapp/Clock.scala", "/*code*/")
+
+ @less
+ @BookData.example(canvas, "Clock().main")
+
+ @p
+ As you can see, we're using more @lnk("Canvas APIs", "https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D"), in this case dealing with rendering text on the canvas. Another thing we're using is the Javascript @lnk("Date", "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date") class, in Scala.js under the full name @hl.scala{scala.scalajs.js.Date}, here imported as @hl.scala{js.Date}. Again, click on the link @i(cls:="fa fa-link ") icon to view the full-code if you're having trouble here.
+
+@sect{Tying it together: Flappy Box}
+
+ @p
+ You've just seen two examples of how to use Scala.js, together with the Javascript DOM APIs, to make simple applications. However, we've only used the "Scala" in Scala.js in the most rudimentary fashion: setting a few primitives here and there, defining some methods, mainly just gluing together a few Javascript APIs
+
+ @p
+ In this example we will make a spiritual clone of the popular @lnk("Flappy Bird", "http://en.wikipedia.org/wiki/Flappy_Bird") video game. This game involves a few simple rules
+
+ @ul
+ @li
+ Your character starts in the middle of the screen
+ @li
+ Gravity pulls your character down
+ @li
+ Clicking/tapping the screen makes your character jump up
+ @li
+ There are obstacles that approach your character from the right side of the screen, and you have to make sure you go through the hole in each obstacle to avoid hitting it
+ @li
+ Don't go out of bounds!
+
+ @p
+ It's a relatively simple game, but there should be enough "business logic" in here that we won't be simply gluing together APIs. Let's start!
+
+ @sect{Setting Up the Canvas}
+ @hl.ref("examples/demos/src/main/scala/canvasapp/FlappyLine.scala", "/*setup*/", end="/*variables*/")
+
+ @p
+ This section of the code is peripherally necessary, but not core to the implementation or logic of Flappy Box. We see the same @hl.scala{canvas}/@hl.scala{renderer} logic we've seen in all our examples, along with some logic to make the canvas a reasonable size, and some configuration of how we will render text to the canvas.
+
+ @p
+ In general, code like this will usually end up being necessary in a Scala.js program: the Javascript APIs that the browser provides to do things often ends up being somewhat roundabout and verbose. It's somewhat annoying to have to do for a small program such as this one, but in a larger application, the cost is both spread out over thousands of lines of code and also typically hidden away in helper functions, so the verbosity and non-idiomatic-scala-ness doesn't bother you much.
+
+ @sect{Defining our State}
+ @hl.ref("examples/demos/src/main/scala/canvasapp/FlappyLine.scala", "/*variables*/", end="def runLive")
+
+ @p
+ This is where we start defining things that are relevant to Flappy Box. There are roughly two groups of values here: immutable constants in the top group, and mutable variables in the bottom. The rough meaning of each variable is documented in the comments, and we'll see exactly how we use them later.
+
+ @p
+ One notable thing is that we're using a @lnk("collection.mutable.Queue", "http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html") to store the list of obstacles. This is defined in the Scala standard library; in general, all the collections in the Scala standard library can be used without issue in Scala.js.
+
+ @sect{Game Logic}
+ @hl.ref("examples/demos/src/main/scala/canvasapp/FlappyLine.scala", "def runLive", "def runDead")
+
+ @p
+ The @hl.scala{runLive} function is the meat of Flappy Box. In it, we
+
+ @ul
+ @li
+ Clear the canvas
+ @li
+ Generate new obstacles
+ @li
+ Apply velocity and acceleration to the player
+ @li
+ Check for collisions or out-of-bounds, killing the player if it happens
+ @li
+ Rendering everything, including the player as the namesake box
+
+ @p
+ This function basically contains all the game logic, from motion, to collision-detection, to rendering, so it's pretty large. Not that large though! And entirely understandable, even if it takes a few moments to read through.
+
+ @hl.ref("examples/demos/src/main/scala/canvasapp/FlappyLine.scala", "def runDead", "def run()")
+
+ @p
+ This is the function that handles what happens when you're dead. Essentially, we reset all the mutable variables to their initial state, and just count down the @hl.scala{dead} counter until it reaches zero and we're considered alive again.
+
+ @sect{A Working Product}
+ @hl.ref("examples/demos/src/main/scala/canvasapp/FlappyLine.scala", "def run()")
+
+ @p
+ And finally, this is the code that kicks everything off: we define the @hl.scala{run} function to swap between @hl.scala{runLive} and @hl.scala{runDead}, register an @lnk.dom.onclick handler to make the player jump by tweaking his velocity, and we call @lnk.dom.setInterval to run the @hl.scala{run} function every 20 milliseconds.
+
+ @p
+ At almost 100 lines of code, this is quite a meaty example! Nonetheless, when all is said and done, you will find that the example actually works! Try it out!
+
+ @div
+ @BookData.example(canvas, "FlappyLine().main")
+
+@sect{Canvas Recap}
+ @p
+ We've now gone through the workings of building a handful of toy applications using Scala.js. What have we learnt in the process?
+
+ @sect{Development Speed}
+ @p
+ We've by now written a good chunk of Scala.js code, and perhaps debugged some mysterious errors, and tried some new things. One thing you've probably noticed is the efficiency of the process: you make a change in your editor, the browser reloads itself, and life goes on. There is a compile cycle, but after a few runs the compiler warms up and the compilation cycle drops to less than a second.
+ @p
+ Apart from the compilation/reload speed, you've probably noticed the benefit of tooling around Scala.js. Unlike Javascript editors, your existin Scala IDEs like @lnk.misc.IntelliJ or @lnk.misc.Eclipse can give very useful help when you're working with Scala.js. Autocomplete, error-highlighting, jump-to-definition, and a myriad other modern conveniences that are missing when working in dynamically-typed languages are present when working in Scala.js. This makes the code much less mysterious: you're no longer trying to guess what methods a value has, or what a method returns: it's all laid out in front of you in plain sight.
+
+ @sect{Full Scala}
+ @p
+ All of the examples so far have been very self-contained: they do not touch the HTML DOM, they do not make Ajax calls, or try to access web services. They don't push the limits of the browser's API.
+
+ @p
+ Nevertheless, these examples have exercised a good amount of the Scala language. List comprehensions, collections, the math library, and more. In general, most of the Scala standard library works under Scala.js, as well as a large number of third-party libraries. Unlike many other compile-to-Javascript languages out there, this isn't a language-that-looks-like-Scala: it is Scala through and through, with a tiny number of semantic differences.
+
+ @sect{Seamless Javascript Interop}
+ @p
+ Even if we take some time to read through the code we've written, it is not immediately obvious which bits of code are Scala and which bits are Javascript! It all kind of meshes together, for example if we take the Flappy Box source code:
+
+ @ul
+ @li
+ @hl.scala{obstacles} is a Scala @lnk("mutable.Queue", "http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html"), as we defined it earlier, and all the methods on it are Scala method calls
+ @li
+ @hl.scala{renderer} is a Javascript @lnk.dom.CanvasRenderingContext2D, and all the methods on it are Javascript method calls directly on the Javascript object
+ @li
+ @hl.scala{frame} is a Scala @hl.scala{Int}, and obeys Scala semantics, though it is implemented as a Javascript @hl.javascript{Number} under the hood.
+ @li
+ @hl.scala{playerY} and @hl.scala{playerV} are Scala @hl.scala{Double}s, implemented directly as Javascript @hl.javascript{Number}s
+
+ @p
+ This reveals something pretty interesting about Scala.js: even though Scala at-first-glance is a very different language from Javascript, the interoperation with Javascript is so seamless that you can't even tell from the code which values/methods are defined in Scala and which values/methods come from Javascript!
+ @p
+ These two classes of values/methods are treated very differently by the compiler when it comes to emitting the executable Javascript blob, but the compiler does not need extra syntax telling it which things belong to Scala and which to Javascript: the types are sufficient. @hl.scala{renderer}, for example is of type @lnk.dom.CanvasRenderingContext2D which is a subtype of @hl.scala{scalajs.js.Object}, indicating to the compiler that it needs special treatment. Primitives like @hl.scala{Double}s and @hl.scala{Int}s have similar treatment
+
+ @p
+ Overall, this seamless mix of Scala and Javascript values/methods/functions is a common theme in Scala.js applications, so you should expect to see more of it in later chapters of the book.
+
+ @hr
+
+ @p
+ You've now had some experience building small canvas applications in Scala.js. Why not try exercising your new-found skills? Here are some possibilities:
+
+ @ul
+ @li
+ Make more video games! I have a set of @lnk("retro-games ported to Scala.js", "http://lihaoyi.github.io/scala-js-games/"). Maybe re-make one of them without looking at the source, or maybe port some other game you're familiar with and enjoy playing. Even just drawing things on canvas, games can get @lnk("pretty elaborate", "http://lihaoyi.github.io/roll/").
+
+ @li
+ Explore the APIs! We've touched on a small number of Javascript APIs here, mainly for dealign with the canvas, but modern browsers offer a whole @lnk("zoo of functionality", "https://developer.mozilla.org/en-US/docs/Web/API"). Try making an application that uses @lnk("localStorage", "https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage") to save state even when you close the tab, or an application that works with the HTML DOM.
+
+ @li
+ Draw something pretty! We have a working canvas, a nice programming language, and a way to easily publish the results online. @lnk("Various", "http://www.scala-js-fiddle.com/gist/77a3840678d154257ca1/KochSnowflake.scala") @lnk("fractals", "http://www.scala-js-fiddle.com/gist/77a3840678d154257ca1/KochCurve.scala"), or @lnk("colorful visualizations", "http://www.scala-js-fiddle.com/gist/9443f8e0ecc68d1058ad/RayTracer.scala") are all possibilities.
+
+ @p
+ By this point you've some experience building stand-alone, single-canvas Scala.js applications, which has hopefully given you a feel for how Scala.js works. The problem is that few web applications satisfy the criteria of being stand-alone single-page canvas applications! Most web applications need to deal with the DOM of the HTML page, need to fetch data from web services, and generally need to do a lot of other messy things. We'll go into that in the next chapter
diff --git a/book/src/main/scalatex/book/handson/ClientServer.scalatex b/book/src/main/scalatex/book/handson/ClientServer.scalatex
new file mode 100644
index 0000000..0a41dae
--- /dev/null
+++ b/book/src/main/scalatex/book/handson/ClientServer.scalatex
@@ -0,0 +1,244 @@
+
+@p
+ Historically, sharing code across client & server has been a holy-grail for web development. There are many things which have made it hard in the past:
+
+@ul
+ @li
+ Javascript on the client v.s. PHP/Perl/Python/Ruby/Java on the client
+ @li
+ Most back-ends make heavy use of C extensions, and front-end code was tightly coupled to the DOM. Even if you manage to port the main language
+@p
+ There have been some attempts in recent years with more traction: Node.js, for example, has been very successful at running Javascript on the server, the Clojure/Clojurescript community has their own version of cross-built code, and there are a number of smaller, more esoteric platforms.
+
+@p
+ Scala.js lets you share code between client and server relatively straightforwardly. As we saw in the previous chapter, where we made a shared module. Let's work to turn that shared module into a working client-server application!
+
+@sect{A Client-Server Setup}
+ @p
+ Getting started with client-server integration, let's go with the simplest configuration possible: a Spray server and a Scala.js client. Most of the other web-frameworks (@lnk.misc.Play, @lnk.misc.Scalatra, etc.) will have more complex configurations, but the basic mechanism of wiring up Scala.js to your web framework will be the same. Our project will look like this:
+
+ @hl.bash
+ $ tree
+ .
+ ├── build.sbt
+ ├── client
+ │   ├── shared/main/scala/simple/FileData.scala
+ │   └── src/main/scala/simple/Client.scala
+ ├── project
+ │   └── build.sbt
+ └── server
+ ├── shared -> ../client/shared
+ └── src/main/scala/simple
+ ├── Page.scala
+ └── Server.scala
+
+ @p
+ First, let's do the wiring in @code{build.sbt}:
+
+ @hl.ref("examples/crossBuilds/clientserver/build.sbt")
+
+ @p
+ We have two projects: @code{client} and @code{server}, one of which is a Scala.js project (indicated by the presence of @hl.scala{scalaJSSettings}). Both projects share a number of settings: the presence of the @code{shared/} folder, which shared code can live in (similar to what we saw in @sect.ref{Cross Publishing Libraries}) and the settings to add @lnk.github.Scalatags and @lnk.github.uPickle to the build. Note that those two dependencies use the triple @code{%%%} instead of the double @code{%%} to declare: this means that for each dependency, we will pull in the Scala-JVM or Scala.js version depending on whether it's being used in a Scala.js project.
+ @p
+ The @code{client} subproject is uneventful, with a dependency on the by-now-familiar @code{scalajs-dom} library. The @code{server} project, on the other hand, is interesting: it contains the dependencies required for us to set up out Spray server, and one additional thing: we add the output of @code{fastOptJS} from the client to the @code{resources} on the server. This will allow the @code{server} to serve the compiled-javascript from our @code{client} project from its resources.
+
+ @p
+ Next, let's kick off the Spray server in our Scala-JVM main method:
+
+ @hl.ref("examples/crossBuilds/clientserver/server/src/main/scala/simple/Server.scala")
+
+ @p
+ This is a not-very-interesting @lnk("spray-routing", "http://spray.io/documentation/1.2.2/spray-routing/") application: we set up a server on @code{localhost:8080}, have the root URL serve the main page on GET, and have other GET URLs serve resources. This includes the @code{js-fastopt.js} file that is now in our resources because of our @code{build.sbt} config earlier! We also add a POST route to allow the client ask the server to list files various directories.
+
+ @p
+ The HTML template @hl.scala{Page.skeleton} is not shown above; I put it in a separate file for neatness:
+
+ @hl.ref("examples/crossBuilds/clientserver/server/src/main/scala/simple/Page.scala")
+
+ @p
+ This is a typical @lnk.github.Scalatags HTML snippet. Note that since we're serving it directly from the server in Scala code, we do not need to leave a @code{.html} file somewhere on the filesystem! We can declare all HTML, including the skeleton of the page, in Scalatags. Otherwise it's the same as what we saw in earlier chapters: A simple HTML page which includes a script tag to run our Scala.js application.
+ @p
+ Lastly, we'll set up the Scala.js main method, which we are calling in the @hl.html{<script>} tag above to kick off the client-side application.
+
+ @hl.ref("examples/crossBuilds/clientserver/client/src/main/scala/simple/Client.scala")
+
+ @p
+ Again this is a simple Scala.js application, not unlike what we saw in earlier chapters. However, there is one difference: earlier, we made our Ajax calls to @code{api.openweathermap.org/...}. Here, we're making it to @code{/ajax}: the same server the page is served from!
+
+ @p
+ You may have noticed in both client and server, we have made reference to a mysterious @hl.scala{FileData} type which holds the name and size of each file. @hl.scala{FileData} is defined in the @code{shared/} folder, so it can be accessed from both Scala-JVM and Scala.js:
+
+ @hl.ref("examples/crossBuilds/clientserver/client/shared/main/scala/simple/FileData.scala")
+
+ @p
+ Now, if we go to the browser at @code{localhost:8080}, we should see our web-page!
+
+@sect{Client-Server Reflections}
+ @p
+ By now you've already set up your first client-server application. However, it might not be immediately clear what we've done and why it's interesting! Here are some points to consider.
+
+ @sect{Shared Templating}
+
+ @p
+ In both the client code and the server code, we made use of the same Scalatags HTML generation library. This is pretty neat: transferring rendering logic between client and server no longer means an annoying/messy rewrite! You can simply C&P the Scalatags snippet over. That means it's easy if you want to e.g. shift the logic from one side to the other in order to optimize for performance or time-to-load or other things.
+ @p
+ One thing to take note of is that we're actually using subtly @i{different} implementations of Scalatags on both sides: on the server, we're importing from @hl.scala{scalatags.Text}, while on the client we're using @hl.scala{scalatags.JsDom}. The @hl.scala{Text} backend renders directly to Strings, and is available on both Scala-JVM and Scala.js. The @hl.scala{JsDom} backend, on the other hand, renders to @lnk.dom.HTMLElement-s which only exist on Scala.js. Thus while on the client you can do things like attach event listeners to the rendered @lnk.dom.HTMLElement objects, or checking their runtime @code{.value}, on the server you can't. And that's exactly what you want!
+
+ @sect{Shared Code}
+ @p
+ One thing that we skimmed over is the fact that we could easily define our @hl.scala{case class FileData(name: String, size: Long)} in the @code{shared/} folder, and have it instantly and consistently available on both client and server. This perhaps does not seem so amazing: we've already done many similar things earlier when we were building Cross-platform Modules. Nevertheless, in the context of web development, it is a relatively novel idea to be able to ad-hoc share bits of code between client and server.
+ @p
+ Sharing code is not limited to class definitions: @i{anything} can be shared. Objects, classes, interfaces/traits, functions and algorithms, constants: all of these are things that you will likely want to share at some point or another. Traditionally, people have simply re-implemented the same code twice in two languages, or have resorted to awkward Ajax calls to push the logic to the server. With Scala.js, you no longer need to do so: you can easily, create ad-hoc bits of code which are available on both platforms.
+
+ @sect{Boilerplate-free Serialization}
+ @p
+ The Ajax/RPC layer is one of the more fragile parts of web applications. Often, you have your various Ajax endpoints written once on the server, have a set of routes written to connect those Ajax endpoints to URLs, and client code (traditionally Javascript) made calls to those URLs with "raw" data: basically whatever you wanted, packed in an ad-hoc mix of CSV and JSON and raw-strings.
+
+ @p
+ This has always been annoying boilerplate, and Scala.js removes it. With @lnk.github.uPickle, you can simply call @hl.scala{upickle.write(...)} and @hl.scala{upickle.read[T](...)} to convert your collections, primitives or case-classes to and from JSON. This means you do not need to constantly re-invent different ways of making Ajax calls: you can just fling the data right across the network from client to server and back again.
+
+@sect{What's Left?}
+ @p
+ We've built a small client-server web application with a Scala.js web-client that makes Ajax calls to a Scala-JVM web-server running on Spray. We performed these Ajax calls using uPickle to serialize the data back and forth, so serializing the arguments and return-value was boilerplate-free and correct.
+
+ @p
+ However, there is still some amount of duplication in the code. In particular, the definition of the endpoint name "list" is duplicated 4 times:
+
+ @hl.ref("examples/crossBuilds/clientserver/server/src/main/scala/simple/Server.scala", """path("ajax" / "list")""", "")
+ @hl.ref("examples/crossBuilds/clientserver/server/src/main/scala/simple/Server.scala", "list(", "")
+ @hl.ref("examples/crossBuilds/clientserver/server/src/main/scala/simple/Server.scala", "def list", "")
+ @hl.ref("examples/crossBuilds/clientserver/client/src/main/scala/simple/Client.scala", "ajax/list", "")
+
+ @p
+ Three times on the server and once on the client! What's worse, two of the appearances of @hl.scala{"list"} are in string literals, which are not checked by the compiler to match up with themselves or the name of the method @hl.scala{list}. Apart from this, there is one other piece of duplication that is unchecked: the type being returned from @hl.scala{list} (@hl.scala{Seq[FileData]}) is being repeated on the client in @hl.scala{upickle.read[Seq[FileData]]} in order to de-serialize the serialized data. This leaves three opportunities for error wide-open:
+
+ @ul
+ @li
+ You could change the string literals @hl.scala{"list"} and forget to change the method-name @hl.scala{list}, thus confusing future maintainers of the code.
+ @li
+ You could change one of literal @hl.scala{"list"}s but forget to change the other, thus causing an error at run-time (e.g. a 404 NOT FOUND response)
+ @li
+ You could update the return type of the @hl.scala{list} method and forget to update the @hl.scala{upickle.read} deserialization call on the client, resulting in a deserialization failure at runtime.
+
+ @p
+
+ @p
+ Neither of these scenarios is great! Although we've already made great progress in making our client-server application type-safe (via Scala.js on the client) and DRY (via shared code in @code{shared/}) we still have this tiny bit of annoying, un-checked duplication and danger lurking in the code-base. The basic problem is that what is normally called the "routing layer" in the web application is still unsafe, and so these silly errors can go un-caught and blow up on unsuspecting developers at run-time. Let's see how we can fix it.
+
+@sect{Autowire}
+
+ @p
+ @lnk("Autowire", "https://github.com/lihaoyi/autowire") is a library that turns your request routing layer from a fragile, hand-crafted mess into a solid, type-checked, boilerplate-free experience. Autowire basically turns what was previously a stringly-typed, hand-crafted Ajax call and route:
+
+ @hl.ref("examples/crossBuilds/clientserver/client/src/main/scala/simple/Client.scala", "ajax/list", "")
+
+ @p
+ Into a safe, type-checked function call:
+
+ @hl.ref("examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala", ".call()", "")
+
+ @p
+ Let's see how we can do that.
+
+ @sect{Setting up Autowire}
+
+ @p
+ To begin with, Autowire requires you to provide three things:
+
+ @ul
+ @li
+ An @hl.scala{autowire.Server} on the Server, set up to feed the incoming request into Autowire's routing logic
+ @li
+ An @hl.scala{autowire.Client} on the Client, set up to take a serialized request and send it across the network to the server.
+ @li
+ An interface (A Scala @hl.scala{trait}) which defines the interface between these two
+
+ @p
+ Let's start with our client-server interface definition
+
+ @hl.ref("examples/crossBuilds/clientserver2/client/shared/main/scala/simple/Shared.scala")
+
+ @p
+ Here, you can see that in addition to sharing the @hl.scala{FileData} class, we are also creating an @hl.scala{Api} trait which contains the signature of our @hl.scala{list} method. The exact name of the trait doesn't matter. We need it to be in @code{shared/} so that the code in both client and server can reference it.
+
+ @p
+ Next, let's look at modifying our server code to make use of Autowire:
+
+ @hl.ref("examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala")
+
+ @p
+ Now, instead of hard-coding the route @hl.scala{"ajax" / "list"}, we now take in any route matching @hl.scala{"ajax" / Segments}, feeding the resultant path segments into the @hl.scala{Router} object:
+
+ @hl.ref("examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala", "path(")
+ @p
+ The @hl.scala{Router} object in turn simply defines how you intend the objects to be serialized and deserialized:
+
+ @hl.ref("examples/crossBuilds/clientserver2/server/src/main/scala/simple/Server.scala", "object Router", "object Server")
+
+ @p
+ In this case using uPickle. Note how the @hl.scala{route} call explicitly states the type (here @hl.scala{Api}) that it is to generate routes against; this ensures that only methods which you explicitly put in your public interface @hl.scala{Api} are publically reachable.
+
+ @p
+ Next, let's look at the modified client code:
+
+ @hl.ref("examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala")
+
+ @p
+ There are two main modifications here: the existence of the new @hl.scala{Ajaxer} object, and the modification to the Ajax call-site. Let's first look at @hl.scala{Ajaxer}:
+
+ @hl.ref("examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala", "object Ajaxer", "@JSExport")
+
+ @p
+ Like the @hl.scala{Router} object, @hl.scala{Ajaxer} also defines how you perform the serialization and deserialization of data-structures, again using uPickle. Unlike the @hl.scala{Router} object, @hl.scala{Ajaxer} also defines how the out-going Ajax call gets sent over the network. Here we're doing it using the @hl.scala{Ajax.post} method.
+
+ @p
+ Lastly, let's look at the modified callsite for the ajax call itself:
+
+ @hl.ref("examples/crossBuilds/clientserver2/client/src/main/scala/simple/Client.scala", "def update", "")
+
+ @p
+ There are a few things of note here:
+
+ @ul
+ @li
+ The previous call to @hl.scala{Ajax.post} with the path as a string has been replaced by calling @hl.scala{Ajaxer[Api].list(...).call()}, since the logic of actually performing the POST is specified once-and-only-once in the @hl.scala{Ajaxer} object.
+ @li
+ While @hl.scala{Ajax.post} returned a @hl.scala{Future[dom.XMLHttpRequest]} and left us to call @hl.scala{upickle.read} and deserialize the data ourselves, @hl.scala{Ajaxer[Api].list(...).call()} now returns a @hl.scala{Future[Seq[FileData]]}! Thus we don't need to worry about making a mistake in the deserialization logic when we write it by hand.
+
+ @p
+ Other than that, nothing much has changed. If you've done this correctly, the web application will look and behave exactly as it did earlier! So why did we do this in the first place?
+
+ @sect{Why Autowire?}
+ @p
+ Overall, this set up requires some boilerplate to define the @hl.scala{Ajaxer} and @hl.scala{Router} objects, as well as the @hl.scala{Api} trait. However, these can be defined just once and used over and over; while it might be wasteful/unnecessary for making a single Ajax call, the cost is much less amortized over a number of Ajax calls. In a non-trivial web application with dozens of routes being called all over the place, spending a dozen lines setting up things up-front isn't a huge cost.
+
+ @p
+ What have we gotten in exchange? It turns out that by using Autowire, we have eliminated the three failure modes described earlier, that could:
+
+ @ul
+ @li
+ It is impossible for the route and the endpoint method-name to diverge accidentally: if the endpoint is called @hl.scala{list}, the requests will go through the @code{/list} URL. No room for discussion, or to make a mistake
+ @li
+ You cannot accidentally rename the route on the server without changing the client, or vice versa. Attempts to do so will cause a compilation error, and even your IDE should highlight it as red. Try it out!
+
+ @li
+ There is no chance of messing up the serialization/deserialization code, e.g. writing a response of type A on the server and trying to read a data-structure of type B on the client. You have no opportunity to make an error: you pass arguments to the Ajax call, and they are serialized/deserialized automatically, such that by the time you get access to the value on the server, it is already of the correct type! The same applies to serializing/deserializing the return-value on the client. There is simply no place for you as a developer to accidentally make a mistake!
+
+ @p
+ Although the functionality of the web application is the same, it is mostly in terms of @i{safety} that we have made the biggest gains. All of the common failure modes described earlier have been guarded against, and you as a developer will have a hard time trying to make a mal-formed Ajax call. It's worth taking some time to poke at the source code to see the boundaries of the type-safety provided by autowire, as it is a very different experience from the traditional "route it manually" approach to making interactive client-server applications.
+
+@hr
+
+@p
+ Hopefully this chapter has given you a glimpse of how a basic client-server application works using Scala.js. Although it is specific to a Spray server, there isn't any reason why you couldn't set up an equivalent thing for your Play, Scalatra or whichever other web framework that you're using.
+
+@p
+ It's probably worth taking a moment to play around with the existing client-server system you have set up. Ideas for improvement include:
+
+@ul
+ @li
+ Try adding additional functionality to the client-server interface: what about making it show the contents of a file if you've entered its full path? This can be added as a separate Ajax call or as part of the existing one.
+ @li
+ How about setting up the build.sbt so it serves the fully-optimized Scala.js blob, @code{client-opt.js}? This is probably what you want before deployment into production, and the same technique as we used to serve the fast-optimized version applies here too.
+ @li
+ What if you wanted to use another server rather than Spray? How about trying to set up a Play or Scalatra server to serve our Scala.js application code? \ No newline at end of file
diff --git a/book/src/main/scalatex/book/handson/CommandLine.scalatex b/book/src/main/scalatex/book/handson/CommandLine.scalatex
new file mode 100644
index 0000000..0663378
--- /dev/null
+++ b/book/src/main/scalatex/book/handson/CommandLine.scalatex
@@ -0,0 +1,173 @@
+
+@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.5} suffix.
+
+ @hl.bash
+ target/scala-2.11/
+ └── example_sjs0.5_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{<script>} tag and run.
+
+ @p
+ This is the first phase which actually results in an executable blob of Javascript. I won't go into much detail about this command: you've used it before, and more details about the particular kind of optimization and how it fits into the large process is available in the chapter on The Compilation Pipeline. Nonetheless, it's fast, produces not-too-huge output code, and is what you typically use for iterative development in the browser.
+
+ @sect{The fullOptJS Command}
+ @hl.bash
+ > fullOptJS
+ @p
+ @code{fullOptJS} is another command that we've seen before: it performs an aggressive, somewhat slower @sect.ref{Full Optimization} pass on the generated Javascript. This results in a much smaller executable Javascript blob, which you can also load via a @hl.html{<script>} tag and run.
+ @p
+ Again, I won't go into much details, as exactly what this optimization does is described in the chapter on the Compilation Pipeline. This command is somewhat too-slow to be running during iterative development, and is instead typically used just before deployment to minimize the size of the file your users have to download.
+
+ @sect{The run Command}
+ @hl.bash
+ > run
+ @p
+ Here's something you haven't seen before: the @code{run} command gives you the ability to run a Scala.js program from the command line. This prints its output to standard output (i.e. the terminal). Like Scala-JVM, you need a @code{main} method to run to kick off your program. Unlike Scala-JVM, the main method is marked on an @hl.scala{object} which @hl.scala{extends scala.scalajs.js.JSApp}, e.g.
+
+ @hl.scala
+ // src/main/scala/RunMe.scala
+ object RunMe extends scala.scalajs.js.JSApp{
+ def main(): Unit = {
+ println("Hello World!")
+ println("In Scala.js, 1/0 is ${1/0}!")
+ }
+ }
+
+ @p
+ Running @code{sbt run} with the above Scala.js code will print out
+
+ @hl.bash
+ Hello World!
+ In Scala.js, 1/0 is 0!
+
+ @p
+ This exhibits the weirdness of integer divide-by-zero in Scala.js, which is one of the few ways in which @sect.ref("Deviations from Scala-JVM", "Scala.js deviates from Scala-JVM"). This also shows us we're really running on Scala.js: on Scala-JVM, integer divide-by-zero throws an exception rather than returning zero!
+
+ @p
+ One thing you may be wondering is: when you run a Scala.js program in the terminal, how does it execute the output Javascript? What about the DOM? and Ajax calls? Can it access the filesystem? The answer to all these questions is "it depends": it turns out there are multiple ways you can run Scala.js from the command-line:
+
+ @ul
+ @li
+ @b{Rhino} using @code{sbt run}
+ @li
+ @b{Node.js} using @code{sbt fastOptStage::run} or @code{sbt fullOptStage::run}, having installed Node.js separately
+ @li
+ @b{PhantomJS} using @code{sbt fastOptStage::run} or @code{sbt fullOptStage::run}, having installed PhantomJS separately, and turned on @hl.scala{requiresDOM := true} in SBT
+
+ @p
+ Typically, the best way to get started is using Rhino and @code{sbt run}, since it's setup-free, and setting up Node.js or PhantomJS later as necessary. The next two sections elaborate on the differences between these ways of running your code. Check out the later sections on @sect.ref{Headless Runtimes} and @sect.ref{Run Configurations} to learn more about the other settings and why you would want to use them.
+
+ @sect{The test Command}
+ @hl.bash
+ > test
+
+ @p
+ The @code{sbt test} command behaves very similar to @code{sbt run}. It can also be prefixed e.g. @code{sbt fastOptStage::run} or @code{sbt fullOptStage::run}, or run in either Rhino, Node.js or PhantomJS.
+ @p
+ The difference is that instead of simply running your @code{main} method, @code{sbt test} runs whatever test-suite you have set-up, which will look through your @code{tests/} folder to find suites of tests it has to run, and aggregate the results formatted nicely for you to see. The exact operation of this depends on which testing library you're using
+ @p
+ We won't spend much time talking about @code{sbt test} here. Not because it's not important: it most certainly is! Rather, we will be spending a good amount of time setting up tests in @sect.ref("Cross Publishing Libraries", "the next chapter"), so feel free to jump ahead if you want to see an example usage of @code{sbt test}.
+
+@sect{Headless Runtimes}
+ @ul
+ @li
+ @lnk.misc.Rhino is the default way of running Scala.js applications, and occurs when you hit @code{sbt run} from the terminal. The upside of using Rhino is that it is pure-Java, and doesn't need any additional binaries or installation. The downside of using Rhino is that it is slow: maybe a hundred times slower than the alternatives, making it not suitable for running long-running, compute-intensive programs. Furthermore, it's a very sparse runtime environment, with no DOM access or similar.
+ @li
+ @lnk.misc.Nodejs, a relatively new Javascript runtime based on Google's V8 Javascript engine, Node.js lets you run your Scala.js application from the command line much faster than in Rhino, with performance that matches that of modern browsers. However, you need to separately @lnk("install Node.js", "http://nodejs.org/download/") in order to use it. Like Rhino, it comes with a bare-minimal runtime environment, with no DOM or browser-related functionality. You need to run @code{sbt fastOptStage::run} to run using Node.js.
+ @li
+ @lnk.misc.PhantomJS is a headless Webkit browser. This means that unlike Node.js or Rhino, PhantomJS provides you with a full DOM and all its APIs to use in your tests, if you wish to e.g. test interactions with the HTML of the web page. On the other hand, it is somewhat slower than Node.js, though still much faster than Rhino. Like Node.js, it needs to be installed separately. You need to run You need to run @code{sbt fastOptStage::run}, as well as setting the @hl.scala{requiresDOM := true} flag in your SBT configuration, to use PhantomJS.
+ @p
+ These are your three options to run your Scala.js code via the command-line. Generally, it's easiest to get started with Rhino since it's the default and requires no setup, though you may find it worthwhile to setup Node or Phantom if you need additional speed or DOM-integration in your runs.
+
+@sect{Run Configurations}
+ @p
+ You may have noticed that in Rhino we use @code{sbt run}, whereas for Node.js or PhantomJS we use @code{sbt fastOptStage::run}. It turns out that similar to the split between @code{fastOptJS}/@code{fullOptJS}, you have a range of options of how you want to prepare your Scala.js code for execution from the command line:
+
+ @ul
+ @li
+ @code{sbt run}: this does not perform any optimization of the output Scala.js files, and does lazy-loading to minimize the amount of files being loading into the interpreter. This is necessary for Rhino because it can't handle large blobs of Javascript, but doesn't map to any compilation mode you'd use for the browser.
+ @li
+ @code{sbt fastOptStage::run}: this performs the same compilation and optimization as @code{sbt fastOptJS}, as described under Fast Optimizations. It then takes the entire output executable (which probably weighs around 1mb) and hands it to Node.js or PhantomJS, which then run it.
+ @li
+ @code{sbt fullOptStage::run}: this performs the same compilation and optimization as @code{sbt fullOptJS}, as described under Full Optimizations. This takes longer to run than @code{sbt fastOptStage::run}, and results in a smaller/faster executable. Practically speaking, this size/speed advantage does not matter from the command line, but @code{fullOptStage::run} is still useful to verify that the behavior does not change (it shouldn't!) under the aggressive full optimization.
+
+@hr
+
+@p
+ Hopefully by this point you more-or-less know your way around the Scala.js command-line tools. As mentioned earlier, command line tools make it much easier to run a bunch of Scala.js code, e.g. unit tests, without having to muck around with HTML pages or refreshing the browser. That will come in handy soon, as we're next going to learn to publish a standalone, distributable Scala.js module. And what's a module without tests... \ No newline at end of file
diff --git a/book/src/main/scalatex/book/handson/GettingStarted.scalatex b/book/src/main/scalatex/book/handson/GettingStarted.scalatex
new file mode 100644
index 0000000..5d9bf7b
--- /dev/null
+++ b/book/src/main/scalatex/book/handson/GettingStarted.scalatex
@@ -0,0 +1,326 @@
+
+@p
+ To get started with Scala.js, you will need to prepare a few things:
+
+@ul
+ @li
+ @lnk("sbt", "http://www.scala-sbt.org/"): SBT is the most common build-tool in the Scala community, and is what we will use for building our Scala.js application. Their home page has a link to download and install it. (If you are already using Typesafe Activator, that is effectively sbt.)
+ @li
+ An editor for Scala: @lnk("IntelliJ Scala", "http://blog.jetbrains.com/scala/") and @lnk("Eclipse ScalaIDE", "http://scala-ide.org/") are the most popular choices and work on all platforms, though there are others.
+ @li
+ @lnk("Git", "http://git-scm.com/"): This is a version-control system that we will use to download and manage our Scala.js projects.
+ @li
+ A terminal: on OSX you have @lnk("Terminal.app", "http://guides.macrumors.com/Terminal") already installed, in Linux you have @lnk("Terminal", "https://help.ubuntu.com/community/UsingTheTerminal"), and on Windows you have @lnk("PowerShell", "http://en.wikipedia.org/wiki/Windows_PowerShell").
+ @li
+ Your favorite web browser: @lnk("Chrome", "https://www.google.com/chrome") and @lnk("Firefox", "https://www.mozilla.org/en-US/firefox") are the most popular.
+
+@p
+ If you've worked with Scala before, you probably already have most of these installed. Otherwise, take a moment to download them before we get to work.
+
+@p
+ The quickest way to get started with Scala.js is to @code{git clone} @lnk("workbench-example-app", "https://github.com/lihaoyi/workbench-example-app"), go into the repository root, and run @code{~fastOptJS}
+
+@hl.bash
+ git clone https://github.com/lihaoyi/workbench-example-app
+ cd workbench-example-app
+ sbt ~fastOptJS
+
+@p
+ This should result in a bunch of spam to the console, and may take a few minutes the first time as SBT resolves and downloads all necessary dependencies. A successful run looks like this
+
+@pre
+ haoyi-mbp:Workspace haoyi$ git clone https://github.com/lihaoyi/workbench-example-app
+ Cloning into 'workbench-example-app'...
+ remote: Counting objects: 876, done.
+ remote: Total 876 (delta 0), reused 0 (delta 0)
+ Receiving objects: 100% (876/876), 676.59 KiB | 317.00 KiB/s, done.
+ Resolving deltas: 100% (308/308), done.
+ Checking connectivity... done.
+ haoyi-mbp:Workspace haoyi$ cd workbench-example-app/
+ haoyi-mbp:workbench-example-app haoyi$ sbt ~fastOptJS
+ [info] Loading global plugins from /Users/haoyi/.sbt/0.13/plugins
+ [info] Updating {file:/Users/haoyi/.sbt/0.13/plugins/}global-plugins...
+ [info] Resolving org.fusesource.jansi#jansi;1.4 ...
+ [info] Done updating.
+ [info] Loading project definition from /Users/haoyi/Dropbox (Personal)/Workspace/workbench-example-app/project
+ [info] Updating {file:/Users/haoyi/Dropbox%20(Personal)/Workspace/workbench-example-app/project/}workbench-example-app-build...
+ [info] Resolving org.fusesource.jansi#jansi;1.4 ...
+ [info] Done updating.
+ [info] Set current project to Example (in build file:/Users/haoyi/Dropbox%20(Personal)/Workspace/workbench-example-app/)
+ [INFO] [10/26/2014 15:42:09.791] [SystemLol-akka.actor.default-dispatcher-2] [akka://SystemLol/user/IO-HTTP/listener-0] Bound to localhost/127.0.0.1:12345
+ [info] Updating {file:/Users/haoyi/Dropbox%20(Personal)/Workspace/workbench-example-app/}workbench-example-app...
+ [info] Resolving jline#jline;2.12 ...
+ [info] Done updating.
+ [info] Compiling 1 Scala source to /Users/haoyi/Dropbox (Personal)/Workspace/workbench-example-app/target/scala-2.11/classes...
+ [info] Fast optimizing /Users/haoyi/Dropbox (Personal)/Workspace/workbench-example-app/target/scala-2.11/example-fastopt.js
+ [info] workbench: Checking example-fastopt.js
+ [info] workbench: Refreshing http://localhost:12345/target/scala-2.11/example-fastopt.js
+ [success] Total time: 11 s, completed Oct 26, 2014 3:42:21 PM
+ 1. Waiting for source changes... (press enter to interrupt)
+
+@p
+ The line @code{Waiting for source changes...} is telling you that your Scala.js program is ready! Now, when you go to the web URL @code{http://localhost:12345/target/scala-2.11/classes/index-dev.html} in your browser, you should see the following:
+
+ @img(src:="images/Hello World.png", maxWidth:="100%")
+
+@p
+ Congratulations, you just built and ran your first Scala.js application! If something here does not happen as expected, it means that one of the steps did not complete successfully. Make sure you can get this working before you proceed onward.
+
+@sect{Opening up the Project}
+
+ @p
+ The next thing to do once you have the project built and running in your browser is to load it into your editor. Both IntelliJ and Eclipse should let you import the Scala.js project without any hassle. Opening it and navigating to @code{ScalaJSExample.scala} would look like this:
+
+ @img(src:="images/IntelliJ Hello.png", maxWidth:="100%")
+
+ @p
+ Let's try changing one line to change the background fill from black to white:
+
+ @hl.diff
+ - ctx.fillStyle = "black"
+ + ctx.fillStyle = "white"
+
+ @p
+ Because we started @code{sbt ~fastOptJS} with the @code{~} prefix earlier, it should pick up the change and automatically recompile. The example project is set up to automatically refresh the page when recompilation is complete.
+
+ @img(src:="images/Hello World White.png", maxWidth:="100%")
+
+ @p
+ If you open up your browser's developer console, you'll see that the SBT log output is being mirrored there:
+
+ @img(src:="images/Hello World Console.png", maxWidth:="100%")
+
+ @p
+ Apart from the SBT log output (which is handled by Workbench) any @hl.scala{println}s in your Scala.js code will also end up in the browser console (the @code{main} you see in the console is printed inside the Scala.js application, see if you can find it!) and so will the stack traces for any thrown exceptions.
+
+@sect{The Application Code}
+
+ @p
+ We've downloaded, compiled, ran, and made changes to our first Scala.js application. Let's now take a closer look at the code that we just ran:
+
+ @hl.ref("output/workbench-example-app/src/main/scala/example/ScalaJSExample.scala")
+
+ @p
+ It's a good chunk of code, though not a huge amount. To someone who didn't know about Scala.js, they would just think it's normal Scala, albeit with this unusual @hl.scala{dom} library and a few weird annotations. Let's pick it apart starting from the top:
+
+ @hl.ref("output/workbench-example-app/src/main/scala/example/ScalaJSExample.scala", "case class Point", "@JSExport")
+
+ @p
+ Here we are defining a @hl.scala{Point} case class which represents a X/Y position, with some basic operators defined on it. This is done mostly for convenience later on, when we want to manipulate these two-dimensional points. Scala.js is Scala, and supports the entirety of the Scala language. @hl.scala{Point} here behaves identically as it would if you had run Scala on the JVM.
+
+ @hl.ref("output/workbench-example-app/src/main/scala/example/ScalaJSExample.scala", "@JSExport", "val ctx")
+
+ @p
+ This @hl.scala("@JSExport") annotation is used to tell Scala.js that you want this method to be visible and callable from Javascript. By default, Scala.js does @sect.ref("Fast Optimization", "dead code elimination") and removes any methods or classes which are not used. This is done to keep the compiled executables a reasonable size, since most projects use only a small fraction of e.g. the standard library. @hl.scala("@JSExport") is used to tell Scala.js that the @hl.scala{ScalaJSExample} object and its @hl.scala{def main} method are entry points to the program. Even if they aren't called anywhere internally, they are called externally by Javascript that the Scala.js compiler is not aware of, and should not be removed. In this case, we are going to call this method from Javascript to start the Scala.js program.
+
+ @p
+ Apart from this annotation, @hl.scala{ScalaJSExample} is just a normal Scala @hl.scala{object}, and behaves like one in every way. Note that the main-method in this case takes a @lnk.dom.HTMLCanvasElement: your exported methods can have any signature, with arbitrary arity or types for parameters or the return value. This is in contrast to the main method on the JVM which always takes an @hl.scala{Array[String]} and returns @hl.scala{Unit}. In fact, there's nothing special about this method at all! It's like any other exported method, we just happen to attribute it the "main" entry point. It is entirely possible to define multiple exported classes and methods, and build a "library" using Scala.js of methods that are intended for external Javascript to use.
+
+ @hl.ref("output/workbench-example-app/src/main/scala/example/ScalaJSExample.scala", "val ctx", "var count")
+
+ @p
+ Here we are retrieving a handle to the canvas we will draw on using @hl.scala{document.getElementById}, and from it we can get a @lnk.dom.CanvasRenderingContext2D which we actually use to draw on it.
+
+ @p
+ @hl.scala{document.getElementById} is the exact same API that's used in normal Javascript, as documented @lnk("here", "https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById"). In fact, the entire @hl.scala{org.scalajs.dom} namespace (imported at the top of the file) comprises statically typed facades for the javascript APIs provided by the browser.
+
+ @p
+ We need to perform the @hl.scala{asInstanceOf} call because depending on what you pass to @hl.scala{getElementById} and @hl.scala{getContext}, you could be returned elements and contexts of different types. Hence we need to tell the compiler explicitly that we're expecting a @lnk.dom.HTMLCanvasElement and @lnk.dom.CanvasRenderingContext2D back from these methods for the strings we passed in.
+
+ @hl.ref("output/workbench-example-app/src/main/scala/example/ScalaJSExample.scala", "def run", "dom.setInterval")
+
+ @p
+ This is the part of the Scala.js program which does the real work. It runs 10 iterations of a @lnk("small algorithm", "http://en.wikipedia.org/wiki/Sierpinski_triangle#Chaos_game") that generates a Sierpinski Triangle point-by-point. The steps, as described by the linked article, are roughly:
+
+ @ul
+ @li
+ Pick a random corner of the large-triangle
+ @li
+ Move your current-position @hl.scala{p} halfway between its current location and that corner
+ @li
+ Draw a dot
+ @li
+ Repeat
+
+ @p
+ In this example, the triangle is hard-coded to be 255 pixels high by 255 pixels wide, and some math is done to pick a color for each dot which will give the triangle a pretty gradient.
+
+ @hl.ref("output/workbench-example-app/src/main/scala/example/ScalaJSExample.scala", "dom.setInterval")
+
+ @p
+ Now this is the call that actually does the useful work. All this method does is call @hl.scala{dom.setInterval}, which tells the browser to run the @hl.scala{run} method every 50 milliseconds. As mentioned earlier, the @hl.scala{dom.*} methods are simply facades to their native Javascript equivalents, and @hl.scala{dom.setInterval} is @lnk("no different", "https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setInterval"). Note how you can pass a Scala lambda to @hl.scala{setInterval} to have it called by the browser, where in Javascript you'd need to pass a Javascript @hl.javascript{function(){...}}
+
+@sect{The Project Code}
+
+ @p
+ We've already taken a look at the application code for a simple, self-contained Scala.js application, but this application is not @i{entirely} self contained. It's wrapped in a small SBT project that sets up the necessary dependencies and infrastructure for this application to work.
+
+ @sect{project/build.sbt}
+ @hl.ref("output/workbench-example-app/project/build.sbt")
+
+ @p
+ This is the list of SBT plugins used by this small example application. There are two of them: the Scala.js plugin (which contains the Scala.js compiler and other things, e.g. tasks such as @code{fastOptJS}) and the @lnk("Workbench", "https://github.com/lihaoyi/workbench") plugin, which is used to provide the auto-reload-on-change behavior and the forwarding of SBT logspam to the browser console.
+
+ @p
+ Of the two, only the Scala.js plugin is really necessary. The Workbench plugin is a convenience that makes development easier. Without it you'd need to keep a terminal open to view the SBT logspam, and manually refresh the page when compilation finished. Not the end of the world.
+
+
+ @sect{build.sbt}
+ @hl.ref("output/workbench-example-app/build.sbt")
+
+ @p
+ The @code{build.sbt} project file for this application is similarly unremarkable: It includes the settings for the two SBT plugins we saw earlier, as well as boilerplate @hl.scala{name}/@hl.scala{version}/@hl.scala{scalaVersion} values common to all projects.
+
+ @p
+ Of interest is the @hl.scala{libraryDependencies}. In Scala-JVM, this key is used to declare dependencies on libraries from Maven Central, so you can use them in your Scala-JVM projects. In Scala.js, the same key is used to declare dependencies on libraries so you can use them in your Scala.js projects! Re-usable libraries can be built and published with Scala.js just as you do on Scala-JVM, and here we make use of one which provides the typed facades with which we used to access the DOM in the application code.
+
+ @p
+ Lastly, we have two Workbench related settings: @hl.scala{bootSnippet} basically tells Workbench how to restart your application when a new compilation run finishes, and @hl.scala{updateBrowsers} actually tells it to perform this application-restarting.
+
+ @sect{src/main/resources/index-dev.html}
+ @hl.ref("output/workbench-example-app/src/main/resources/index-dev.html")
+
+ @p
+ This is the HTML page which our toy app lives in, and the same page that we have so far been using to view the app in the browser. To anyone who has used HTML, most of it is probably familiar. Things of note are the @hl.html{<script>} tags: @hl.scala{"../example-fastopt.js"} Is the executable blob spat out by the compiler, which we need to include in the HTML page for anything to happen. This is where the results of your compiled Scala code appear. @hl.scala{"workbench.js"} is the client for the Workbench plugin that connects to SBT, reloads the browser and forwards logspam to the browser console.
+
+ @p
+ The @hl.scala{ScalaJSExample().main()} call is what kicks off the Scala.js application and starts its execution. Scala.js follows Scala semantics in that @hl.scala{object}s are evaluated lazily, with no top-level code allowed. This is in contrast to Javascript, where you can include top-level statements and object-literals in your code which execute immediately. In Scala.js, nothing happens when @code{../example-fastopt.js} is imported! We have to call the main-method first. In this case, we're passing the canvas object (attained using @hl.javascript{getElementById}) to it so it knows where to do its thing.
+
+ @p
+ Lastly, only @hl.scala("@JSExport")ed objects and methods can be called from Javascript. Also, although this example only exports the @hl.scala{main} method which is called once, there is nothing stopping you from exporting any number of objects and methods and calling them whenever you need to. In this way, you can easily make a Scala.js "library" which is available to external Javascript as an API.
+
+@sect{Publishing}
+ @p
+ The last thing that we'll do with our toy application is to publish it. If you look in the @code{target/scala2.11} folder, you'll see the output of everything we've done so far:
+
+ @hl.bash
+ target/scala-2.11
+ ├── classes
+ │   ├── JS_DEPENDENCIES
+ │   ├── 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
+ │   ├── index-dev.html
+ │   └── index-opt.html
+ ├── example-fastopt.js
+ └── example-fastopt.js.map
+
+ @p
+ All the @code{.class} and @code{.sjsir} files are the direct output of the Scala.js compiler, and aren't necessary to actually run the application. The only two files necessary are @code{index-dev.html} and @code{example-fastopt.js}. You may recognize @code{index-dev.html} as the file that we were navigating to in the browser earlier.
+
+ @p
+ These two files can be extracted and published as-is: you can put them on @lnk("Github-Pages", "https://pages.github.com/"), @lnk("Amazon Web Services", "http://docs.aws.amazon.com/gettingstarted/latest/swh/website-hosting-intro.html"), or a hundred other places. However, one thing of note is the fact that the generated Javascript file is quite large:
+
+ @hl.bash
+ haoyi-mbp:temp haoyi$ du -h target/scala-2.11/example-fastopt.js
+ 856K target/scala-2.11/example-fastopt.js
+
+ @p
+ 856 Kilobytes for a hello world app! That is clearly too large. If you examine the contents of the file, you'll see that your code has been translated into something like this:
+
+ @hl.javascript
+ var i$2 = i;
+ if (((ScalaJS.m.Lexample_ScalaJSExample().count$1 % 30000) === 0)) {
+ ScalaJS.m.Lexample_ScalaJSExample().clear__V()
+ };
+ ScalaJS.m.Lexample_ScalaJSExample().count$1 = ((ScalaJS.m.Lexample_ScalaJSExample().count$1 + 1) | 0);
+ var jsx$3 = ScalaJS.m.Lexample_ScalaJSExample();
+ var jsx$2 = ScalaJS.m.Lexample_ScalaJSExample().p$1;
+ var jsx$1 = ScalaJS.m.Lexample_ScalaJSExample().corners$1;
+ var this$5 = ScalaJS.m.s_util_Random();
+ jsx$3.p$1 = jsx$2.$$plus__Lexample_Point__Lexample_Point(ScalaJS.as.Lexample_Point(jsx$1.apply__I__O(this$5.self$1.nextInt__I__I(3)))).$$div__I__Lexample_Point(2);
+ var height = (512.0 / ((255 + ScalaJS.m.Lexample_ScalaJSExample().p$1.y$1) | 0));
+ var r = ((ScalaJS.m.Lexample_ScalaJSExample().p$1.x$1 * height) | 0);
+ var g = ((((255 - ScalaJS.m.Lexample_ScalaJSExample().p$1.x$1) | 0) * height) | 0);
+
+ @p
+ As you can see, this code is still very verbose, with lots of unnecessarily long identifiers such as @hl.javascript{Lexample_ScalaJSExample} in it. This is because we've only performed the @sect.ref{Fast Optimization} on this file, to try and keep the time taken to edit -> compile while developing reasonably short.
+
+ @sect{Optimization}
+ @p
+ If we're planning on publishing the app for real, we can run the @sect.ref{Full Optimization}. This takes several seconds longer than the @sect.ref{Fast Optimization}, but results in a significantly smaller and leaner output file @code{example-opt.js}.
+
+ @hl.bash
+ haoyi-mbp:temp haoyi$ du -h target/scala-2.11/example-opt.js
+ 144K target/scala-2.11/example-opt.js
+
+ @p
+ 144 Kilobytes! Better. Not great, though! In general, Scala.js does not produce tiny executables, although the output size of the compiled executables is dropping all the time. If you look inside that file, you'll see all of the long identifiers have been replaced by short ones by the @lnk("Google Closure Compiler", "https://developers.google.com/closure/compiler/").
+
+ @hl.javascript("""
+ Rb(P(),N(r(L(Ea),["rgb(",", ",", ",")"])))),Sb(P(),r(L(K),[n,s,C])));fe().te.fillRect(fe().Oc.jc,fe().Oc.kc,1,1);e=e+1|0;c=c+k|0}},50))}ae.prototype.Zf=function(){this.te.fillStyle="black";this.te.fillRect(0,0,255,255)};ae.prototype.main=function(){return ee(),void 0};ae.prototype.a=new I({lj:0},!1,"example.ScalaJSExample$",K,{lj:1,b:1});var be=void 0;function fe(){be||(be=(new ae).c());return be}ba.ScalaJSExample=fe;function le(){}le.prototype=new J;function me(){}me.prototype=le.prototype;
+ """)
+
+ @p
+ These files are basically unreadable, but nonetheless behave the same as the @code{-fastopt} versions. Try it out by opening the @code{index-opt.html} file in the @code{target/scala-2.11/classes} directory with your browser: you should see the thing as when opening @code{index-dev}, except it will be pulling in the fully-optmized version of your application.
+
+ @p
+ This means you can develop and debug using @code{fastOptJS}, and only spend the extra time (and increased debugging-difficulty) on the @code{fullOptJS} version just as you're going to publish it, with the assurance that although the code is much more compact, its behavior will not change.
+
+ @sect{Blob Size}
+ @p
+ Even the fully-optimized version of our toy Scala.js app are pretty large. There are some factors that mitigate the large size of these executables:
+
+ @ul
+ @li
+ A large portion of this 144k is the Scala standard library, and so the size of the compiled blob does not grow that fast as your program grows. For example, while this ~50 line application is 144k, a @lnk("much larger ~2000 line application", "https://github.com/lihaoyi/roll") is only 288k.
+ @li
+ This size is pre-@lnk("gzip", "http://en.wikipedia.org/wiki/Gzip"), and most webservers serve their contents compressed via gzip to reduce the download size. Gzip cuts the actual download size down to 43k, which is more acceptable.
+ @li
+ You will likely have other portions of the page that are of similar size: e.g. @lnk("JQuery", "http://jquery.com/") is extremely popular, and weights in at a comparable 32kb minified and gzipped, while @lnk("React.js", "http://facebook.github.io/react/downloads.html") weighs in at a cool 150kb gzipped. Scala.js arguably provides more than either of these libraries.
+
+ @p
+ Regardless, there is ongoing work to shrink the size of these executables. If you want to read more about this, check out the section on @sect.ref("The Compilation Pipeline") to learn about what we currently do to crunch the executables down.
+
+ @hr
+
+ @p
+ In general, all the output of the Scala.js compiler is bundled up into the @code{example-fastopt.js} and @code{example-opt.js} files. As a first approximation, these files can be included directly on a HTML page (as we have here, with @code{index-dev.html} and @code{index-opt.html}) and published together as a working web app. Even zipping them up and emailing them to a friend is sufficient to give someone a working, live version of your hard work!
+
+ @p
+ More advanced users would want to integrate them into their build process or serve them from a web server, all of which is entirely possible. You just need to run the Scala.js compiler and place the output @code{.js} file somewhere your web server can pick it up, e.g. in some static-resource folder. We cover an example setup of this with a Scala webserver in our chapter @sect.ref("Integrating Client-Server").
+
+@sect{Recap}
+
+ @p
+ If you've made it this far, you've downloaded, made modifications to, and published a toy Scala.js application. At the same time, we've gone over many of the key concepts in the Scala.js development process:
+
+ @ul
+ @li
+ Getting a Scala.js application
+ @li
+ Building it and seeing it work in the browser
+ @li
+ Made modifications to it to see it update
+ @li
+ Examined the source code to try and understand what it's doing
+ @li
+ Examined the output code, at two levels of optimization, to see how the Scala.js compiler works
+ @li
+ Packaged the application in a form that can be easily published online
+
+ @p
+ Hopefully this gives a good sense of the workflow involved in developing a Scala.js application end-to-end, as well as a feel for the magic involved in the compilation process. Nevertheless, we have barely written any Scala.js code itself!
+
+ @p
+ Since you have a working development environment set up, you should take this time to poke around what you can do with our small Sierpinski-Triangle drawing app. Possible exercises include:
+
+ @ul
+ @li
+ Javascript includes APIs for @lnk("getting the size of the window", "http://stackoverflow.com/questions/1248081/get-the-browser-viewport-dimensions-with-javascript") and @lnk("changing the size of a canvas", "http://stackoverflow.com/questions/9251480/set-canvas-size-using-javascript"). These Javascript APIs are available in Scala.js, and we've already used some of them in the course of this example. Try making the Canvas full-screen, and re-positioning the corners of the triangle to match.
+ @li
+ The @lnk.dom.CanvasRenderingContext2D has a bunch of methods on it that can be used to draw things. Here we only draw 1x1 rectangles to put points on the canvas; try modifying the code to make it draw something else.
+ @li
+ We've look at the @code{master} branch of @code{workbench-example-app}, but this project also has several other branches showing off different facets of Scala.js: @lnk("dodge-the-dots", "https://github.com/lihaoyi/workbench-example-app/tree/dodge-the-dots") and @lnk("space-invaders", "https://github.com/lihaoyi/workbench-example-app/tree/space-invaders") are both interesting branches worth playing with as a beginner. Check them out!
+ @li
+ Try publishing the output code somewhere. You only need @code{example-opt.js} and @code{index-opt.html}; try putting them somewhere online where the world can see it.
+
+ @p
+ When you're done poking around our toy web application, read on to the next chapter, where we will explore making something more meaty using the Scala.js toolchain!
diff --git a/book/src/main/scalatex/book/handson/PublishingModules.scalatex b/book/src/main/scalatex/book/handson/PublishingModules.scalatex
new file mode 100644
index 0000000..9e742e2
--- /dev/null
+++ b/book/src/main/scalatex/book/handson/PublishingModules.scalatex
@@ -0,0 +1,197 @@
+@import BookData._
+@p
+ We've spent several chapters exploring the experience of making web apps using Scala.js, but any large application (web or not!) likely relies on a host of libraries in order to implement large chunks of its functionality. Ideally these libraries would be re-usable, and can be shared among different projects, teams or even companies.
+
+@p
+ Not all code is developed in the browser. Maybe you want to run simple snippets of Scala.js which don't interact with the browser at all, and having to keep a browser open is an overkill. Maybe you want to write unit tests for your browser-destined code, so you can verify that it works without firing up Chrome. Maybe it's not a simple script but a re-distributable library, and you want to run the same command-line unit tests on both Scala.js and Scala-JVM to verify that the behavior is identical. This chapter will go through all these cases.
+
+
+@sect{A Simple Cross-Built Module}
+
+ @p
+ As always, we will start with an example: in this case a toy library whose sole purpose in life is to take a series of timestamps (milliseconds UTC) and format them into a single, newline-delimited string. This is what the project layout looks like:
+
+ @hl.bash
+ $ tree
+ .
+ ├── build.sbt
+ ├── js
+ │   ├── shared/main/scala/simple/Simple.scala
+ │   └── src/main/scala/simple/Platform.scala
+ ├── jvm
+ │   ├── shared -> ../js/shared
+ │   └── src/main/scala/simple/Platform.scala
+ └── project/ build.sbt
+ @p
+ In this case the two @code{shared/} folders are symlinked together to keep them in sync. This can be done by first creating @code{js/shared}, and then running
+
+ @hl.bash
+ $ ln -s ../js/shared jvm/shared
+
+ @sect{Build Configuration}
+ @p
+ From the bash shell in the project root. Let's take a look at the various files that make up this project. First, the @code{build.sbt} files:
+
+ @hl.ref("examples/crossBuilds/simple/project/build.sbt")
+
+ @p
+ The @code{project/build.sbt} file is uneventful: it simply includes the Scala.js SBT plugin. However, the @code{build.sbt} file is a bit more interesting:
+
+ @hl.ref("examples/crossBuilds/simple/build.sbt")
+
+ @p
+ Unlike the equivalent @code{build.sbt} files you saw in earlier chapters, this does not simply add @hl.scala{scalaJSSettings} to the root project. Rather, it sets up two projects: one in the @code{js/} folder and one in the @code{jvm/} folder, with the @code{js/} version getting the settings from the Scala.js plugin. To both of these, we add @code{shared/main/scala} to the list of source directories. This means that both projects will pick up the sources we symlinked between @code{js/shared/} and @code{jvm/shared/}.
+
+ @sect{Source Files}
+
+ @p
+ Now, let's look at the contents of the @code{.scala} files that make up the meat of this project:
+
+ @hl.ref("examples/crossBuilds/simple/js/shared/main/scala/simple/Simple.scala")
+
+ @p
+ In @code{Simple.scala} we have the shared, cross-platform API of our library: a single @hl.scala{object} with a single method @hl.scala{def} which does what we want, which can then be used in either Scala.js or Scala-JVM. In general, you can put as much shared logic here as you want: classes, objects, methods, anything that can run on both Javascript and on the JVM. We're chopping off the last 5 characters (the milliseconds) to keep the formatted dates slightly less verbose.
+
+ @p
+ However, when it comes to actually formatting the date, we have a problem: Javascript and Java provide different utilities for formatting dates! They both let you format them, but they provide different APIs. Thus, to do the formatting of each individual date, we call out to the @hl.scala{Platform.format} function, which is implemented twice: once in @code{js/} and once in @code{jvm/}:
+
+ @split
+ @half
+ @hl.ref("examples/crossBuilds/simple/js/src/main/scala/simple/Platform.scala")
+
+ @half
+ @hl.ref("examples/crossBuilds/simple/jvm/src/main/scala/simple/Platform.scala")
+
+ @p
+ In the @code{js/} version, we are using the Javascript @hl.javascript{Date} object to take the millis and do what we want. In the @code{jvm/} version, we instead use @hl.scala{java.text.SimpleDateFormat} with a custom formatter (The syntax is defined @lnk("here", "http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html")).
+
+ @p
+ Again, you can put as much platform-specific logic in these files as you want, to account for differences in the available APIs. Maybe you want to use @lnk.dom.JSONparse for parsing JSON blobs in @code{js/}, but @lnk("Jackson", "http://jackson.codehaus.org/") or @lnk("GSON", "https://code.google.com/p/google-gson/") for parsing them in @code{jvm/}.
+ @p
+ Lastly, you'll also have noticed the two identical @hl.scala{main} methods in the platform-specific code. This is an implementation detail around the fact that Scala.js picks up the main method differently from Scala-JVM, using @hl.scala{js.JSApp} instead of looking for a @hl.scala{main(args: Array[String]): Unit} method. These two main methods allow us to test our implementations
+ @sect{Running the Module}
+ @hl.bash
+ > ; js/run; jvm/run
+ [info] Running simple.Platform
+ Running on JS! 1
+ November 2, 2014 2:58:48 PM PST
+ November 2, 2014 2:58:49 PM PST
+ [success] Total time: 4 s, completed Nov 2, 2014 2:58:48 PM
+ [info] Running simple.Platform
+ Running on JVM! 1.0
+ November 2, 2014 2:58:49 PM PST
+ November 2, 2014 2:58:50 PM PST
+ [success] Total time: 0 s, completed Nov 2, 2014 2:58:49 PM
+
+ @p
+ As you can see, both runs printed the same results, modulo three things:
+
+ @ul
+ @li
+ The "Running on XXX!" statement which shows us we're actually running on two platforms.
+ @li
+ The other hint is the time taken: the second run is instant while the first takes three seconds! This is because by default we run on @lnk.misc.Rhino, which is a simple interpreter hundreds of times slower than running code natively on the JVM.
+ @li
+ In Scala-JVM the double 1.0 is printed as @code{1.0}, while in Scala.js it's printed as @code{1}. This is one of a small number of differences between Scala.js and Scala-JVM, and verifies that we are indeed running on both platforms!
+
+ @p
+ You've by this point set up a basic cross-building Scala.js/Scala-JVM project!
+
+ @hr
+
+ @p
+ If you wish, you can do more things with this project you've set up:
+
+ @ul
+ @li
+ Flesh it out! Currently this module only does a single, trivial thing. If you've done any web development before, I'm sure you can find some code snippet, function or algorithm that you'd like to share between client and server. Try implementing it in the @code{shared/} folder to be usable in both Scala.js and Scala-JVM
+ @li
+ Publish it! Both @code{sbt publishLocal} and @code{sbt publishSigned} work on this module, for publishing either locally, Maven Central via Sonatype, or Bintray. Running the command bare should be sufficient to publish both the @code{js} or @code{jvm} projects, or you can also specify which one e.g. @code{jvm/publishLocal} to publish only one subproject.
+
+ @p
+ This @code{jvm} project works identically to any other Scala-JVM project, and the @code{js} project works identically to the Command Line API described earlier. Thus you can do things like @code{fastOptStage::run} to run the code on Node.js, setting @hl.scala{requiresDOM := true}, run @code{fullOptStage::run} to run the code with full, aggressive optimizations. And of course, things that work in both Scala.js and Scala-JVM can be run on both, basic commands such as @code{run} or @code{test}.
+
+ @p
+ You can also run tests using this code, if you have a testing library set up. The next section will go into detail as to how to set that up.
+
+@sect{Cross-Testing with uTest}
+ @p
+ @lnk("uTest", "https://github.com/lihaoyi.utest") is a small unit-testing library for Scala programs, that works on both Scala-JVM and Scala.js. At the time it was written, it was the first one out there, though now there are others such as @lnk("little-spec", "https://github.com/eecolor/little-spec") or @lnk("otest", "https://github.com/cgta/otest"). Notably, Scala's traditional testing libraries such as @lnk("Scalatest", "http://www.scalatest.org/") or @lnk("Specs2", "http://etorreborre.github.io/specs2/") do not work with Scala.js, as they make use of Reflection or other things not supported on Scala.js
+
+ @sect{uTest Configuration}
+ @p
+ To make your code use uTest, there are a few changes you need to make. First, you need to add the uTest SBT plugin:
+
+ @hl.ref("examples/crossBuilds/simple2/project/build.sbt")
+
+ @p
+ Here, in @code{project/build.sbt}, we see it used next to the Scala.js SBT plugin. Next, we need to modify our @code{build.sbt} file
+
+ @hl.ref("examples/crossBuilds/simple2/build.sbt")
+
+ @p
+ The main thing we've done is make use of uTest's @hl.scala{JsCrossBuild}: this does the work we've previously done to setup the shared-source-directory in SBT, as well as doing the neccessary configuration for uTest itself, providing you with ready-made @hl.scala{js} and @hl.scala{jvm} projects you can work with.
+
+ @sect{Your First Tests!}
+ @p
+ Lastly, we need to start writing tests! @lnk("uTest", "https://github.com/lihaoyi.utest") is well documented, but to get started here's a simple test suite for our @hl.scala{formatDates} function:
+
+ @hl.ref("examples/crossBuilds/simple2/js/shared/test/scala/simple/SimpleTest.scala")
+
+ @p
+ Since this is in @code{shared/}, it is automatically symlinked and is picked up by both @code{js} and @code{jvm} subprojects. With that done, you just need to run the @code{test} commands:
+
+ @hl.bash
+ > ; js/test; jvm/test
+ [info] 1/4 simple.SimpleTest.format.nil Success
+ [info] 2/4 simple.SimpleTest.format.timeZero Success
+ [info] 3/4 simple.SimpleTest.format Success
+ [info] 4/4 simple.SimpleTest Success
+ [info] -----------------------------------Results-----------------------------------
+ [info] simple.SimpleTest Success
+ [info] format Success
+ [info] nil Success
+ [info] timeZero Success
+ [info] Failures:
+ [info]
+ [info] Tests: 4
+ [info] Passed: 4
+ [info] Failed: 0
+ [success] Total time: 4 s, completed Nov 8, 2014 7:42:39 PM
+ [info] 1/4 simple.SimpleTest.format.nil Success
+ [info] 2/4 simple.SimpleTest.format.timeZero Success
+ [info] 3/4 simple.SimpleTest.format Success
+ [info] 4/4 simple.SimpleTest Success
+ [info] -----------------------------------Results-----------------------------------
+ [info] simple.SimpleTest Success
+ [info] format Success
+ [info] nil Success
+ [info] timeZero Success
+ [info] Failures:
+ [info]
+ [info] Tests: 4
+ [info] Passed: 4
+ [info] Failed: 0
+ [success] Total time: 0 s, completed Nov 8, 2014 7:42:39 PM
+
+ @p
+ And you'll see that we've run our unit tests twice: once on Scala.js in Rhino, and once on Scala-JVM! As expected, the first run in Rhino took much longer (4 seconds!) than the second, as Rhino is much slower than running code directly on the JVM. You can configure the Scala.js run to run in Node.js or PhantomJS, as well as running under different optimization levels.
+
+ @hr
+
+ @p
+ Now that you've got a basic cross-platform Scala module building and testing, what next? One thing you may want to do is add things to the project. Depending on where you want your code to run, there's a place for everything:
+
+ @ul
+ @li
+ @code{js/shared/main/scala}/@code{jvm/shared/main/scala} is where your shared library code goes. This code will be run on both Scala.js and Scala-JVM
+ @li
+ @code{jvm/src/main/scala} Scala-JVM only code
+ @li
+ @code{js/src/main/scala} Scala.js only code
+
+ @p
+ It is entirely possible for your modules to have slightly different implementations and APIs on Scala.js and Scala-JVM. @lnk.github.Scalatags exposes additional DOM-related functionality only for it's Scala.js version, while @lnk.github.uPickle uses different JSON libraries (@lnk("Jawn", "https://github.com/non/jawn") v.s. @lnk("DOM", "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse")) on the backend while the exposed interface remains the same. You have the flexibility to pick and choose which bits of your library you wish to share and which bits will be different.
+
+ @p
+ Everything above also applies to your unit tests, which fall in @code{test/} folders mirroring the @code{main/} folders listed above. You can also choose to share or not-share your unit test code as you see fit. \ No newline at end of file
diff --git a/book/src/main/scalatex/book/handson/WebPage.scalatex b/book/src/main/scalatex/book/handson/WebPage.scalatex
new file mode 100644
index 0000000..1056460
--- /dev/null
+++ b/book/src/main/scalatex/book/handson/WebPage.scalatex
@@ -0,0 +1,267 @@
+@import BookData._
+
+@p
+ Most web applications aren't neat little games which live on a single canvas: they are large, structured HTML pages, which involve displaying data (whether from the user or from the web) in multiple ways, while allowing the user to make changes to the data that can be saved back to whatever remote web-service/database it came from.
+
+@p
+ At this point, you are already competent at using Scala.js to make basic, self-contained canvas applications. In this chapter, we will cover how to use Scala.js to build the sort of interactive-web-pages that make up the bulk of the modern-day internet. We'll cover how to use powerful libraries that turn front-end development form the typical fragile-mess into a structured, robust piece of software.
+
+@sect{Hello World: HTML}
+
+ @p
+ The most basic way of building interactive web pages using Scala.js is to use the Javascript APIs to blat HTML strings directly into some container @hl.html{<div>} or @hl.html{<body>}. This approach works, as the following code snippet demonstrates:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/HelloWorld0.scala")
+
+ @less
+ @BookData.example(div, "HelloWorld0().main")
+
+ @p
+ Remember that we're now requiring a @hl.scala{dom.HTMLDivElement} instead of a @hl.scala{dom.HTMLCanvsElement} to be passed in when the Javascript calls @hl.javascript{HelloWorld0().main(...)}. If you're coming to this point from the previous chapter, you'll need to update the on-page Javascript's @hl.javascript{document.getElementById} to pick a @hl.html{<div>} rather than the @hl.html{<canvas>} we were using in the previous chapter.
+
+ @p
+ This approach works, as the above example shows, but has a couple of disadvantages:
+
+ @ul
+ @li
+ It is untyped: it is easy to accidentally mistype something, and result in malformed HTML. A typo such as @hl.html{<dvi>} would go un-noticed at build-time. Depending on where the typo happens, it could go un-noticed until the application is deployed, causing subtle bugs that only get resolved much later.
+ @li
+ It is insecure: @lnk("Cross-site Scripting", "http://en.wikipedia.org/wiki/Cross-site_scripting") is a real thing, and it is easy to forget to escape the values you are putting into your HTML strings. Above they're constants like @hl.scala{"dog"}, but if they're user-defined, you may not notice there is a problem until something like @hl.scala{"<script>...</script>"} sneaks through and your users' accounts & data is compromised.
+
+ @p
+ There are more, but we won't go deep into the intricacies of these problems. Suffice to say it makes mistakes easy to make and hard to catch, and we have something better...
+
+@sect{Scalatags}
+ @p
+ @lnk("Scalatags", "https://github.com/lihaoyi/scalatags") is a cross-platform Scala.js/Scala-JVM library that is designed to generate HTML. To use Scalatags, you need to add it as a dependency to your Scala.js SBT project, in the @code{build.sbt} file:
+
+ @hl.ref("examples/demos/build.sbt", "com.scalatags", "")
+
+ @p
+ With that, the above snippet of code re-written using Scalatags looks as follows:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/HelloWorld1.scala")
+
+ @less
+ @BookData.example(div, "HelloWorld1().main")
+
+ @p
+ Scalatags has some nice advantages over plain HTML: it's type-safe, so typos like @hl.scala{dvi} get caught at compile-time. It's also secure, such that you don't need to worry about script-tags in strings or similar. The @lnk("Scalatags Readme", "https://github.com/lihaoyi/scalatags#scalatags") elaborates on these points and other advantages. As you can see, it takes just 1 import at the top of the file to bring it in scope, and then you can use all of Scalatags' functionality.
+
+ @p
+ The Scalatags github page has @lnk("comprehensive documentation", "https://github.com/lihaoyi/scalatags#hello-world") on how to express all manner of HTML fragments using Scalatags, so anyone who's familiar with how HTML works can quickly get up to speed. Instead of a detailed listing, we'll walk through some interactive examples to show Scalatags in action!
+
+ @sect{User Input}
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Inputs.scala", "val box")
+
+ @less
+ @BookData.example(div(height:="150px"), "Inputs().main")
+
+ @p
+ In Scalatags, you build up fragments of type @hl.scala{Frag} using functions like @hl.scala{div}, @hl.scala{h1}, etc., and call @hl.scala{.render} on it to turn it into a real @lnk.dom.Element. Different fragments render to different things: e.g. @hl.scala{input.render} gives you a @lnk.dom.HTMLInputElement, @hl.scala{span.render} gives you a @lnk.dom.HTMLSpanElement. You can then access the properties of these elements: adding callbacks, checking their value, anything you want.
+
+ @p
+ In this example, we render and @hl.scala{input} element and a @hl.scala{span}, wire up the input to set the value of the span whenever you press a key in the input, and then stuff both of them into a larger HTML fragment that forms the contents of our @hl.scala{target} element.
+
+ @sect{Re-rendering}
+ @p
+ Let's look at a slightly longer example. While above we spliced small snippets of text into the DOM, here we are going to re-render entire sections of HTML! The goal of this little exercise is to make a filtering search-box: starting from a default list of items, narrow it down as the user enters text into the box.
+
+ @p
+ To begin with, let's define our list of items: Fruits!
+
+ @hl.ref("examples/demos/src/main/scala/webpage/Search0.scala", "val listings", "def")
+
+ @p
+ Next, let's think about how we want to render these fruits. One natural way would be as a list, which in HTML is represented by a @hl.html{<ul>} with @hl.html{<li>}s inside of it if we wanted the list to be unordered. We'll make it a @hl.scala{def}, because we know up-front we're going to need to re-render this listing as the search query changes. Lastly, we know we want 1 list item for each fruit, but only if the fruit starts with the search query.
+
+ @hl.ref("examples/demos/src/main/scala/webpage/Search0.scala", "def renderListings", "lazy val")
+
+ @p
+ Using a @hl.scala{for}-loop with a filter inside the Scalatags fragment is just normal Scala, since you can nest arbitrary Scala expressions inside a Scalatags snippet. In this case, we're converting both the fruit and the search query to lower case so we can compare them case-insensitively.
+
+ @p
+ Lastly, we just need to define the input box and output-container (as we did earlier), set the @lnk.dom.onkeyup event handler, and place it in a larger fragment, and then into our target:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Search0.scala", "val output")
+
+ @less
+ @BookData.example(div, "Search0().main")
+
+ @p
+ And there you have it! A working search box. This is a relatively self-contained example: all the items its searching are available locally, no Ajax calls, and there's no fancy handling of the searched items. If we want to, for example, highlght the matched section of each fruit's name, we can modify the @hl.scala{def renderListings} call to do so:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Search1.scala", "def renderListings", "lazy val")
+
+ @less
+ @BookData.example(div, "Search1().main")
+
+ @p
+ Here, instead of sticking the name of the matched fruits directly into the @hl.scala{li}, we instead first split off the part which matches the query, and then highlght the first section yellow. Easy!
+
+ @hr
+
+ @p
+ Hopefully this has given you a good overview of how to do things using Scala.js and Scalatags. I won't go too deep into the various ways you can use Scalatags: the @lnk("documentation", "https://github.com/lihaoyi/scalatags") should cover most of it. Now that you've gone through this experience, it's worth re-iterating a few things you've probably already noticed about Scalatags
+
+ @ul
+ @li
+ It's safe! If you make a trivial syntactic mistake, the compiler will catch it, because Scalatags is plain Scala. Try it!
+ @li
+ It's composable! You can easily define fragments and assign them to variables, to be used later. You can break apart large Scalatags fragments the same way you break apart normal code, avoiding the huge monolithic HTML templates that are common in other templating systems.
+ @li
+ It's Scala! You have the full power of the Scala language to write your fragments. No need to learn special syntax/cases for conditionals or repetitions: you can use plain-old-Scala @hl.scala{if}-@hl.scala{else}s, @hl.scala{for}-loops, etc.
+
+ @p
+ Now that you've gotten a quick overview of the kinds of things you can do with Scalatags, let's move on to the next section of our hands-on tutorial...
+
+@sect{Using Web Services}
+
+ @p
+ One half of the web application faces forwards towards the user, managing and rendering HTML or Canvas for the user to view and interact with. Another half faces backwards, talking to various web-services or databases which turn the application from a standalone-widget into part of a greater whole. We've already seen how to make the front half, let's now talk about working with the back half.
+
+ @sect{Raw Javascript}
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Weather0.scala", "val xhr")
+
+ @less
+ @BookData.example(div, "Weather0().main")
+ @p
+ The above snippet of code uses the raw Javascript Ajax API in order to make a request to @lnk("openweathermap.org", "http://openweathermap.org/"), to get the weather data for the city of Singapore as a JSON blob. The part of the API that we'll be using is documented @lnk("here", "http://openweathermap.org/current"), and if you're interested you can read all about the various options that they provide. For now, we're unceremoniously dumping it in a @hl.scala{pre} so you can see the raw response data.
+
+ @p
+ As you can see, using the raw Javascript API to make the Ajax call looks almost identical to actually doing this in Javascript, shown below:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/resources/webpage/weather.js", "var xhr")
+
+ @less
+ @BookData.example(div, "WeatherJs")
+
+ @p
+ The primary syntactic differences are:
+
+ @ul
+ @li
+ @hl.scala{val}s for immutable data v.s. mutable @hl.javascript{var}s.
+ @li
+ @hl.scala("=>") v.s. @hl.javascript{function} to define the callback.
+ @li
+ Scalatags' @hl.scala{pre} v.s. @hl.javascript{document.createElement("pre")}
+
+ @p
+ Overall, they're pretty close, which is a common theme in Scala.js: using Javascript APIs in Scala.js is often as seamless and easy as using them in Javascript itself, and it often looks almost identical.
+
+ @sect{dom.extensions}
+ @p
+ Although the Javascript XMLHttpRequest API is workable, it's kind of awkward and clunky compared to what you're used to in Scala. We create a half-baked object, set some magic properties, and call a magic function, which all has to be done in the correct order or it won't work.
+
+ @p
+ With Scala.js, we provide a simpler API that is more clearly functional. First, you need to import some things into scope:
+
+ @hl.ref("examples/demos/src/main/scala/webpage/Weather1.scala", "import dom", "val url =")
+
+ @p
+ The first import brings in Scala adapters to several DOM APIs, which allow you to use them more idiomatically from Scala. The second brings in an implicit @hl.scala{scala.concurrent.ExecutionContext} that we'll need to run our asynchronous operations.
+
+ @p
+ Then we need the code itself:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Weather1.scala", "val url")
+
+ @less
+ @BookData.example(div(height:="100%", overflow:="scroll"), "Weather1().main")
+
+ @p
+ A single call to @hl.scala{Ajax.get(...)}, with the URL, and we receive a @hl.scala{scala.concurrent.Future} that we can use to get access to the result when ready. Here we're just using it's @hl.scala{onSuccess}, but we could use it in a for-comprehension, with @lnk("Scala Async", "https://github.com/scala/async"), or however else we can use normal @hl.scala{Future}s
+
+ @sect{Parsing the Data}
+ @p
+ We've taken the data-dump from OpenWeatherMap in three different ways, but there's still something missing: we need to actually parse the JSON data to make use of it! Most people don't use their JSON data as strings but as structured documents, querying and extracting only the bits we need.
+
+ @p
+ First, let's make the call prettyprint the document, so at least we can see what it contains:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Weather2.scala", "Ajax.get")
+
+ @less
+ @BookData.example(div(height:="100%", overflow:="scroll"), "Weather1().main")
+
+ @p
+ We do this by taking @hl.scala{xhr.responseText} and putting it through both @hl.scala{JSON.parse} and @hl.scala{JSON.stringify}, passing in a @hl.scala{space} argument to tell @hl.scala{JSON.stringify} to spread it out nicely.
+
+ @p
+ Now that we've pretty-printed it, we can immediately see what data it contains and which part of the data we want. Let's change the previous example's @hl.scala{onSuccess} call to extract the @hl.scala{weather}, @hl.scala{temp} and @hl.scala{humidity} and put them in a nice, human-friendly format for us to enjoy:
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/webpage/Weather3.scala", "Ajax.get")
+
+ @less
+ @BookData.example(div(height:="100%", overflow:="scroll"), "Weather3().main")
+
+ @p
+ First we parse the incoming response, extract a bunch of values from it, and then stick it in a Scalatags fragment for us to see. Note how we can use the names of the attributes e.g. @hl.scala{json.name} even though @hl.scala{name} is a dynamic property which you can't be sure exists: this is because @hl.scala{json} is of type @hl.scala{js.Dynamic}, which allows us to refer to arbitrary parameters and methods on the underlying object without type-checking.
+
+ @p
+ Calls on @hl.scala{js.Dynamic} resolve directly to javascript property/method references, and will fail at run-time with an exception if used wrongly. This is also why we need to call @hl.scala{.toString} or @hl.scala{.asInstanceOf}on the values before use: without these casts, the compiler can't be sure what kind of value is underneath the @hl.scala{js.Dynamic} type, and so we have to provide it the guarantee that it is what it needs.
+
+@sect{Tying it together: Weather Search}
+ @p
+ At this point we've made a small app that allows us to search from a pre-populated list of words, as well as a small app that lets us query a remote web-service to find the weather in Singapore. The natural thing to do is to put these things together to make a app that will let us search from a list of countries and query the weather in any country we desire. Let's start!
+
+ @hl.ref("examples/demos/src/main/scala/webpage/WeatherSearch.scala", "lazy val box", "def fetchWeather")
+
+ @p
+ This sets up the basics: an input box, an output div, and sets an @hl.scala{onkeyup} that fetches the weather data each time you hit a key. It then renders all these components and sticks them into the @hl.scala{target} div. This is basically the same stuff we saw in the early examples, with minor tweaks e.g. adding a @hl.scala{maxHeight} and @hl.scala{overflowY:="scroll"} to the @hl.scala{output} box in case the output is too large. Whenever we enter something in the box, we call the function @hl.scala{fetchWeather}, which is defined as:
+
+ @hl.ref("examples/demos/src/main/scala/webpage/WeatherSearch.scala", "def fetchWeather", "def showResults")
+
+ @p
+ This is where the actual data fetching happens. It's relatively straightforward: we make an @hl.scala{Ajax.get} request, @hl.scala{JSON.parse} the response, and feed it into the callback function. We're using a slightly different API from earlier: we now have the @hl.scala{"type=like"} flag, which is documented in the @lnk("OpenWeatherMap API docs", "http://openweathermap.org/current#other") to return multiple results for each city whose name matches your query.
+
+ @p
+ Notably, before we re-render the results, we check whether the @hl.scala{query} that was passed in is the same value that's in the @hl.scala{box}. This is to prevent a particularly slow ajax call from finishing out-of-order, potentially stomping over the results of more recent searches. We also check whether the @hl.scala{.list: js.Dynamic} property we want is an instance of @hl.scala{js.Array}: if it isn't, it means we don't have any results to show, and we can skip the whole render-output step.
+
+ @hl.ref("examples/demos/src/main/scala/webpage/WeatherSearch.scala", "def showResults")
+
+ @p
+ Here is the meat and potatoes of this program: every time it gets called with an array of weather-data, we iterate over the cities in that array. It then does a similar sort of data-extraction that we did earlier, putting the results into the @hl.scala{output} div we defined above, including highlighting.
+
+ @BookData.example(div, "WeatherSearch().main")
+
+ @p
+ And that's the working example! Try searching for cities like "Singapore" or "New York" or "San Francisco" and watch as the search narrows as you enter more characters into the text box. Note that the OpenWeatherMap API limits ambiguous searches to about a dozen results, so if a city doesn't turn up in a partial-search, try entering more characters to narrow it down.
+
+@sect{Interactive Web Pages Recap}
+ @p
+ In this chapter, we've explored the basics of how you can use Scala.js to build interactive web pages. The two main contributions are using Scalatags to render HTML in a concise, safe way, and making Ajax calls to external web services. We combined these two capabilities in a small weather-search app that let a user interactively search for the weather in different cities around the world.
+ @p
+ Some things you may have noticed in the process:
+
+ @ul
+ @li
+ Using Scalatags to render HTML fragments, and managing them at runtime with callbacks and getting/setting properties, is really quite nice
+ @li
+ Using @hl.scala{new dom.XMLHttpRequest} to make web requests feels just like the Javascript code to do so
+ @li
+ Using @hl.scala{Ajax.get(...)} and working with the resultant : @hl.scala{Future} feels a lot cleaner than directly using the Javascript API
+
+ @p
+ You're at this point reasonably pro
diff --git a/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex b/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex
new file mode 100644
index 0000000..171b2d3
--- /dev/null
+++ b/book/src/main/scalatex/book/indepth/AdvancedTechniques.scalatex
@@ -0,0 +1,305 @@
+@import BookData._
+@p
+ @sect.ref{Getting Started} walks you through how to set up some basic Scala.js applications, but that only scratches the surface of the things you can do with Scala.js. Apart from being able to use the same techniques you're used to in Scala-JVM in the browser, Scala.js opens up a whole range of possibilities and novel techniques that are not found in typical Scala-JVM applications.
+
+@p
+ Although these techniques may technically be possible on the JVM, very few Scala-JVM applications are built in a way that can take advantage of them. Most Scala-JVM code runs on back-end servers which have a completely different structure from the client-side apps that Scala.js allows.
+@p
+ This client-side user-interface-focused code lends itself to completely different design patterns from those used to develop server-side code. This section will explore a number of techniques which are present
+
+@ul
+ @li
+ @sect.ref("Functional-Reactive UIs", "Functional-reactive user interfaces")
+ @li
+ @sect.ref("Asynchronous Workflows", "Asynchronous user-interation workflows")
+
+@p
+ One note is that these are "Techniques" rather than "Libraries" because they have not been packaged up in a way that is sufficiently nice that you can use them out-of-the-box just by adding a dependency somewhere. Thus, they each require some small amount of boilerplate before use, though the amount of boilerplate is fixed: it does not grow with the size of your program, and anyway gives you a chance to tweak it to do exactly what you want.
+
+@sect{Functional-Reactive UIs}
+ @p
+ @lnk("Functional-reactive Programming", "http://en.wikipedia.org/wiki/Functional_reactive_programming") (FRP) is a field with encompasses several things:
+
+ @ul
+ @li
+ @b{Discrete}: Handling of first-class event-streams like in @link("RxJS", "https://github.com/Reactive-Extensions/RxJS")
+ @li
+ @b{Continuous}: Handling of first-class signals, like in @link("Elm", "http://elm-lang.org/learn/What-is-FRP.elm")
+
+ @sect{Why FRP}
+ @p
+ The value proposition of FRP is that in a "traditional" program, when an event occurs, events and changes propagate throughout the program in an ad-hoc manner. An event-listener may trigger additional events, call some callbacks, or set some mutable variables that subsequent code will read and react to.
+
+ @p
+ This works, but the ad-hoc nature is both free-ing and limiting. You are free to do whatever you want in response to any action, but in return the developer who maintains your code (e.g. yourself 6 months from now) has no idea what your code is doing in response to any action: the possible consequence of an action is basically "Anything"!
+
+ @p
+ Furthermore, because the propagation is ad-hoc, there is no way for the code to help ensure that you are propagating changes in a "valid" manner: it is thus easy for programmer errors to result in changes or events being incorrectly propagated. This most often results in data falling out of sync: a UI widget may forget to update when an action is taken, resulting in an inconsistent state being shown to the user, ultimately resulting in confused users.
+
+ @p
+ FRP basically structures these event- or change-propagations as first-class values within the program, either as an @hl.scala{EventSource[T]} type that represents a discrete source of individual @hl.scala{T} events, or as a @hl.scala{Signal[T]} type which represents a continuous time-varying value @hl.scala{T}. This comes at some cost within the program: you now have to program using these @hl.scala{EventSource}s or @hl.scala{Signal}s, rather than just ad-hoc running callbacks or listening-to/triggering events all over the place. In exchange, you get more powerful tools to work with these values, making it easy for the library to e.g. ensure that changes always propagate correctly throughout your program, and that all values are always kept in sync.
+
+ @sect{FRP with Scala.Rx}
+ @p
+ @lnk("Scala.Rx", "https://github.com/lihaoyi/scala.rx") is a change-propagation library that implements the @b{Continuous} style of FRP. To begin with, we need to include it in our @code{build.sbt} dependencies:
+
+ @hl.ref("examples/demos/build.sbt", "com.scalarx")
+
+ @p
+ Scala.Rx provides you with smart variables that automatically track dependencies with each other, such that if one smart variable changes, the rest re-compute immediately and automatically. The main primitives in Scala.Rx are:
+
+ @ul
+ @li
+ @b{Var}s: Smart variables that can be set manually, and automatically notify their dependents that they need to recompute
+ @li
+ @b{Rx}s: Smart values which are set as some computation of other @b{Rx}s or @b{Var}s, which recompute automatically when their dependencies change, and notify their dependents
+ @li
+ @b{Obs}s: Observers on either an @b{Rx} or a @b{Var}, which performs some action when it changes
+
+ @p
+ @hl.scala{Var}s and @hl.scala{Rx}s roughly correspond to the idea of a @hl.scala{Signal} described earlier. The documentation for Scala.Rx goes into this in much more detail, so if you're curious you should read it. This section will jump straight into how to use Scala.Rx with Scala.js.
+
+ @p
+ To begin with, let's set up our imports:
+
+ @hl.ref("examples/demos/src/main/scala/advanced/BasicRx.scala", "package advanced", "@JSExport")
+
+ @p
+ Here we are seeing the same @hl.scala{dom} and @hl.scala{scalatags}, imports we saw in the hands-on tutorial, as well a new @hl.scala{import rx._} which bring all the Scala.Rx names into the local namespace.
+
+ @p
+ Scala.Rx does not "natively" bind to Scalatags, but integrating them yourself is simple enough that it's not worth putting into a separate library. He's a simple integration:
+
+ @hl.ref("examples/demos/src/main/scala/advanced/BasicRx.scala", "implicit def")
+
+ @p
+ Scalatags requires that anything you want to embed in a Scalatags fragment be implicitly convertible to @hl.scala{Frag}; here we are providing one for any Scala.Rx @hl.scala{Rx[T]}s, as long as the @hl.scala{T} provided is itself convertible to a @hl.scala{Frag}. We call @hl.scala{r().render} to extract the "current" value of the @hl.scala{Rx}, and then set up an @hl.scala{Obs} that watches the @hl.scala{Rx}, replacing the previous value with the current one every time its value changes.
+
+ @p
+ Now that the set-up is out of the way, let's consider a simple HTML widhet that lets you enter text in a @hl.html{<textarea>}, and keeps track of the number of words, characters, and counts how long each word is.
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/advanced/BasicRx.scala", "val txt =")
+
+ @less
+ @example(div, "BasicRx().main")
+
+ @p
+ This snippet sets up a basic data-flow graph. We have our @hl.scala{txt} @hl.scala{Var}, and a bunch of @hl.scala{Rx}s (@hl.scala{numChars}, @hl.scala{numWords}, @hl.scala{avgWordLength}) that are computed based on @hl.scala{txt}.
+
+ @p
+ Next, we construct our Scalatags fragment: a @hl.scala{textarea} tag with a listener that updates @hl.scala{txt}, and a @hl.scala{div} containing the @hl.scala{textarea} and a list containing the bound values of our @hl.scala{Rx}s.
+
+ @p
+ That's all we need to end up with a live-updating widget, which re-renders the necessary bits of the page when the contents of the text box changes! Note how the code basically flows top-to-bottom, like a batch-rendering program, but at the end of it we get a live widget. The code is much simpler than a similar widget built up using jQuery or Backbone.
+
+ @p
+ Furthermore, there is no chance for the parts of the DOM which are "live" to fall out of sync. There is no visible logic that handles the individual re-calulations and re-renders: that is all done by Scala.Rx and by our @hl.scala{rxFrag} implicit. Because we do not need to write code for each site to keep each individual @hl.scala{Rx} and each DOM fragment in sync, that means there is no chance of the developer screwing it up and resulting in an out-of-sync page.
+
+ @sect{More Rx}
+ @p
+ That was a pretty simple example to get you started with a simple Scala.Rx application. Let's look at a more meaty example to see how we can use Scala.Rx to help structure our interactive web application:
+
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/advanced/BasicRx.scala", "val fruits =")
+
+ @less
+ @example(div, "BasicRx().main2")
+
+ @p
+ This is a basic re-implementation of the autocomplete widget we created in the chapter @sect.ref{Interactive Web Pages}, except done using Scala.Rx. Note that unlike the original implementation, we don't need to manage the clearing of the output area via @hl.scala{innerHTML = ""} and the re-rendering via @hl.scala{appendChild(...)}. All this is handled by the same @hl.scala{rxFrag} code we wrote earlier.
+
+ @p
+ Furthermore, this implementation is more efficient than the original: In the original, everything is always re-rendered every time, which can be a problem if the number of things being rendered is large. In this implementation, only when a fruit appears-in/disappears-from the list does re-rendering happen, and only for that particular fruit. For the bulk of the fruits which did not experience any change in appearance, the DOM is left entirely untouched.
+
+ @p
+ Again, there is no chance for the developer to make a mistake updating things, because all this rendering and re-rendering is hidden from view inside the library.
+
+ @hr
+
+ @p
+ Hopefully this has given you a sense of how you can use Scala.Rx to help build complex, interactive web applications. The implementation is tricky, but the basic value proposition is clear: you get to write your code top-to-bottom, like the most old-fashioned static pages, and have it transformed by Scala.Rx into an interactive, always-consistent web app. By abstracting away the whole event-propagation, manual-updating process inside the library, we have ensured that there is no place where the developer can screw it up, and the application's UI will forever be in sync with its data.
+
+@sect{Asynchronous Workflows}
+ @p
+ In a traditional setting, Scala applications tend to have a mix of concurrency models: some spawn multiple threads and use thread-blocking operations or libraries, others do things with Actors or Futures, trying hard to stay non-blocking throughout, while most are a mix of these two paradigms.
+
+ @p
+ On Scala.js, things are different: multi-threaded concurrency is a non-starter, since Javascript engines are all single-threaded. As a result, there are virtually no blocking APIs in Javascript: all operations need to be asynchronous if you don't want them to freeze the user interface of the browser while the operation is happening. Scala.js uses standard Javascript APIs and is no different.
+
+ @p
+ However, Scala.js has much more powerful tools to work with than your typical Javascript libraries. The Scala standard library comes with a rich API for @sect.ref{Futures & Promises}, which are thankfully 100% asynchronous. Though this design was chosen for performance on the JVM, it perfectly fits our 100% asynchronous Javascript APIs. We have tools like @sect.ref{Scala-Async}, which works perfectly with Scala.js, and lets you create asynchronous computations in a much less confusing manner.
+
+ @sect{Futures & Promises}
+ @p
+ A Future represents an in-progress computation that may or may not have completed. It may encapsulate a web request, or an RPC, or a task happening on another thread. They are not a novel concept, and Scala provides a good in-built implementation of Futures that works well with Scala.js.
+
+ @p
+ To motivate this, let's consider a simple example application that:
+
+ @ul
+ @li
+ Takes as user input a comma-separated list of city-names
+ @li
+ Fetches the temperature in each city from @code{api.openweathermap.org}
+ @li
+ Displays the results when they are all back
+
+ @p
+ We'll work through a few implementations of this.
+
+ @p
+ To begin with, let's write the scaffolding code, that will display the input box, deal with the listeners, and all that:
+
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "val myInput")
+
+ @p
+ So far so good. The only thing that's missing here is the mysterious @hl.scala{handle} function, which is given the list of names and the @hl.scala{output} div, and must handle the Ajax requests, aggregating the results, and displaying them in @hl.scala{output}. Let's also define a small number of helper functions that we'll use later:
+
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "def urlFor", "def parseTemp")
+
+ @p
+ @hl.scala{urlFor} encapsulates the messy URL-construction logic that we need to make the Ajax call to the right place.
+
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "parseTemp", "def formatResults")
+
+ @p
+ @hl.scala{parseTemp} encapsulates the messy result-extraction logic that we need to get the data we want (current temperature, in celsius) out of the structured JSON return blob.
+
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "def formatResults", "def main")
+
+ @p
+ @hl.scala{formatResults} encapsulates the conversion of the final @hl.scala{(name, celsius)} data back into readable HTML.
+
+ @p
+ Overall, these helper functions do nothing special, btu we're defining them first to avoid having to copy-&-paste code throughout the subsequent examples. Now that we've defined all the relevant scaffolding, let's walk through a few ways that we can implement the all-important @hl.scala{handle} method.
+
+ @def scrollDiv = div(
+ height:="200px",
+ overflow:="scroll"
+ )
+
+ @sect{Direct Use of XMLHttpRequest}
+ @example(scrollDiv, "Futures().main0")
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "def handle0", "main")
+
+ @p
+ This is a simple solution that directly uses the @hl.scala{XMLHttpRequest} class that is available in Javascript in order to perform the Ajax call. Every Ajax call that returns, we aggregate in a @hl.scala{results} buffer, and when the @hl.scala{results} buffer is full we then append the formatted results to the output div.
+ @p
+ This is relatively straightforward, though maybe knottier than people would be used to. For example, we have to "construct" the Ajax call via calling mutating methods and setting properties on the @hl.scala{XMLHttpRequest} object, where it's easy to make a mistake. Furthermore, we need to manually aggregate the @hl.scala{results} and keep track ourselves whether or not the calls have all completed, which again is messy and error-prone.
+
+ @p
+ This solution is basically equivalent to the initial code given in the @sect.ref{Raw Javascript} section of @sect.ref{Interactive Web Pages}, with the additional code necessary for aggregation. As described in @sect.ref{dom.extensions}, we can make use of the @hl.scala{Ajax} object to make it slightly tidier.
+
+ @sect{Using dom.extensions.Ajax}
+ @example(scrollDiv, "Futures().main1")
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "def handle1", "main")
+
+ @p
+ This solution uses the @hl.scala{dom.extensions.Ajax} object, as described in @hl.scala{dom.extensions}. This basically wraps the messy @hl.scala{XMLHttpRequest} interface in a single function that returns a @hl.scala{scala.concurrent.Future}, which you can then map/foreach over to perform the action when the @hl.scala{Future} is complete.
+ @p
+ However, we still have the messiness inherent in the result aggregation: we don't actually want to perform our action (writing to the @hl.scala{output} div) when one @hl.scala{Future} is complete, but only when @i{all} the @hl.scala{Future}s are complete. Thus we still need to do some amount of manual book-keeping in the @hl.scala{results} buffer.
+
+ @sect{Future Combinators}
+ @example(scrollDiv, "Futures().main2")
+ @hl.ref("examples/demos/src/main/scala/advanced/Futures.scala", "def handle2", "main")
+
+ @p
+ Since we're using Scala's @hl.scala{Future}s, we aren't limited to just map/foreach-ing over them. @hl.scala{scala.concurrent.Future} provides a @lnk("rich api", "http://www.scala-lang.org/files/archive/nightly/docs/library/scala/concurrent/Future.html") that can be used to deal with common tasks like working with lists of futures in parallel, or aggregating the result of futures together.
+ @p
+ Here, instead of manually counting until all the @hl.scala{Future}s are complete, we instead create the Futures which will contain what we want (name and temperature) and store them in a list. Then we can use the @hl.scala{Future.sequence} function to invert the @hl.scala{Seq[Future[T]]} into a @hl.scala{Future[Seq[T]]}, a single Future that will provide all the results in a single list when every Future is complete. We can then simply foreach- over the single Future to get the data we need to feed to @hl.scala{formatResults}/@hl.scala{appendChild}.
+
+ @p
+ This approach is significantly neater than the previous two examples: we no longer have any mutation going on, and the logic is expressed in a very high-level, simple manner. "Make a bunch of Futures, join them, use the result" is much less error-prone than the imperative result-aggregation-and-counting logic used in the previous examples.
+
+ @hr
+
+ @p
+ @hl.scala{scala.concurrent.Future} isn't limited to just calling @hl.scala{.sequence} on lists. It provides the ability to @hl.scala{.zip} two Futures of different types together to get their result, or @hl.scala{.recover} in the case where Futures fail. Although these tools were originally built for Scala-JVM, all of them work unchanged on Scala.js, and serve their purpose well in simplifying messy asynchronous computations.
+
+ @sect{Scala-Async}
+ @p
+ Let's look at how to use Scala-Async. To motivate us, let's consider a simple paint-like canvas application similar to the one we built in the section @sect.ref{Making a Sketchpad using Mouse Input}. This application will have a few properties:
+
+ @ul
+ @li
+ The user clicks and drags to begin drawing a line on the canvas
+ @li
+ When the user releases the mouse, we fill the shape that was formed by the dragging
+ @li
+ The user clicks again to clear the canvas; like most clicks, the action happens when the button is released
+ @li
+ And can repeat the process from the top, indefinitely
+
+ @p
+ This is a toy example, but is enough to bring out the difficulty of doing things the "traditional" way, and why using Scala-Async with Scala.js is superior. To begin with, let's set the stage:
+
+ @hl.ref("examples/demos/src/main/scala/advanced/Async.scala", "val renderer")
+
+ @p
+ To initialize the canvas with the part of the code which will remain the same, so we can look more closely at the code which differs.
+
+ @sect{Traditional Asynchrony}
+ @p
+ Let's look at a traditional implementation, using Scala.js but no special features. We'll just use the Javascript @hl.scala{canvas.onmouveXXX} operations directly.
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/advanced/Async.scala", "// traditional")
+
+ @less
+ @example(canvas, "Async().main0")
+ @p
+ This is a working implementation, and you can play with it on the right. We basically set the three listeners:
+
+ @ul
+ @li
+ @hl.scala{canvas.onmousemove}
+ @li
+ @hl.scala{canvas.onmousedown}
+ @li
+ @hl.scala{canvas.onmouseup}
+
+ @p
+ And each listener is in charge of deciding what to do when it is it's turn to fire.
+
+ @p
+ This code is pretty tricky and hard to follow. It's not immediately clear what it is doing. One thing you may notice is the presence of this @hl.scala{dragState} variable, which seems to add a lot to the confusion with branches all over the place. At first you may think you can simplify the code to do without it, but attempts to do so will reveal why it is necessary.
+
+ @p
+ This variable is necessary because each mouse event could mean different things at different times. For example, @hl.scala{canvas.onmousemove} should do nothing it occurs between an @hl.scala{canvas.onmousedown} and @hl.scala{canvas.onmouseup}. @hl.scala{canvas.onmouseup} itself has two tasks: it either ends the dragging phase (which necessitates the fill-current-shape call) or it serves to clear the canvas if happening after a drag. And @hl.scala{canvas.onmousedown} should not start a new drag if the previous drawing hasn't been cleared from the canvas.
+
+ @p
+ This is a pretty simple workflow for the user, and yet the code is already tricky enough it's not obvious that it's correct at first glance. More complex tools will have correspondingly more complex workflows, and it is easy to see how just another 1 or 2 more states can get out of hand.
+
+ @sect{Using Scala-Async}
+ @p
+ Now we've seen what a "traditional" approach looks like, let's look at how we would do this using Scala-Async.
+
+ @split
+ @more
+ @hl.ref("examples/demos/src/main/scala/advanced/Async.scala", "// async")
+
+ @less
+ @example(canvas, "Async().main")
+
+ @p
+ We have an @hl.scala{async} block, which contains a while loop. Each round around the loop, we wait for the @hl.scala{mousedown} channel to start the path, waiting for either @hl.scala{mousedown} or @hl.scala{mousedown} (which continues the path or ends it respectively), fill the shape, and then wait for another @hl.scala{mousedown} before clearing the canvas and going again.
+
+ @p
+ Hopefully you'd agree that this code is much simpler to read and understand than the previous version. In particular, the control-flow of the code goes from top to bottom in a "natural" fashion, rather than jumping around ad-hoc like in the previous callback-based design.
+ @p
+ You may be wondering what these @hl.scala{Channel} things are, and where they are coming from. Although these are not provided by Scala, they are pretty straightforward to define ourselves:
+
+ @hl.ref("examples/demos/src/main/scala/advanced/Async.scala", "case class Channel")
+
+ @p
+ The point of @hl.scala{Channel} is to allow us to turn event-callbacks (like those provided by the DOM's @hl.scala{onmouseXXX} properties) into some kind of event-stream, that we can listen to asynchronously (via @hl.scala{apply} that returns a @hl.scala{Future}) or merge via @hl.scala{|}. This is a minimal implementation for what we need now, but it would be easy to provide more functionality (filter, map, etc.) as necessary.
+
+ @hr
+
+ @p
+ Scala-Async is a Macro; that means that it is both more flexible and more limited than normal Scala, e.g. you cannot put the @hl.scala{await} call inside a lambda or higher-order-function like @hl.scala{.map}. Like Futures, it doesn't provide any fundamentally new capabilities, but is a tool that can be used to simplify otherwise messy asynchronous workflows.
diff --git a/book/src/main/scalatex/book/indepth/CompilationPipeline.scalatex b/book/src/main/scalatex/book/indepth/CompilationPipeline.scalatex
new file mode 100644
index 0000000..1718f5e
--- /dev/null
+++ b/book/src/main/scalatex/book/indepth/CompilationPipeline.scalatex
@@ -0,0 +1,185 @@
+
+@p
+ Scala.js is implemented as a compiler plugin in the Scala compiler. Despite this, the overall process looks very different from that of a normal Scala application. This is because Scala.js optimizes for the size of the compiled executable, which is something that Scala-JVM does not usually do.
+
+@sect{Whole Program Optimizaton}
+ @p
+ At a first approximation, Scala.js achieves its tiny executables by using whole-program optimization. Scala-JVM, like Java, allows for separate compilation: this means that after compilation, you can combine your compiled code with code compiled separately, which can interact with the code you already compiled in an ad-hoc basis: code from both sides can call each others methods, instantiate each others classes, etc. without any limits.
+
+ @p
+ Even things like package-private do not help you: Java packages are separate-compile-able too, and multiple compilation runs can dump things in the same package! You may think that private members and methods may be some salvation, but the Java ecosystem typically relies heavily on reflection, which depends on the fact that these private things remain exactly as-they-are.
+
+ @p
+ Overall, this makes it difficult to do any meaningful optimization: you never know whether or not you can eliminate a class, method or field. Even if it's not used anywhere you can see, it could easily be used by some other code compiled separately, or accessed through reflection.
+
+ @p
+ With Scala.js, we have decided to forgo reflection, and forgo separate compilation, in exchange for smaller executables. This is made easier by the fact that the pure-Scala ecosystem makes little use of reflection overall. Thus, at the right before shipping your Scala.js app to your users, the Scala.js optimizer gathers up all your Scala.js code, determines which things are used and which are not, and eliminates all the un-used classes/methods/variables. This allows us to achieve a much smaller code size than is possible with reflection/separate-compilation support. Furthermore, because we forgo these two things, we can perform much more aggressive inlining and other compile-time optimizations than is possible with Scala-JVM, further reducing code size and improving performance.
+
+ @p
+ It's worth noting that such optimizations exist as an option on the JVM aswell: @lnk("Proguard", "http://proguard.sourceforge.net/") is a well known library for doing similar DCE/optimization for Java/Scala applications, and is extensively used in developing mobile applications which face similar "minimize-code-size" constraints that web-apps do. However, the bulk of Scala code which runs on the server does not use these tools.
+
+@sect{How Compilation Works}
+ @p
+ The Scala.js compilation pipeline is roughly split into multiple stages:
+
+ @ul
+ @li
+ @b{Initial Compilation}: @code{.scala} files to @code{.class} and @code{.sjsir} files
+ @li
+ @b{Fast Optimization}: @code{.sjsir} files to smallish/fast @code{.js} files
+ @li
+ @b{Full Optimization}: @code{.js} files to smaller/faster @code{.js} files
+
+ @p
+ @code{.scala} files are the source code you're familiar with. @code{.class} files are the JVM-targetted artifacts which aren't used, but we keep around: tools such as @lnk.misc.IntelliJ or @lnk.misc.Eclipse use these files to provide IDE support for Scala.js code, even if they take no part in compilation. @code{.js} files are the output Javascript, which we can execute in a web browser.
+ @p
+ @code{.sjsir} files are worth calling out: the name stands for "ScalaJS Intermediate Representation", and these files contain compiled code half-way between Scala and Javascript: most Scala features have by this point been replaced their Java/Javascript equivalents, but it still contains Types (which have all been inferred) that can aid in analysis. Many Scala.js specific optimizations take place on this IR.
+
+ @p
+ Each stage has a purpose, and together the stages do bring benefits to offset their cost in complexity. The original compilation pipeline was much more simple:
+
+ @ul
+ @li
+ @b{Compilation}: @code{.scala} files to @code{.js} files
+
+ @p
+ But produced far larger (20mb) and slower executables. This section will explore each stage and we'll learn what these stages do, starting with a small example program:
+
+ @hl.scala
+ def main() = {
+ var x = 0
+ while(x < 999){
+ x = x + "2".toInt
+ }
+ println(x)
+ }
+
+ @sect{Compilation}
+ @p
+ As described earlier, the Scala.js compiler is implemented as a Scala compiler plugin, and lives in the main repository in @lnk("compiler/", "https://github.com/scala-js/scala-js/tree/master/compiler"). The bulk of the plugin runs after the @code{mixin} phase in the @lnk("Scala compilation pipeline", "http://stackoverflow.com/a/4528092/871202"). By this point:
+
+ @ul
+ @li
+ Types and implicits have all been inferred
+ @li
+ Pattern-matches have been compiled to imperative code
+ @li
+ @hl.scala("@tailrec") functions have been translated to while-loops, @hl.scala{lazy val}s have been replaced by @hl.scala{var}s.
+ @li
+ @hl.scala{trait}s have been @lnk("replaced by interfaces and classes", "http://stackoverflow.com/a/2558317/871202")
+
+ @p
+ Overall, by the time the Scala.js compiler plugin takes action, most of the high-level features of the Scala language have already been removed. Compared to a hypothetical, alternative "from scratch" implementation, this approach has several advantages:
+
+ @ul
+ @li
+ It helps ensure that the semantics of these features always, 100% match that of Scala-JVM
+ @li
+ It reduces the amount of implementation work required by re-using the existing compilation phases
+
+ @p
+ This first phase is mostly a translation from the Scala compiler's internal AST to the Scala.js Intermediate Representation, and does not contain very many interesting optimizations. At the end of the initial compilation, the Scala compiler with Scala.js plugin results in two sets of files:
+
+ @ul
+ @li
+ The original @code{.class} files, as if it were compiled on the JVM. For code that does not use any Javascript interop, these are perfectly valid Java @code{.class} files full of bytecode and can even be executed on the JVM.
+ @li
+ The @code{.sjsir} files, destined for further compilation in the Scala.js pipeline.
+
+ @p
+ The ASTs defined in the @code{.sjsir} files is at about the same level of abstraction as the @hl.scala{Tree}s that the Scala compiler is working with at this stage. However, the @hl.scala{Tree}s within the Scala compiler contain a lot of cruft related to the compiler internals, and are also not easily serializable. This phase cleans them up into a "purer" format, (defined in the @lnk("ir/", "https://github.com/scala-js/scala-js/blob/master/ir/src/main/scala/scala/scalajs/ir/Trees.scala") folder) which is also serializable.
+
+ @p
+ This is the only phase in the Scala.js compilation pipeline that separate compilation is possible: you can compile many different sets of Scala.js @code{.scala} files separately, only to combine them later. This is used e.g. for distributing Scala.js libraries as Maven Jars, which are compiled separately by library authors to be combined into a final executable later.
+
+ @sect{Fast Optimization}
+ @hl.javascript
+ ScalaJS.c.LExample$.prototype.main__V = (function() {
+ var x = 0;
+ while ((x < 999)) {
+ x = ((x + new ScalaJS.c.sci_StringOps().init___T(
+ ScalaJS.m.s_Predef().augmentString__T__T("2")
+ ).toInt__I()) | 0)
+ };
+ ScalaJS.m.s_Predef().println__O__V(x)
+ });
+
+ @p
+ This phase is a whole-program optimization of the @code{.sjsir} files, and lives in the @lnk("tools/", "https://github.com/scala-js/scala-js/tree/master/tools") folder of the Scala.js repository. The end result is a rough translation of Scala into the equivalent Javascript (e.g. above):
+
+ @ul
+ @li
+ Scala-style method @hl.scala{def}s become Javascript-style prototype-function-assignment
+ @li
+ Scala @hl.scala{var}s become Javascript @hl.scala{var}s
+ @li
+ Scala @hl.scala{while}s become Javascript @hl.scala{while}s
+ @li
+ Implicits are materialized, hence all the @hl.scala{StringOps} and @hl.scala{augmentString} extensions are present in the output
+ @li
+ Classes and methods are fully-qualified, e.g. @hl.scala{println} becomes @hl.scala{Predef().println}
+ @li
+ Method names are qualified by their types, e.g. @hl.scala{__O__V} means that @hl.scala{println} takes @hl.scala{Object} and returns @hl.scala{void}
+
+ @p
+ This is an incomplete description of the translation, but it should give a good sense of how the translation from Scala to Javascript looks like. In general, the output is verbose but straightforward.
+
+ @p
+ In addition to this superficial translation, the optimizer does a number of things which are more subtle and vary from case to case. The rough operations that get performed are:
+
+ @ul
+ @li
+ @b{Dead-code elimination}: entry-points to the program such as @hl.scala("@JSExport")ed methods/classes are kept, as are any methods/classes that these reference. All others are removed. This reduces the potentially 20mb of Javascript generated by a naive compilation to a more manageable 700kb-1mb for a typical application
+ @li
+ @b{Inlining}: in cases where a method is only called in a few places, the optimizer inlines the implementation of the method at those callsites. This typically reduces the code size by a small amount, but offers a several-times speedup of the generated code by inlining away much of the overhead from the abstractions (implicit-conversions, higher-order-functions, etc.) in Scala's standard library.
+ @p
+ As a whole-program optimization, it tightly ties together the code it is compiling and does not let you e.g. inject additional classes later. This does not mean you cannot interact with external code at all: you can, but it has to go through explicitly @hl.scala{@@JSExport}ed methods and classes via Javascript Interop, and not on ad-hoc classes/methods within the module. Thus it's entirely possible to have multiple "whole-programs" running in the same browser; they just will likely have duplicate copies of e.g. standard library classes inside of them, since they cannot share the code as it's not exported.
+
+ @p
+ While the input for this phase is the aggregate @code{.sjsir} files from your project and all your dependencies, the output is executable Javascript. This phase usually runs in less than a second, outputs a Javascript blob in the 700kb-1mb range, and is suitable for repeated use during development. This corresponds to the @code{fastOptJS} command in SBT.
+
+ @sect{Full Optimization}
+ @hl.javascript
+ be.prototype.main=function(){
+ for(var a=0;999>a;)
+ a=a+(new de).g(S(L(),"2")).ne()|0;
+ ee(); L();
+ var b=F(fe); ge();
+ a=(new he).g(w(a)); b=bc(0,J(q(b,[a])));
+ ie(bc(L(),J(q(F(fe),[je(ke(ge().Vg),b)]))))
+ }
+
+ @p
+ The @lnk("Google Closure Compiler", "https://developers.google.com/closure/compiler/") (GCC) is a set of tools that work with Javascript. It has multiple @lnk("levels of optimization", "https://developers.google.com/closure/compiler/docs/compilation_levels"), doing everything from basic whitespace-removal to heavy optimization. It is a old, relatively mature project that is relied on both inside and outside google to optimize the delivery of Javascript to the browser.
+
+ @p
+ Scala.js uses GCC in its most aggressive mode: @lnk("Advanced Optimization", "https://developers.google.com/closure/compiler/docs/api-tutorial3"). GCC spits out a compressed, minified version of the Javascript (above) that @sect.ref{Fast Optimization} spits out: e.g. in the example above, all identifiers have been renamed to short strings, the @hl.javascript{while}-loop has been replaced by a @hl.javascript{for}-loop, and the @hl.scala{println} function has been inlined.
+
+ @p
+ As described in the linked documentation, GCC performs optimizations such as:
+
+ @ul
+ @li
+ Whitespace removal
+ @li
+ Variable and property renaming
+ @li
+ Dead code elimination
+ @li
+ Inlining
+
+ @p
+ Notably, GCC @i{does not preserve the semantics of arbitrary Javascript}! In particular, it only works for a subset of Javascript that it understands and can properly analyze. This is an issue when hand-writing Javascript for GCC since it's very easy to step outside that subset and have GCC break your code, but is not a worry when using Scala.js: the Scala.js optimizer (the previous phase in the pipeline) automatically outputs Javascript which GCC understands and can work with.
+ @p
+ GCC duplicates a lot of functionality that the Scala.js optimizer already does, such as DCE and inlining. It is entirely possible to skip the optimization phase, output the naive 20mb Javascript blob, and run GCC on it to bring the size down. However, GCC is much slower than the Scala.js optimizer, taking 60 seconds where the optimizer takes less than 1.
+
+ @p
+ Empirically, running GCC on the output of the optimizer produces the smallest output blobs: ~150-400kb, significantly smaller than the output of running either of them alone, and so that is what we do. This takes 5-10 seconds to run, which makes it somewhat slow for iterative development, so it's typically only run right before final testing and deployment. This corresponds to the @code{fullOptJS} command in SBT.
+
+@hr
+
+@p
+ This hopefully has given a good overview of how the Scala.js compilation pipeline works. The pipeline and optimizer is a work-in-progress, and is changing all the time in an attempt to achieve ever-smaller executables and ever-faster code.
+
+@p
+ This whole chapter has been focused on the @i{what} but not the @i{why}. The chapter on @sect.ref{Scala.js' Design Space} contains a section which talks about @sect.ref("Small Executables", "why we care so much about small executables"). \ No newline at end of file
diff --git a/book/src/main/scalatex/book/indepth/DesignSpace.scalatex b/book/src/main/scalatex/book/indepth/DesignSpace.scalatex
new file mode 100644
index 0000000..a1e807f
--- /dev/null
+++ b/book/src/main/scalatex/book/indepth/DesignSpace.scalatex
@@ -0,0 +1,239 @@
+@p
+ Scala.js is a relatively large project, and is the result of both an enormous amount of hard work as well as a number of decisions that craft what it's like to program in Scala.js today. Many of these decisions result in marked differences from the behavior of the same code running on the JVM. This chapter explores the reasoning and rationale behind these decisions.
+
+
+@sect{Why No Reflection?}
+ @p
+ Scala.js prohibits reflection as it makes dead-code elimination difficult, and the compiler relies heavily on dead-code elimination to generate reasonably-sized executables. The chapter on @sect.ref("The Compilation Pipeline") goes into more detail of why, but a rough estimate of the effect of various optimizations on a small application is:
+
+ @ul
+ @li
+ @b{Full Output} - ~20mb
+ @li
+ @b{Naive Dead-Code-Elimnation} - ~800kb
+ @li
+ @b{Inlining Dead-Code-Elimnation} - ~600kb
+ @li
+ @b{Minified by Google Closure Compiler} - ~200kb
+
+ @p
+ The default output size of 20mb makes the executables difficult to work with. Even though browsers can deal with 20mb Javascript blobs, it takes the browser several seconds to even load it, and up to a minute after that for the JIT to optimize the whole thing.
+
+ @sect{Dead Code Elimination}
+ @p
+ To illustrate why reflection makes things difficult, consider a tiny application:
+
+ @hl.scala
+ @@JSExport
+ object App extends js.JSApp{
+ @@JSExport
+ def main() = {
+ println(foo())
+ }
+ def foo() = 10
+ def bar = "i am a cow"
+ }
+ object Dead{
+ def complexFunction() = ...
+ }
+
+ @p
+ When the @sect.ref("Fast Optimization", "Scala.js optimizer"), looks at this application, it is able to deduce certain things immediately:
+
+ @ul
+ @li
+ @hl.scala{App} and @hl.scala{App.main} are exported via @hl.scala{@@JSExport}, and thus can't be considered dead code.
+ @li
+ @hl.scala{App.foo} is called from @hl.scala{App.main}, and so has to be kept around
+ @li
+ @hl.scala{App.bar} is never called from @hl.scala{App.main} or @hl.scala{App.foo}, and so can be eliminated
+ @li
+ @hl.scala{Dead}, including @hl.scala{Dead.complexFunction}, are not called from any live code, and can be eliminated.
+
+ @p
+ The actual process is a bit more involved than this, but this is a first-approximation of how the dead-code-elimination works: you start with a small set of live code (e.g. @hl.scala{@@JSExport}ed things), search out to find the things which are recursively reachable from that set, and eliminate all the rest. This means that the Scala.js compiler can eliminate, e.g., parts of the Scala standard library that you are not using. The standard library is not small, and makes up the bulk of the 20mb of the uncompressed blob.
+
+ @sect{Whither Reflection?}
+ @p
+ To imagine why reflection makes this difficult, imagine a slightly modified program which includes some reflective calls in @hl.scala{App.main}
+
+ @hl.scala
+ @@JSExport
+ object App extends js.JSApp{
+ @@JSExport
+ def main() = {
+ Class.forName(userInput()).getMethod(userInput()).invoke()
+ }
+ def foo() = 10
+ def bar = "i am a cow"
+ }
+ object Dead{
+ def complexFunction() = ...
+ }
+
+ @p
+ Here, we're assuming @hl.scala{userInput()} is some method which returns a @hl.scala{String} that was input by the user or otherwise somehow decided at runtime.
+ @p
+ We can start the same process: @hl.scala{App.main} is live since we @hl.scala{@@JSExport}ed it, but what objects or methods are reachable from @hl.scala{App.main}? The answer is: it depends on the values of @hl.scala{userInput()}, which we don't know. And hence we don't know which classes or methods are reachable! Depending on what @hl.scala{userInput()} returns, any or all methods and classes could be used by @hl.scala{App.main()}.
+ @p
+ This leaves us a few options:
+
+ @ul
+ @li
+ Keep every method or class around at runtime. This severely hampers the compiler's ability to optimize, and results in massive 20mb executables.
+ @li
+ Ignore reflection, and go ahead and eliminate/optimize things assuming reflection did not exist.
+ @li
+ Allow the user to annotate methods/classes that should be kept, and eliminate the rest.
+
+ @p
+ All three are possible options: Scala.js started off with #1. #3 is the approach used by @lnk("Proguard", "http://proguard.sourceforge.net/manual/examples.html#annotated"), which lets you annotate things e.g. @hl.scala{@@KeepApplication} to preserve things for reflection and preventing Proguard from eliminating them as dead code.
+
+ @p
+ In the end, Scala.js chose #2. This is helped by the fact that overall, Scala code tends not to use reflection as heavily as Java, or dynamic languages which use it heavily. Scala uses techniques such as @lnk("lambdas", "http://docs.scala-lang.org/tutorials/tour/anonymous-function-syntax.html") or @lnk("implicits", "http://docs.scala-lang.org/tutorials/tour/implicit-parameters.html") to satisfy many use cases which Java has traditionally used reflection for, while friendly to the optimizer.
+
+ @p
+ There are a range of use-cases for reflection where you want to inspect an object's structure or methods, where lambdas or implicits don't help. People use reflection to @lnk("serialize objects", "http://jackson.codehaus.org/DataBindingDeepDive"), or for @lnk("routing messages to methods", "https://access.redhat.com/documentation/en-US/Fuse_ESB_Enterprise/7.1/html/Implementing_Enterprise_Integration_Patterns/files/BasicPrinciples-BeanIntegration.html"). However, both these cases can be satisfied by...
+
+ @sect{Macros}
+
+ @p
+ The Scala programming language, since the 2.10.x series, has support for @lnk("Macros", "http://docs.scala-lang.org/overviews/macros/overview.html") in the language. Although experimental, these are heavily used in many projects such as Play and Slick and Akka, and allow a developer to perform compile-time computations and generate code where-ever the macros are used.
+
+ @p
+ People typically think of macros as AST-transformers: you pass in an AST and get a modified AST out. However, in Scala, these ASTs are strongly-typed, and the macro is able to inspect the types involved in generating the output AST. This leads to a lot of @lnk("interesting techniques", "http://docs.scala-lang.org/overviews/macros/implicits.html") around macros where you synthesize ASTs based on the type (explicit or inferred) of the macro callsite, something that is impossible in dynamic languages.
+
+ @p
+ Practically, this means that you can use macros to do things such as inspecting the methods, fields and other type-level properties of a typed value. This allows us to do things like @lnk("serialize objects with no boilerplate", "https://github.com/lihaoyi/upickle"):
+
+ @hl.scala
+ import upickle._
+
+ case class Thing(a: Int, b: String)
+ write(Thing(1, "gg"))
+ // res23: String = {"a": 1, "b": "gg"}
+
+ @p
+ Or to @lnk("route messages to the appropiate methods", "https://github.com/lihaoyi/autowire") without boilerplate, and @i{without} using reflection!
+
+ @p
+ The fact that you can satisfy these use cases with macros is non-obvious: in dynamic languages, macros only get an AST, which is basically opaque when you're only passing a single value to it. With Scala, you get the value @i{together with it's type}, which lets you inspect the type and generate the proper serialization/routing code that is impossible to do in a dynamic language with macros.
+
+ @p
+ Using macros here also plays well with the Scala.js optimizer: the macros are fully expanded before the optimizer is run, so by the time the optimizer sees the code, there is no more magic left: it is then free to do dead-code-elimination/inlining/other-optimizations without worrying about reflection causing the code to do weird things at runtime. Thus, we've managed to substitute most of the main use-cases of reflection, and so can do without it.
+
+@sect{Why does error behavior differ?}
+ @p
+ Scala.js deviates from the semantics of Scala-JVM in several ways. Many of these ways revolve around the edge-conditions of a program: what happens when something goes wrong? An array index is out of bounds? An integer is divided-by-zero? These differences cause some amount of annoyance when debugging, since when you mess up an array index, you expect an exception, not silently-invalid-data!
+
+ @p
+ In most of these cases, it was a trade-off between performance and correctness. These are situations where the default semantics of Scala deviate from that of Javascript, and Scala.js would have to perform extra work to emulate the desired behavior. For example, compare the division behavior of the JVM and Javascript.
+ @sect{Divide-by-zero: a case study}
+ @hl.scala
+ /*JVM*/
+ 15 / 4 // 3
+ @hl.javascript
+ /*JS*/
+ 15 / 4 // 3.25
+ @p
+ On the JVM, integer division is a primitive, and dividing @hl.scala{15 / 4} gives @hl.scala{3}. However, in Javascript, it gives @hl.javascript{3.25}, since all numbers of double-precision floating points.
+
+ @p
+ Scala.js works around this in the general case by adding a @hl.javascript{| 0} to the translation, e.g.
+
+ @hl.scala
+ /*JVM*/
+ 15 / 4 // 3
+ @hl.javascript
+ /*JS*/
+ (15 / 4) | 0 // 3
+
+ @p
+ This gives the correct result for most numbers, and is reasonably efficient. However, what about dividing-by-zero?
+
+ @hl.scala
+ /*JVM*/
+ 15 / 0 // ArithmeticException
+ @hl.javascript
+ /*JS*/
+ 15 / 0 // Infinity
+ (15 / 0) | 0 // 0
+
+ @p
+ On the JVM, the JVM is kind enough to throw an exception for you. However, in Javascript, the integer simply wraps around to @hl.javascript{Infinity}, which then gets truncated down to zero.
+ @p
+ So that's the current behavior of integers in Scala.js. One may ask: can we fix it? And the answer is, we can:
+ @hl.scala
+ /*JVM*/
+ 1 / 0 // ArithmeticException
+ @hl.javascript
+ /*JS*/
+ function intDivide(x, y){
+ var z = x / y
+ if (z == Infinity) throw new ArithmeticException("Divide by Zero")
+ else return z
+ }
+ intDivide(1, 0) // ArithmeticException
+ @p
+ This translation fixes the problem, and enforces that the @hl.scala{ArithmeticException} is thrown at the correct time. However, this approach causes some overhead: what was previously two primitive operations is now a function call, a local variable assignment, and a conditional. That is a lot more expensive than two primitive operations!
+
+ @sect{The Performance/Correctness Tradeoff}
+ @p
+ In the end, a lot of the semantic differences listed here come down to the same tradeoff: we could make the code behave more-like-Scala, but at a cost of adding overhead via function calls and other checks. Furthermore, the cost is paid regardless of whether the "exceptional case" is triggered or not: in the example above, every division in the program pays the cost!
+ @p
+ The decision to not support these exceptional cases comes down to a value judgement: how often do people actually depend on an exception being thrown as part of their program semantics, e.g. by catching it and performing actions? And how often are they just a way of indicating bugs? It turns out that very few @hl.scala{ArithmeticException}s, @hl.scala{ArrayIndexOutOfBoundsException}s, or similar are actually a necessary part of the program! They exist during debugging, but after that, these code paths are never relied upon "in production".
+ @p
+ Thus Scala.js goes for a compromise: in the Fast Optimization mode, we run the code with all these checks in place, so as to catch cases where these errors occur close-to-the-source and make it easy for you to debug them. In Full Optimization mode, on the other hand, we remove these checks, assuming you've already ran through these cases and found any bugs during development.
+ @p
+ This is a common pattern in situations where there's a tradeoff between debuggability and speed. In Scala.js' case, it allows us to get good debuggability in development, as well as good performance in production. There's some loss in debuggability in development, sacrificed in exchange for greater performance.
+
+@sect{Small Executables}
+ Why do we care so much about how big our executables are in Scala.js? Why don't we care about how big they are on Scala-JVM? This is mostly due to three reasons:
+
+ @ul
+ @li
+ When cross-compiling Scala to Javascript, the end-result tends to be much more verbose than when cross-compiled to Java Bytecode.
+ @li
+ Scala.js typically is run in web browsers, which typically do not work well with large executables compared to e.g. the JVM
+ @li
+ Scala.js often is delivered to many users over the network, and long download times force users to wait, degrading the user experience
+
+ @p
+ These factors combined means that Scala.js has to put in extra effort to optimize the code to reduce it's size at compile-time.
+
+ @sect{Raw Verbosity}
+ @p
+ Scala.js compiles to Javascript source code, while Scala-JVM compiles to Java bytecode. Java bytecode is a binary format and thus somewhat optimized for size, while Javascript is textual and is designed to be easy to read and write by hand.
+ @p
+ What does these mean, concretely? This means that a symbol marking something, e.g. the start of a function, is often a single byte in Java bytecode. Even more, it may not have any delimiter at all, instead the meaning of the binary data being inferred from its position in the file! On the other hand, in Javascript, declaring a function takes a long-and-verbose @hl.javascript{function} keyword, which together with peripheral punctuation (@code{.}, @code{ = }, etc.) often adds up to tens of bytes to express a single idea.
+ @p
+ What does this mean concretely? This means that expressing the same meaning in Javascript usually takes more "raw code" than expressing the same meaning in Java bytecode. Even though Java bytecode is relatively verbose for a binary format, it still is significantly more concise the Javascript, and it shows: the Scala standard library weighs in at a cool 6mb on Scala-JVM, while it weighs 20mb on Scala.js.
+ @p
+ All things being equal, this would mean that Scala.js would have to work harder to keep down code-size than Scala-JVM would have to. Alas, not all other things are equal.
+
+ @sect{Browsers Performance}
+ @p
+ Without any optimization, a naive compilation to Scala.js results in an executable (Including the standard library) weighing around 20mb. On the surface, this isn't a problem: runtimes like the JVM have no issue with loading 20mb of Java bytecode to execute; many large desktop applications weigh in the 100s of megabytes while still loading and executing fine.
+ @p
+ However, the web browser isn't a native execution environment; loading 20mb of Javascript is sufficient to heavily tax even the most modern web browsers such as Chrome and Firefox. Even though most of the code comprises class and method definitions that never have their contents executed, loading such a heavy load into e.g. Chrome makes it freeze for 5-10 seconds initially. Even after that, even after the code has all been parsed and isn't been actively executed, having all this Javascript makes the browser sluggish for up to a minute before the JIT compiler can speed things up.
+ @p
+ Overall, this means that you probably do not want to work with un-optimized Scala.js executables. Even for development, the slow load times and initial sluggishness make testing the results of your hard-work in the browser a frustrating experience. But that's not all...
+
+ @sect{Deployment Size}
+ @p
+ Scala.js applications often run in the browser. Not just any browser, but the browsers of your users, who had come to your website or web-app to try and accomplish some task. This is in stark contrast the Scala-JVM applications, which most often run on servers: servers that you own and control, and can deploy code to at your leisure.
+
+ @p
+ When running code on your own servers in some data center, you often do not care how big the compiled code is: the Scala standard library is several (6-7) megabytes, which added to your own code and any third-party libraries you're using, may add up to tens of megabytes, maybe a hundred or two if it's a relatively large application. Even that pales in comparison to the size of the JVM, which weighs in the 100s of megabytes.
+ @p
+ Even so, you are deploying your code on an machine (virtual or real) which has several gigabytes of memory and 100s of gigabytes of disk space. Even if the size of the code makes deployment slower, you only deploy fresh code a handful of times a day at most, and the size of your executable typically does not worry you.
+ @p
+ Scala.js is different: it runs in the browsers of your users. Before it can run in their browser, it first has to be downloaded, probably over a connection that is much slower than the one used to deploy your code to your servers or data-center. It probably is downloaded thousands of times per day, and every user which downloads it must pay the cost of waiting for it to finish downloading before they can take any actions on your website.
+
+ @p
+ A typical website loads ~100kb-1mb of Javascript, and 1mb is on the heavy side. Most Javascript libraries weigh in on the order of 50-100kb. For Scala.js to be useful in the browser, it has to be able to compare favorably with these numbers.
+
+ @hr
+
+ @p
+ Thus, while on Scala-JVM you typically have executables that (including dependencies) end up weighing 10s to 100s of megabytes, Scala.js has a much tighter budget. A hello world Scala.js application weighs in at around 100kb, and as you write more code and use more libraries (and parts of the standard library) this number rises to the 100s of kb. This isn't tiny, especially compared to the many small Javascript libraries out there, but it definitely is much smaller than what you'd be used to on the JVM.
diff --git a/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex b/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex
new file mode 100644
index 0000000..0ad886d
--- /dev/null
+++ b/book/src/main/scalatex/book/indepth/JavaAPIs.scalatex
@@ -0,0 +1,44 @@
+@p
+ Below is a list of classes from the Java Standard Library that are available from Scala.js. In general, much of @hl.scala{java.lang}, and parts of @hl.scala{java.io}, @hl.scala{java.util} and @hl.scala{java.net} have been ported over. This means that all these classes are available for use in Scala.js applications despite being part of the Java standard library.
+@p
+ There are many reasons you may want to port a Java class to Scala.js: you want to use it directly, you may be trying to port a library which uses it. In general, we haven't been porting things "for fun", and obscure classes like @hl.scala{org.omg.corba} will likely never be ported: we've been porting things as the need arises in order to support libraries (e.g. @lnk("Scala.Rx", "https://github.com/lihaoyi/scala.rx") that need them.
+
+@sect{Available Java APIs}
+
+ @ul
+ @for(data <- BookData.javaAPIs)
+ @li
+ @a(data._1, href:=data._2)
+
+@sect{Porting Java APIs}
+ @p
+ The process for making Java library classes available in Scala.js is relatively straightforward:
+ @ul
+ @li
+ Find a class that you want to use in Scala.js, but is not implemented.
+ @li
+ Write a clean-room implementation in Scala, without looking at the source code of @lnk("OpenJDK", "http://openjdk.java.net/"). This is due to legal-software-license incompatibility between OpenJDK and Scala.js. Reading the docs or specification are fine, as is looking at the source of alternate implementations such as @lnk("Harmony", "http://harmony.apache.org/")
+ @li
+ Submit a pull-request to the @lnk("Scala.js repository", "https://github.com/scala-js/scala-js"), including your implementation, together with tests. See the @lnk("existing tests", "https://github.com/scala-js/scala-js/tree/master/test-suite/src/test/scala/scala/scalajs/testsuite/javalib") in the repository if you need examples of how to write your own.
+
+ @p
+ In general, this is a simple process, for "pure-Java" classes which do not use any special JVM/Java-specific APIs. However, this will not be possible for classes which do! This means that classes that make use of Java-specific things like:
+
+ @ul
+ @li
+ Threads
+ @li
+ Filesystem APIs
+ @li
+ Network APIs
+ @li
+ @hl.scala{sun.misc.Unsafe}
+
+ @p
+ And other similar APIs will either need to be rewritten to not-use them. For example, @hl.scala{AtomicXXXs} can be written without threading/unsafe APIs because Javascript is single-threaded, making the implementation for e.g. an @hl.scala{AtomicBoolean} pretty trivial:
+
+ @hl.ref("output/scala-js/javalib/src/main/scala/java/util/concurrent/atomic/AtomicBoolean.scala")
+
+ @p
+ Others can't be ported at all (e.g. @code{java.io.File}) simply because the API capabilities they provide (blocking reads & writes to files) do not exist in the Javascript runtime.
+
diff --git a/book/src/main/scalatex/book/indepth/JavascriptInterop.scalatex b/book/src/main/scalatex/book/indepth/JavascriptInterop.scalatex
new file mode 100644
index 0000000..30404ce
--- /dev/null
+++ b/book/src/main/scalatex/book/indepth/JavascriptInterop.scalatex
@@ -0,0 +1 @@
+TODO \ No newline at end of file
diff --git a/book/src/main/scalatex/book/indepth/SemanticDifferences.scalatex b/book/src/main/scalatex/book/indepth/SemanticDifferences.scalatex
new file mode 100644
index 0000000..ebfd1db
--- /dev/null
+++ b/book/src/main/scalatex/book/indepth/SemanticDifferences.scalatex
@@ -0,0 +1,247 @@
+@import BookData._
+@p
+ Although Scala.js tries very hard to maintain compatibility with Scala-JVM, there are some parts where the two platforms differs. This can be roughly grouped into two things: differences in the libraries available,and differences in the language itself. This chapter will cover both of these facets.
+
+@sect{Language Differences}
+ @sect{Floats are Doubles}
+ @p
+ Since JavaScript doesn't have a native float type, we sometimes represent Floats using doubles/numbers, rather than with lower-precision 32-bit floats.
+ @p
+ The choice of how to represent floats is up to the implementation. You may not rely on floats providing 64-bit floating point precision.
+ @p
+ Float literals are truncated to their (binary) precision. However, output does not truncate to that precision. This can lead to the following behavior (this works as expected when using doubles):
+
+ @split
+ @half
+ @hl.scala
+ // Scala-JVM
+ > println(13.345f)
+ 13.345
+
+ @half
+ @hl.scala
+ // Scala.js
+ > println(13.345f)
+ 13.345000267028809
+
+ @sect{Int division by 0 is undefined}
+ @p
+ Unlike the JVM where dividing an integer type by 0 throws an exception, in Scala.js integer division by 0 is undefined. This allows for efficient implementation of division. Dividing a Double or Float by 0 yields positive or negative infinity as expected.
+
+ @split
+ @half
+ @hl.scala
+ // Scala-JVM
+ > 10 / 0
+ java.lang.ArithmeticException: / by zero
+ @half
+ @hl.scala
+ // Scala.js
+ > 10 / 0
+ 0
+
+ @p
+ This is a consequence of the eternal trade-off between performance and correctness, as described in the section @sect.ref{Why does error behavior differ?}
+
+ @sect{Primitive isInstanceOf tests are based on value}
+ @p
+ Instance tests (and consequently pattern matching) on any of @hl.scala{Byte}, @hl.scala{Short}, @hl.scala{Int}, @hl.scala{Float}, @hl.scala{Double} are based on the value and not the type they were created with. The following are examples:
+
+ @ul
+ @li
+ @hl.scala{1} matches @hl.scala{Byte}, @hl.scala{Short}, @hl.scala{Int}, @hl.scala{Float}, @hl.scala{Double}
+ @li
+ @hl.scala{128} (> @hl.scala{Byte.MaxValue}) matches @hl.scala{Short}, @hl.scala{Int}, @hl.scala{Float}, @hl.scala{Double}
+ @li
+ @hl.scala{32768} (> @hl.scala{Short.MaxValue}) matches @hl.scala{Int}, @hl.scala{Float}, @hl.scala{Double}
+ @li
+ @hl.scala{2147483648} (> @hl.scala{Int.MaxValue}) matches @hl.scala{Float}, @hl.scala{Double}
+ @li
+ @hl.scala{1.2} matches @hl.scala{Float}, @hl.scala{Double}
+ @p
+ As a consequence, the following apparent subtyping relationship holds:
+
+ @hl.scala
+ Byte <:< Short <:< Int <:< Float =:= Double
+
+ @sect{toString for integral Floats and Doubles}
+ @p
+ Calling toString on a Float or a Double that holds an integral value, will not append ".0" to that value:
+
+ @split
+ @half
+ @hl.scala
+ // Scala-JVM:
+ > println(1.0)
+ 1.0
+ @half
+ @hl.scala
+ // Scala.js:
+ > println(1.0)
+ 1
+ @p
+ This is due to how numeric values are represented at runtime in Scala.js: @hl.scala{Float}s and @hl.scala{Double}s are raw Javascript @hl.scala{Number}s, and their @hl.scala{toString} behavior follows from that.
+
+ @p
+ Use a formatting interpolator if you always want to show decimals:
+
+ @hl.scala
+ val x = 1.0
+ println(f"$x%.1f")
+ // Scala-JVM: 1.0
+ // Scala.js: 1.0
+
+ @sect{Unit}
+ @p
+ @hl.scala{scala.Unit} is represented using JavaScript's undefined. Therefore, calling @hl.scala{toString()} on @hl.scala{Unit} will return @hl.scala{"undefined"} rather than @hl.scala{"()"}. In practice, this shouldn't matter for most use cases.
+
+ @sect{Reflection}
+ @p
+ Java reflection and Scala reflection, are not supported. There is limited support for @lnk("java.lang.Class", "https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html"), e.g., @hl.scala{obj.getClass.getName} will work for any Scala.js object but not for objects that come from JavaScript interop (i.e. anything which @hl.scala{extends js.Object}). Reflection makes it difficult to perform the optimizations that Scala.js heavily relies on. For a more detailed discussion on this topic, take a look at the section @sect.ref{Why No Reflection?}.
+
+ @sect{Exceptions}
+ @p
+ In general, Scala.js supports exceptions, including catching them based on their type. However, exceptions that are typically triggered by the JVM have flaky semantics, in particular:
+
+ @ul
+ @li
+ @hl.scala{ArrayIndexOutOfBoundsException} is never thrown. Instead, the value becomes @hl.javascript{undefined}, which will probably propagate through your program and blow up somewhat later.
+ @li
+ @hl.scala{NullPointerException} is reported as JavaScript @hl.scala{TypeError} instead.
+ @li
+ @hl.scala{StackOverflowError} is unsupported since the underlying JavaScript exception type varies based on the browser. e.g. in Chrome the browser just hangs and kills the process/tab without any chance for the developer to catch the error.
+
+ @sect{Regular expressions}
+ @p
+ JavaScript regular expressions are slightly different from Java regular expressions. The support for regular expressions in Scala.js is implemented on top of JavaScript regexes.
+ @p
+ This sometimes has an impact on functions in the Scala library that use regular expressions themselves. A list of known functions that are affected is given here:
+ @ul
+ @li
+ @hl.scala{StringLike.split(x: Array[Char])}
+
+ @sect{Symbols}
+ @p
+ @hl.scala{scala.Symbol} is supported, but is a potential source of memory leaks in applications that make heavy use of symbols. The main reason is that JavaScript does not support weak references, causing all symbols created by Scala.js tow remain in memory throughout the lifetime of the application.
+
+ @sect{Enumerations}
+ @p
+ The methods @hl.scala{Value()} and @hl.scala{Value(i: Int)} on @lnk.scala{scala.Enumeration} use reflection to retrieve a string representation of the member name and are therefore -- in principle -- unsupported. However, since Enumerations are an integral part of the Scala library, Scala.js adds limited support for these two methods:
+ @p
+ Calls to either of these two methods of the forms:
+ @hl.scala
+ val <ident> = Value
+ val <ident> = Value(<num>)
+ @p
+ are statically rewritten to (a slightly more complicated version of):
+ @hl.scala
+ val <ident> = Value("<ident>")
+ val <ident> = Value(<num>,"<ident>")
+ @p
+ Note that this also includes calls like
+ @hl.scala
+ val A,B,C,D = Value
+ @p
+ since they are desugared into separate val definitions.
+ @p
+ Calls to either of these two methods which could not be rewritten, or calls to constructors of the protected Val class without an explicit name as parameter, will issue a warning.
+ @p
+ Note that the name rewriting honors the @hl.scala{nextName} iterator. Therefore, the full rewrite is:
+ @hl.scala
+ val <ident> = Value(
+ if (nextName != null && nextName.hasNext)
+ nextName.next()
+ else
+ "<ident>"
+ )
+ @p
+ We believe that this covers most use cases of scala.Enumeration. Please let us know if another (generalized) rewrite would make your life easier.
+
+@sect{Library Differences}
+ @val myTable = Seq(
+ ("Most of java.lang.*", "java.lang.Thread, java.lang.Runtime, ..."),
+ ("Almost all of scala.*", "scala.collection.parallel, scala.tools.nsc"),
+ ("Some of java.util.*", "org.omg.CORBA, sun.misc.*"),
+ ("Macros: uPickle, Scala-Async, Scalaxy, etc", "Reflection: Scala-Pickling, Scala-Reflect"),
+ ("Shapeless, Scalaz, Scalatags, uTest", "Scalatest, Scalate"),
+ ("XMLHttpRequest, Websockets. Localstorage", "Netty, Akka, Spray, File IO, JNI"),
+ ("HTML DOM, Canvas, WebGL", "AWT, Swing, SWT, OpenGL"),
+ ("Chipmunk.js, Hand.js, React.js, jQuery", "Guice, JUnit, Apache-Commons, log4j"),
+ ("IntelliJ, Eclipse, SBT, Chrome console, Firebug", "Scala REPL, Yourkit, VisualVM, JProfiler")
+ )
+
+ @p
+ Scala.js differs from Scala-JVM not just in the corner-cases of the language, but also in the libraries available. Scala-JVM has access to JVM APIs and the wealth of the Java libraries, while Scala.js has access to Javascript APIs and Javascript libraries. It's also possible to write pure-Scala libraries that run on both Scala.js and Scala-JVM, as detailed @a("here").
+ @p
+ This table gives a quick overview of the sorts of libraries you can and can't use when working on Scala.js:
+
+ @val tableHead = pureTable(th("Can Use"), th("Can't Use"))
+
+ @tableHead
+ @for(tuple <- myTable)
+ @tr
+ @td{@tuple._1}@td{@tuple._2}
+
+ @p
+ We'll go into each section bit by bit
+
+ @sect{Standard Library}
+ @tableHead
+ @for(tuple <- myTable.slice(0, 3))
+ @tr
+ @td{@tuple._1}@td{@tuple._2}
+
+ @p
+ You can use more-or-less the whole Scala standard library in Scala.js, sans some more esoteric components like the parallel collections or the tools. Furthermore, we've ported some subset of the Java standard library that many common Scala libraries depends on, including most of @hl.scala{java.lang.*} and some of @hl.scala{java.util.*}.
+ @p
+ There isn't a full list of standard library library APIs which are available from Scala.js, but it should be enough to give you a rough idea of what is supported. The full list of classes that have been ported to Scala.js is available under @sect.ref{Available Java APIs}
+
+ @sect{Macros v.s. Reflection}
+ @tableHead
+ @for(tuple <- myTable.slice(3, 4))
+ @tr
+ @td{@tuple._1}@td{@tuple._2}
+
+ @p
+ As described @sect.ref("Why No Reflection?", "here"), Reflection is not supported in Scala.js, due to the way it inhibits optimization. This doesn't just mean you can't use reflection yourself: many third-party libraries also use reflection, and you won't be able to use them either.
+
+ @p
+ On the other hand, Scala.js does support Macros, and macros can in many ways substitute many of the use cases that people have traditionally used reflection for (see @sect.ref("Macros", "here")). For example, instead of using a reflection-based serialization library like @lnk.github.scalaPickling, you can use a macro-based library such as @lnk.github.uPickle.
+
+ @sect{Pure-Scala v.s. Java Libraries}
+ @tableHead
+ @for(tuple <- myTable.slice(4, 5))
+ @tr
+ @td{@tuple._1}@td{@tuple._2}
+ @p
+ Scala.js has access to any pure-Scala libraries that you have cross-compiled to Scala.js, and cross-compiling a pure-Scala library with no dependencies is straightforward. Many of them, such as the ones listed above, have already been cross-compiled and can be used via their maven coordinates.
+ @p
+ You cannot use any libraries which have a Java dependency. This means libraries like @lnk.misc.ScalaTest or @lnk.misc.Scalate, which depend on a number of external Java libraries or source files, cannot be used from Scala.js. You can only use libraries which have no dependency on Java libraries or sources.
+
+ @sect{Javascript APIs v.s. JVM APIs}
+ @tableHead
+ @for(tuple <- myTable.slice(5, 7))
+ @tr
+ @td{@tuple._1}@td{@tuple._2}
+
+ @p
+ Apart from depending on Java sources, the other thing that you can't use in Scala.js are JVM-specific APIs. This means that anything which goes down to the underlying operating system, filesystem, GUI or network are unavailable in Scala.js. This makes sense when you consider that these capabilities are no provided by the browser which Scala.js runs in, and it's impossible to re-implement them ourselves.
+ @p
+ In exchange for this, Scala.js provides you access to Browser APIs that do related things. Although you can't set up a HTTP server to take in-bound requests, you can make out-bound requests using @lnk.dom.XMLHttpRequest to other servers. You can't write to the filesystem or databases directly, but you can write to the @hl.scala{dom.localStorage} provided by the browser. You can't use Swing or AWT or WebGL but instead work with the DOM and Canvas and WebGL.
+ @p
+ Naturally, none of these are an exact replacement, as the browser environment is fundamentally different from that of a desktop application running on the JVM. Nonetheless, there are many analogues, and if so desired you can write code to abstract away these differences and run on both Scala.js and Scala-JVM
+
+
+ @sect{Scala/Browser tooling v.s. Java tooling}
+ @tableHead
+ @for(tuple <- myTable.slice(7, 8))
+ @tr
+ @td{@tuple._1}@td{@tuple._2}
+
+
+ @p
+ Lastly, there is the matter of tools. Naturally, all the Scala tools which depend on the JVM are out. This means things like the @lnk("Yourkit", "http://www.yourkit.com/"), @lnk("VisualVM", "http://visualvm.java.net/") and @lnk("JProfiler", "https://www.ej-technologies.com/products/jprofiler/overview.html") profilers, as well as things like the Scala command-line REPL which relies on classloaders and other such things to run on the JVM
+ @p
+ On the other hand, you do get to keep and continue using many tools which are build for Scala but JVM-agnostic. For example, IDEs such a @lnk.misc.IntelliJ and @lnk.misc.Eclipse work great with Scala.js; from their point of view, it's just Scala, and things like code-navigation, refactoring and error-highlighting all work out of the box. SBT works with Scala.js too, and you see the same compile-erorrs in the command-line as you would in vanilla Scala, and even things like incremental compilation work un-changed.
+ @p
+ Lastly, you gain access to browser tools that don't work with normal Scala: you can use the Chrome or Firefox consoles to poke at your Scala.js application from the command line, or their profilers/debuggers. With source maps set up, you can even step-through debug your Scala.js application directly in Chrome.