【发布时间】:2017-01-30 02:40:29
【问题描述】:
我正在尝试构建一个使用大量 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>
现在,这 不 工作 如果 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-setting ,则会弹出一个带有“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
)
但是在编译过程中我得到:
./fastopt-launcher.js 中的错误 [信息]找不到模块:错误:无法解析/home/中的“文件”或“目录”/home/sorona/scalajstestbed/modules/client/target/scala-2.12/scalajs-bundler/main/client-fastopt.js sorona/scalajstestbed/modules/client/target/scala-2.12/scalajs-bundler/main
编辑:解决方案是包含client-fastopt-bundle.js et voila
【问题讨论】:
标签: scala.js