sscalaJSModuleKind := ModuleKind.CommonJSModule - 无法再调用 main 方法 :(

sscalaJSModuleKind := ModuleKind.CommonJSModule - cannot invoke main method anymore :(

我正在尝试构建一个使用大量 JSImport 语句的新外观。我想把它放在我目前正在做的项目的子文件夹中,以便在做的时候改进它。

在我的根 build.sbt 之前 scala.js 部分看起来像这样:

lazy val client = (project in file("modules/client"))
      .enablePlugins(ScalaJSPlugin, ScalaJSWeb)
      .settings(generalSettings: _*)
      .settings(
        name := "client",
        libraryDependencies += CrossDependencies.scalaTags,
        persistLauncher := true
      )

现在我添加了这个:scalaJSModuleKind := ModuleKind.CommonJSModule,它与 persistLauncher 设置不兼容,所以我删除了 persistLauncher := true

当然,在我看来,我不能再只添加 client-launcher.js。所以我尝试手动包装主方法调用,如下所示:

<script type="text/javascript">
    tld.test.Test().main()
</script>

现在,这 NOT 工作 IF scalaJSModuleKind := ModuleKind.CommonJSModule 被添加到我的 build.sbt。如果我删除该设置,一切正常。

这是我的测试

package tld.test

import org.scalajs.dom
import scala.scalajs.js.JSApp

object Test extends JSApp
{
  import scalatags.JsDom.all._

  def main(): Unit =
  {
    // Add js script dynamically
    val s = script(
      "alert('Hello World!')"
    )
    dom.document.getElementsByTagName("head")(0).appendChild(s.render)
  }
}

现在,如果我删除 ModuleKind 设置,则会弹出一个带有 'Hello World' 的警报,但如果没有,则不会。是什么原因造成的,我该如何预防?


编辑 在@sjrd 回答后,我尝试了以下操作: plugins.sbt:

addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.5.0")
addSbtPlugin("ch.epfl.scala" % "sbt-web-scalajs-bundler" % "0.5.0")

build.sbt:

lazy val client = (project in file("modules/client"))
  .enablePlugins(ScalaJSBundlerPlugin, ScalaJSWeb) // ScalaJSBundlerPlugin automatically enables ScalaJSPlugin
  .settings(generalSettings: _*)
  .settings(
    name := "client"
    , libraryDependencies += CrossDependencies.scalaTags
    //, scalaJSModuleKind := ModuleKind.CommonJSModule // ScalaJSBundlerPlugin implicitly sets moduleKind to CommonJSModule enables ScalaJSPlugin
  )

lazy val server = (project in file("modules/server"))
  .enablePlugins(PlayScala, WebScalaJSBundlerPlugin)
  .settings(generalSettings: _*)
  .settings(
    name := "server"
    ,libraryDependencies ++= Seq(
      CrossDependencies.scalaTest,
      CrossDependencies.scalactic,
      CrossDependencies.scalaTags,
      "com.typesafe.play" %% "play-json" % "2.6.0-M1")
    ,scalaJSProjects := Seq(client)
    ,pipelineStages in Assets := Seq(scalaJSPipeline)
    //,pipelineStages := Seq(digest, gzip)
    ,compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value
  )

但是在编译过程中我得到:

ERROR in ./fastopt-launcher.js [info] Module not found: Error: Cannot resolve 'file' or 'directory' /home/sorona/scalajstestbed/modules/client/target/scala-2.12/scalajs-bundler/main/client-fastopt.js in /home/sorona/scalajstestbed/modules/client/target/scala-2.12/scalajs-bundler/main


edit:解决方案是然后包含 client-fastopt-bundle.js 等瞧

更改模块种类会显着改变输出文件的形状,包括其外部 "specification"。特别是,它不再是可以嵌入到网页中的脚本。相反,它是一个 CommonJS 模块。

为了能够将其包含在网页中,您需要将其捆绑。最好的方法是使用 scalajs-bundler.