Skip to content

Inline method wrapping Scala.js dynamicImport throws "stub" exception #22162

@raquo

Description

@raquo

Compiler version

3.3.4, 3.5.2, 3.6.4-RC1-bin-20241205-c61897d-NIGHTLY

Minimized code

Full runnable project with this code and instructions on how to run scala.js + vite is available in the attached .zip archive.

Main.scala

package example

import org.scalajs.dom

import scala.annotation.nowarn
import scala.scalajs.js

@nowarn inline def dynamicImport[F](inline f: F): js.Promise[F] = {
  js.dynamicImport(f)
}

@main def main(): Unit =

  js.timers.setTimeout(2000) {
    val _ = js.dynamicImport({ dom.console.log("THIS IS js.dynamicImport") })
    val _ = dynamicImport({ dom.console.log("THIS IS local dynamicImport") }) // this throws
    // val _ = Importer.dynamicImport({ dom.console.log("THIS IS Importer.dynamicImport") }) // this throws after `sbt clean` only
  }

  dom.document.querySelector("#app").innerText = "Scala.js loaded. Open browser console to see `stub` error."

Importer.scala

package example

import scala.annotation.nowarn
import scala.scalajs.js

object Importer {

  @nowarn inline def dynamicImport[F](inline f: F): js.Promise[F] = {
    js.dynamicImport(f)
  }
}

build.sbt (extract)

    scalaJSUseMainModuleInitializer := true,
    scalaJSLinkerConfig ~= {
      _.withModuleKind(ModuleKind.ESModule)
        .withModuleSplitStyle(ModuleSplitStyle.SmallModulesFor(List("example")))
    }

Output

Browser console at runtime:

Uncaught java.lang.Error: stub
  ...
THIS IS js.dynamicImport

Expectation

Browser console at runtime:

THIS IS js.dynamicImport
THIS IS local dynamicImport

One more failure mode

In addition to the local dynamicImport never working, there is another, intermittent failure mode when the dynamicImport method is placed in a separate file (Importer.scala). Steps to reproduce that particular mode:

  • comment out the line with the local dynamicImport
  • uncomment the line with Importer.dynamicImport
  • run sbt clean
  • run the project as usual with ~fastLinkJS and npm run dev
  • you will see the same stub error in the browser console
  • add an empty // comment in Main.scala
  • after Vite reloads the page, the stub error is gone, and Importer.dynamicImport works as expected, printing out THIS IS Importer.dynamicImport
  • if you do sbt clean again, the error comes back on the next compile.
Screenshot 2024-12-07 at 11 42 05 PM

Full project

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions