无法从 Scala 中的 CrossType.Pure sbt 项目导入

Can't import from CrossType.Pure sbt project in Scala

我正在尝试在前端和一个共享项目上使用 Scala.js 制作 Play 框架项目。我的 sbt 配置是:

import sbt.Project.projectToRef

lazy val scalaV = "2.11.8"

lazy val shared = (crossProject.crossType(CrossType.Pure) in file("shared"))
  .settings(
    scalaVersion := scalaV,
    libraryDependencies ++= Seq(
      "com.mediamath" %%% "scala-json" % "1.0"
    ),
    resolvers += "mmreleases" at "https://artifactory.mediamath.com/artifactory/libs-release-global",
    addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
  )
  // set up settings specific to the JS project
  .jsConfigure(_ enablePlugins ScalaJSPlay)


lazy val sharedJVM = shared.jvm.settings(name := "sharedJVM")

lazy val sharedJS = shared.js.settings(name := "sharedJS")

lazy val root = (project in file(".")).settings(
  scalaVersion := scalaV,
  scalaJSProjects := jsProjects,
  pipelineStages := Seq(scalaJSProd, gzip),
  routesGenerator := InjectedRoutesGenerator,
  scalikejdbcSettings,
  libraryDependencies ++= Seq(
    jdbc,
    cache,
    ws,
    evolutions,
    "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
    "mysql" % "mysql-connector-java" % "5.1.39",
    "com.vmunier" % "play-scalajs-scripts_2.11" % "0.5.0"
  ),
  resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
).
  enablePlugins(PlayScala).
  aggregate(jsProjects.map(projectToRef): _*)

lazy val jsProjects = Seq(js)


lazy val js = (project in file("client")).settings(
  scalaVersion := scalaV,
  persistLauncher := true,
  persistLauncher in Test := false,
  autoCompilerPlugins := true,
  scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
  libraryDependencies ++= Seq(
    "org.scala-js" %%% "scalajs-dom" % "0.9.0",
    "com.mediamath" %%% "scala-json" % "1.0"
  ),
  resolvers += "mmreleases" at "https://artifactory.mediamath.com/artifactory/libs-release-global",
  resolvers += Resolver.sonatypeRepo("releases"),
  addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
).enablePlugins(ScalaJSPlugin, ScalaJSPlay)

一切正常,但问题是:我无法从 Scala.js 和 Play Framework 项目中的共享项目导入任何内容。这是我的共享项目结构的样子:

下面是我尝试导入它的方式:

import services.Encryptor

编译时出现错误:

not found: object services [error] import services.Encryptor

如何解决这个问题?

首先,永远不会 (!) 这样做:

lazy val sharedJVM = shared.jvm.settings(name := "sharedJVM")
lazy val sharedJS = shared.js.settings(name := "sharedJS")

这会创建由 sbt 拾取的新项目,因此交叉项目不再包含正确的项目。有关详细信息,请参阅 docs

相反,使用 jsSettingsjvmSettings:

(crossProject.crossType(CrossType.Pure) in file("shared"))
  // snip
  .jsSettings(name := "sharedJS")
  .jvmSettings(name := "sharedJVM")

lazy val sharedJVM = shared.jvm
lazy val sharedJS = shared.js

在您的构建中,您的 js 项目似乎不依赖于共享项目。因此,如果共享项目的内容当然不可用。

你需要

lazy val js = (project in file("client"))
  // snip
  .dependsOn(shared.js)