Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Workaround to depend on the browser window object while using the LibraryAndApplication bundling mode #355

Open
AlexITC opened this issue Jul 13, 2020 · 1 comment

Comments

@AlexITC
Copy link

AlexITC commented Jul 13, 2020

I have a library built with ScalaJs, which I'm trying to get running on the iOS JsContext, the problem that I have found is that the @JSExportTopLevel annotation takes no effect unless:

  • I follow the instructions for the LibraryOnly bundling mode, which depend on the browser window.
  • I set the LibraryAndApplication bundling mode which depends on the window.

These approaches don't work because iOS doesn't know about such window object, there are previous discussions about removing such window dependency to get this working for node environments (like #205 , #250 )

I have tried to run scalajs generated code without problems, I can likely get every library included manually and generate the JavaScript without the bundler, but it's a bit complex to test and to maintain.

I wonder if there is a workaround to get rid of this window dependency, the docs suggest to add the following code, hopefully there is something similar that doesn't depend on the window:

var exports = window;
exports.require = window["ScalaJSBundlerLibrary"].require;

Thanks!

@AlexITC
Copy link
Author

AlexITC commented Jul 15, 2020

As an update, I got the scalajs code produced by the bundler running on iOS by using a global, and the Application bundling mode, this is done by running some code on the main entrypoint, the requirement is to run var Sdk = {}; on iOS before loading the scalajs script.

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobalScope

@js.native
@JSGlobalScope
object JsGlobals extends js.Object {
  var Sdk: js.Dictionary[_] = js.native
}

object JsSdk {

  def main(args: Array[String]): Unit = {
    if (js.typeOf(JsGlobals.Sdk) != "undefined") {
      JsGlobals.Sdk = js.Object.fromEntries(
        js.Array(
          "hello" -> js.Any.fromFunction1(hello)
        )
      )
    } else {
      // This means we are out of the iOS environment, where the global is not needed
    }
  }

  def hello(name: String): String = {
    s"Hello $name"
  }
}

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

No branches or pull requests

1 participant