You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/project/building.md
+27-16Lines changed: 27 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,18 +11,12 @@ This will generate `.sjsir` and `.class` files for each class in your project (j
11
11
12
12
The `.sjsir` files are an internal representation which can be linked to actual JavaScript code as will be explained shortly.
13
13
14
-
## Produce one JavaScript file
15
-
16
-
To produce a proper JavaScript file from your code, you need to call the linker:
17
-
18
-
sbt> fastOptJS
19
-
20
-
This will perform fast Scala.js-specific optimizations and write the resulting code to a single JavaScript file. You can now use this JavaScript file in your HTML page or in whatever way you like. The resulting file in the target folder will have the suffix `-fastopt.js`.
21
-
22
14
## Actually *do* something
23
15
24
-
By default, Scala.js produces "libraries", that do not actually *do* anything when their `-fastopt.js` file is loaded.
25
-
To make it do something, you need a top-level object with a `main` method:
16
+
To produce JavaScript code, we need to either export something (e.g. a method or a class) or do something when the JavaScript code is loaded.
17
+
Otherwise, Scala.js will not produce any JavaScript code (because to do nothing, there is no need for any code).
18
+
19
+
To do something when your code is loaded, you need a top-level object with a `main` method (to export something, see [Export Scala.js APIs to JavaScript]({{ site.production_url }}/doc/interoperability/export-to-javascript.html)).
Just like in a JVM project, sbt will automatically detect the object with a `main(Array[String]): Unit` method, and use it as the main method of the application.
42
-
Now, the .js file produced by `fastOptJS` will print `"Hello world!"`.
43
36
44
37
Note that this will require that there is a *unique* such object or that the one to use be explicitly set with `mainClass in Compile := Some(<name>)`.
45
38
If you explicitly set `mainClass`, note that it needs to be set on a per-configuration basis (i.e. the part `in Compile` is essential, otherwise the setting will be ignored). For further information see the Stack Overflow entry ['How to set mainClass in ScalaJS build.sbt?'](http://stackoverflow.com/questions/34965072/how-to-set-mainclass-in-scalajs-build-sbt) (specific to Scala.js) and the Stack Overflow entry ['How to set main class in build?'](http://stackoverflow.com/questions/6467423/how-to-set-main-class-in-build) (not specific to Scala.js).
@@ -49,14 +42,29 @@ Since 0.6.18, any object with a standard `main` method will be recognized.
49
42
`js.JSApp` is now deprecated.
50
43
See [the Scaladoc of `js.JSApp`]({{ site.production_url }}/api/scalajs-library/0.6.20/#scala.scalajs.js.JSApp) for migration tips.
51
44
45
+
## Produce JavaScript code
46
+
47
+
To produce JavaScript code from your Scala code, you need to call the linker:
48
+
49
+
sbt> fastLinkJS
50
+
51
+
This will perform fast Scala.js-specific optimizations and write the resulting JavaScript code to a directory.
52
+
With the default options, it will write a single file `main.js`.
53
+
You can now use this JavaScript file in your HTML page or in whatever way you like.
54
+
The resulting directory in the target folder will have the suffix `-fastopt`.
55
+
56
+
Loading the `main.js` file produced by `fastLinkJS` will print `"Hello world!"`.
57
+
58
+
**Note for Scala.js 1.2.x and earlier:** in Scala.js 1.2.x and earlier, we used `fastOptJS` instead of `fastLinkJS`, which always produces a single file with the suffix `-fastopt.js` directly in the target directory.
59
+
52
60
## Running in the console
53
61
54
62
You can run a Scala.js application (that has `scalaJSUseMainModuleInitializer` set to `true`) by using the `run` task:
55
63
56
64
sbt> run
57
65
58
-
This will run the `-fastopt.js` file right inside of your sbt console.
59
-
By default, the file is run with [Node.js](http://nodejs.org/), which you need to install separately.
66
+
This will run the `main.js` file right inside of your sbt console.
67
+
By default, the file is run with [Node.js](https://nodejs.org/), which you need to install separately.
60
68
61
69
**Scala.js 0.6.x only:** If your application or one of its libraries requires a DOM (which can be specified with `jsDependencies += RuntimeDOM`), you will also need to install [`jsdom`](https://github.com/jsdom/jsdom) with `npm install jsdom`.
62
70
`jsDependencies += RuntimeDOM` is now deprecated, and should be replaced by `jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()`.
@@ -86,16 +94,19 @@ See [the Scaladoc of `StandardLinker.Config`]({{ site.production_url }}/api/scal
86
94
87
95
To make the resulting JavaScript even smaller (and usually faster as well), the sbt plugin integrates the Google Closure Compiler under the so-called full-optimizations. You can use them by issuing:
88
96
89
-
sbt> fullOptJS
97
+
sbt> fullLinkJS
90
98
91
-
This will produce another single JavaScript file that is fully optimized.
99
+
This will produce a `main.js`file that is fully optimized in another directory.
92
100
Note that this can take a while and is therefore not recommended in the development cycle.
93
-
The resulting file in the target folder will have the suffix `-opt.js`.
101
+
The resulting directory in the target folder will have the suffix `-opt`.
94
102
95
103
You can run your code and tests in fullOpt stage with the following command:
96
104
97
105
sbt> set scalaJSStage in Global := FullOptStage
98
106
107
+
**Note for Scala.js 1.2.x and earlier:** in Scala.js 1.2.x and earlier, we used `fullOptJS` instead of `fullLinkJS`, which always produces a single file with the suffix `-opt.js`.
108
+
`scalaJSStage` works the same way in Scala.js 1.2.x and in later versions.
Copy file name to clipboardExpand all lines: doc/project/linking-errors.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,17 @@ layout: doc
3
3
title: Linking Errors
4
4
---
5
5
6
-
When linking a Scala.js application, either directly through `fastOptJS`/`fullOptJS` or indirectly through `run` or `test`, Scala.js can sometimes report *linking errors*.
6
+
When linking a Scala.js application, either directly through `fastLinkJS`/`fullLinkJS` (`fastOptJS`/`fullOptJS` up to Scala.js 1.2.x) or indirectly through `run` or `test`, Scala.js can sometimes report *linking errors*.
7
7
They look like the following:
8
8
9
9
```
10
-
[info] Fast optimizing .../helloworld/target/scala-2.12/helloworld-fastopt.js
10
+
[info] Fast optimizing .../helloworld/target/scala-2.12/helloworld-fastopt
11
11
[error] Referring to non-existent method scala.concurrent.impl.Promise$CompletionLatch.releaseShared(scala.Int)scala.Boolean
12
12
[error] called from scala.concurrent.impl.Promise$CompletionLatch.apply(scala.util.Try)scala.Unit
13
13
[error] called from scala.concurrent.impl.Promise$CompletionLatch.apply(java.lang.Object)java.lang.Object
14
14
[error] ...
15
15
[error] There were linking errors
16
-
[error] (helloworld/compile:fastOptJS) There were linking errors
16
+
[error] (helloworld/compile:fastLinkJS) There were linking errors
Copy file name to clipboardExpand all lines: doc/project/testing.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,9 +75,9 @@ A list of testing frameworks compatible with Scala.js can be found [here](../../
75
75
76
76
Note: Don't forget to mark a test framework SBT dependency as `test,it` if you have both unit and integration tests.
77
77
78
-
## Testing over `fullOptJS`-generated files
78
+
## Testing over `fullLinkJS`-generated files
79
79
80
-
By default, tests runs over `fastOptJS`-built JS files since their build time are shorter than `fullOptJS`.
80
+
By default, tests run over `fastLinkJS`-built (resp. `fastOptJS` up to Scala.js 1.2.x) JS files since their build time is shorter than `fullLinkJS` (resp. `fullLinkJS`).
81
81
82
-
If you want to run tests over `fullOptJS`-build JS files for some reason, run `set scalaJSStage in Global := FullOptStage` before test.
82
+
If you want to run tests over `fullLinkJS`-build JS files for some reason, run `set scalaJSStage in Global := FullOptStage` before test.
83
83
This increases test time significantly, and omit checks for undefined behavior, so not recommended in default build settings. Instead, consider run test both in `FastOptStage` and `FullOptStage` in CI.
0 commit comments