Scala-js: The linker should refuse to build if @JSImport is used with NoModule

Created on 25 Oct 2016  路  8Comments  路  Source: scala-js/scala-js

I tried to import electron in my application. But build is failing to import.

Full problem is explained here :


import scala.scalajs.js
import js.annotation._
import scala.scalajs.js.annotation.JSExport

@JSExport("CloudConnectionComponent.App")
object CloudConnectionComponent extends js.JSApp {

  @ScalaJSDefined
  trait BrowserWindowOptions extends js.Object {
    def width: Double
    def height: Double
  }

  @js.native
  @JSImport("electron", "BrowserWindow")
  class BrowserWindow(options: BrowserWindowOptions) extends js.Object {
    def on(eventName: String, handler: js.Function0[Any]): Unit = js.native
    def openDevTools():Unit = js.native
    def loadUrl(url:String) :Unit = js.native
  }

  val dropboxOAuthUrl = "https://www.dropbox.com/1/oauth2/authorize?client_id=8343b03llcys1pw&response_type=token&redirect_uri=http://localhost:3000/auth/db/callback&state=433"
  var mainWindow:BrowserWindow = null

  @JSExport
  def main() = {
    mainWindow = new BrowserWindow(new BrowserWindowOptions {
      val width = 800.0
      val height = 600.0
    })

    mainWindow.loadUrl(dropboxOAuthUrl)
    mainWindow.openDevTools()
    mainWindow.on("closed", () => {
      mainWindow = null
    })
  }

}

Error :

target/scala-2.11/cloud-manager-app.js.map -> src/main/electron/cloud-manager-app.js.map
App threw an error during load
ReferenceError: $i_electron is not defined
    at ta (/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/src/main/electron/file:/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/src/main/scala/CloudConnectionComponent.scala:31:18
    )
    at T.main (/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/src/main/electron/file:/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/src/main/scala/CloudConnectionComponent.scala:29:4)
    at Object.<anonymous> (/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/src/main/electron/cloud-manager-app-launcher.js:22:50)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at loadApplicationPackage (/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/node_modules/electron-prebuilt/dist/Electron.app/Contents/Resources/default_app.asar/main.js:288:12)
    at Object.<anonymous> (/Users/rajeevprasanna/Desktop/cloudManager/CloudManagerApp/node_modules/electron-prebuilt/dist/Electron.app/Contents/Resources/default_app.asar/main.js:330:5)

http://stackoverflow.com/questions/40224123/import-electron-browser-window-in-scala-js/40225351

Project code with example is available here : https://github.com/rajeevprasanna/CloudManagerApp

bug

All 8 comments

I think you forgot to instruct Scala.js to output a CommonJS module. Add the following to your build:

scalaJSModuleKind := ModuleKind.CommonJSModule

No the setting is there. It looks like an actual bug.

@sjrd setting was missed. I made change now. seems it is working. I will close this issue once i am sure that it is working. Allow me to look up for sometime. Thanks for a very quick response

Ah I see. Well, it's still a bug that Scala.js lets you generate a .js file with that buggy configuration. It should complain earlier with a clear error message.

After adding suggested code line in build.sbt, @JSExport is not working.
Code change made :
scalaJSModuleKind := ModuleKind.CommonJSModule

Error after making this change :

target/scala-2.11/cloud-manager-app.js.map -> src/main/electron/cloud-manager-app.js.map
App threw an error during load
ReferenceError: CloudConnectionComponent is not defined
    at Object.<anonymous> (/Users/rajeevprasanna/Desktop/temp/CloudManagerApp/src/main/electron/cloud-manager-app-launcher.js:22:1)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at loadApplicationPackage (/Users/rajeevprasanna/Desktop/temp/CloudManagerApp/node_modules/electron-prebuilt/dist/Electron.app/Contents/Resources/default_app.asar/main.js:288:12)
    at Object.<anonymous> (/Users/rajeevprasanna/Desktop/temp/CloudManagerApp/node_modules/electron-prebuilt/dist/Electron.app/Contents/Resources/default_app.asar/main.js:330:5)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)

build.sbt : https://github.com/rajeevprasanna/CloudManagerApp/blob/master/build.sbt)
CloudConnectionComponent.scala

CloudConnectionComponent.scala : https://github.com/rajeevprasanna/CloudManagerApp/blob/master/src/main/scala/CloudConnectionComponent.scala

Please let me know if i am doing wrong anywhere. I am struggling to debug scala.js error messages as very less documentation is available

It seems you are somehow still using the -launcher.js file. That should not be possible; Scala.js should have refused to generate the launcher file with CommonJSModule. Launcher files do not work with CommonJS modules. Instead, you need to write your own, which requires the .js file produced by Scala.js.

@sjrd can you please point me to any example documentation/ sample code about "you need to write your own, which requires the .js file produced by Scala.js."

It's something like

require('./path/to/fastopt.js').mainpackage.MainObject().main();

There is no real documentation on this so far.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sjrd picture sjrd  路  6Comments

MasseGuillaume picture MasseGuillaume  路  7Comments

coreyoconnor picture coreyoconnor  路  6Comments

japgolly picture japgolly  路  4Comments

japgolly picture japgolly  路  5Comments