Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

JSExportTopLevel doesn't work? #205

Open
ryan-williams opened this issue Jan 24, 2018 · 1 comment
Open

JSExportTopLevel doesn't work? #205

ryan-williams opened this issue Jan 24, 2018 · 1 comment

Comments

@ryan-williams
Copy link

ryan-williams commented Jan 24, 2018

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants