2

我尝试制作一个简单的scalajs + scalajs-bootstrap应用程序,但是关于如何设置骨架的文档很少。scalajs-bootstrap 项目有一个示例,但是 build.sbt 文件非常大,它包含 scalajs-bootstrap 的源代码和示例本身。

我尝试仅使用 bootstrap4 示例创建一个新项目,但是当我在浏览器上打开 index.html 页面时,它会抱怨

Uncaught ReferenceError: exports is not defined at scalajsenv.js:29

这是我目前的设置:

项目/plugins.sbt

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.20")
addSbtPlugin("com.github.karasiq" % "sbt-scalajs-bundler" % "1.2.1")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.0")

构建.sbt

enablePlugins(ScalaJSBundlerPlugin)

scalaVersion := "2.12.6"
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.7"
libraryDependencies += "com.github.karasiq" %%% "scalajs-bootstrap-v4" % "2.3.5"

npmDependencies in Compile += "jquery" -> "3.2.1"
npmDependencies in Compile += "bootstrap" -> "4.1.1"

mainClass in Compile := Some("com.karasiq.bootstrap4.test.frontend.BootstrapTestApp")
scalaJSUseMainModuleInitializer := true

scalaJSModuleKind := ModuleKind.CommonJSModule
version in webpack := "4.28.4"
emitSourceMaps := false

这是 scalajs-bootstrap4 libray 的源代码和示例: https ://github.com/Karasiq/scalajs-bootstrap

这是我完整的最小示例,失败并出现exports is not defined错误: https ://github.com/dportabella/scalajs-bootstrap4-examples

如何修改 plugins.sbt 和/或 build.sbt 以使这个项目工作?


解决方案

项目/plugins.sbt

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.28")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.0")

构建.sbt

enablePlugins(ScalaJSBundlerPlugin)

scalaVersion := "2.12.8"
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.7"
libraryDependencies += "com.github.karasiq" %%% "scalajs-bootstrap-v4" % "2.3.5"

npmDependencies in Compile += "jquery" -> "3.2.1"
npmDependencies in Compile += "bootstrap" -> "4.1.1"

mainClass in Compile := Some("com.karasiq.bootstrap4.test.frontend.BootstrapTestApp")
scalaJSUseMainModuleInitializer := true

scalaJSModuleKind := ModuleKind.CommonJSModule
version in webpack := "4.28.4"
emitSourceMaps := false

完整示例: https ://github.com/dportabella/scalajs-bootstrap4-examples

4

2 回答 2

1

对于像我这样使用sbt-web-scalajs 的人,请参阅:
https ://scalacenter.github.io/scalajs-bundler/getting-started.html#sbt-web

于 2019-12-17T20:28:30.783 回答
0

根据我的经验,您需要:

  • 将以下行添加到您的project/plugins.sbt:
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.1")
  • 启用两者ScalaJSPluginScalaJSBundlerPlugin在您的build.sbt
  • 将以下配置添加到您的build.sbt
scalaJSUseMainModuleInitializer := true
scalaJSModuleKind := ModuleKind.CommonJSModule
version in webpack := "4.28.4"
emitSourceMaps := false

最后,运行fastOptJS::webpack创建打包好的 JS 文件。

注意打包的JS文件需要在index.html-不是编译的JS文件中引用!

我希望这有帮助。

于 2019-07-25T12:48:15.277 回答