1

I set up a very simple scala.js project with this very simple "application":

package example

import org.scalajs.dom._

import scala.scalajs.js.JSApp

class EverythingWorks extends JSApp {

  def main() = {
    console.log("It works!")
  }
}

The corresponding HTML looks the following:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript">
    example.EverythingWorks().main();
</script>
</body>
</html>

Now, I did sbt fastOptJS, copied the generated JS file to js/app.js and the js.map file next to app.js.

Instead of printing "It works!" on the console, I get ReferenceError: example is not defined. I double-checked that app.js is found from the HTML.

What am I missing?

2 Answers 2

1

Your main class should be object, not class.

Sign up to request clarification or add additional context in comments.

Comments

0

The ScalaJS build process does "dead code elimination." This reduces the size of the build dramatically. However, unless you tell it that some of your top level stuff is NOT dead code, it will be eliminated.

That means you have to use the @JSExport annotation to tell ScalaJS not to remove your top level methods and objects.

From this introduction, the example code:

@JSExport
object ScalaJSExample {
  @JSExport
  def main(canvas: html.Canvas): Unit = {

1 Comment

This solved it for me, I could not get it to work with JSApp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.