reactjs 集成服务器端渲染和scalajs-react

roejwanj  于 2023-01-30  发布在  React
关注(0)|答案(1)|浏览(147)
    • bounty将在15小时后过期**。回答此问题可获得+50声望奖励。jesus g_force Harris希望引起更多人关注此问题。

我一直试图遵循integrating server-side rendering in scalajs-react的指导,但我的堆栈必须有点不同,所以它不是那么超级直接。
我使用SBT 1.5.5scala 2.12.10与以下相关插件:

addSbtPlugin("com.typesafe.play"  % "sbt-plugin"                    % "2.7.4")
  addSbtPlugin("org.scala-js"       % "sbt-scalajs"                   % "1.7.0")
  addSbtPlugin("org.scala-js"       % "sbt-jsdependencies"            % "1.0.2")
  addSbtPlugin("ch.epfl.scala"      % "sbt-scalajs-bundler"           % "0.20.0")
  addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
  addSbtPlugin("org.scala-native"   % "sbt-scala-native"              % "0.3.7")
  addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject"      % "1.2.0")
  addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0")

在本文的步骤2中,它要求在"build. sbt"文件中实现以下内容:

val scalaGraalVer = "1.0.1"

  lazy val webappSsr = crossProject("webapp-ssr")

  lazy val webappSsrJs = webappSsr.js
    .dependsOn(myScalaJsWebapp) // change this to your real SJS module name(s)
    .settings(
      libraryDependencies ++= Seq(
        "com.github.japgolly.scala-graal" %%% "core-js"       % scalaGraalVer,
        "com.github.japgolly.scala-graal" %%% "ext-boopickle" % scalaGraalVer
      ),
      scalaJSLinkerConfig ~= { _.withSourceMap(false) },
      artifactPath in (Compile, fastOptJS) := (crossTarget.value / "webapp-ssr.js"),
      artifactPath in (Compile, fullOptJS) := (crossTarget.value / "webapp-ssr.js")
    )

  lazy val webappSsrJvm = webappSsr.jvm
    .settings(
      libraryDependencies ++= Seq(
        "com.github.japgolly.scala-graal" %% "core"          % scalaGraalVer,
        "com.github.japgolly.scala-graal" %% "core-js"       % scalaGraalVer,
        "com.github.japgolly.scala-graal" %% "ext-boopickle" % scalaGraalVer
      ),
      unmanagedResources in Compile += Def.taskDyn {
        val stage = (scalaJSStage in Compile in webappSsrJs).value
        val task = stageKey(stage)
        Def.task((task in Compile in webappSsrJs).value.data)
      }.value)
    )

因此,我目前有两个问题:

  1. crossProject看起来不以String作为参数,即,
    def crossProject(platforms : sbtcrossproject.Platform*)
  2. val task = stageKey(stage)-stageKey是一个无法识别的函数。我在网上搜索过,但无法弄清楚它在哪里,因此我缺少什么,或者是否有办法解决。
fae0ux8s

fae0ux8s1#

正如@tdimoff已经说过的,sbtcrossproject库的crossProject方法不接受字符串参数,所以lazy val webappSsr = crossProject("webapp-ssr")行应该替换为lazy val webappSsr = crossProject(JSPlatform, JVMPlatform)
关于stageKey函数,它看起来像是scalajs-bundler库的一部分,因此您需要添加以下库依赖项:
libraryDependencies += "ch.epfl.scala" % "scalajs-bundler" % "0.20.0"
这应该使stageKey函数可用。

相关问题