-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Labels
Description
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
andnpm 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, andImporter.dynamicImport
works as expected, printing outTHIS IS Importer.dynamicImport
- if you do
sbt clean
again, the error comes back on the next compile.
