scalajs-bundler icon indicating copy to clipboard operation
scalajs-bundler copied to clipboard

JSExportTopLevel doesn't work?

Open ryan-williams opened this issue 7 years ago • 1 comments

Maybe I'm missing something obvious, but I can't get top-level exports to work under scalajs-bundler.

(Full repro here)

I have one source file, HelloWorld.scala:

import scala.scalajs.js.annotation.JSExportTopLevel
@JSExportTopLevel("HelloWorld")
object HelloWorld {
  println("yay")
  println(this)
  def hello(): Unit = {
    println("hello")
  }
}

build.sbt:

name := "hello"
scalaVersion := "2.12.4"
enablePlugins(
  ScalaJSPlugin,
  ScalaJSBundlerPlugin
)
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.1"

project/plugins.sbt:

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.21")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.9.0")

project/build.properties:

sbt.version=1.1.0¨

index.html:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="./target/scala-2.12/scalajs-bundler/main/hello-fastopt-bundle.js"></script>
</body>
</html>

I run fastOptJS::webpack, then open the html page (as file://) in my browser, and see the two print statements in the console:

yay
HelloWorld$@1

But referencing HelloWorld gives:

Uncaught ReferenceError: HelloWorld is not defined
    at <anonymous>:1:1

It seems like the object is getting renamed or obfuscated by webpack in some way?

If I don't enable ScalaJSBundlerPlugin, do regular fastOptJS, and point my index.html at that file, I get a top-level HelloWorld JS object as expected.

Sorry if I'm missing something obvious.

ryan-williams avatar Jan 24 '18 15:01 ryan-williams

I think you need this: https://scalacenter.github.io/scalajs-bundler/cookbook.html#several-entry-points

sjrd avatar Jan 26 '18 16:01 sjrd