简体   繁体   English

Scala.js crossProject与手动设置

[英]Scala.js crossProject vs manual setup

To compile the same source code with Scala.js and Scala JVM the documentation says to use crossProject, for instance 为了使用Scala.js和Scala JVM编译相同的源代码, 文档说明例如使用crossProject

lazy val foo = crossProject.in(file(".")).
  settings(
    name := "foo",
    version := "0.1-SNAPSHOT"
  )

lazy val fooJVM = foo.jvm
lazy val fooJS = foo.js

However it looks that the same goal can be achieved setting up the modules manually 但是,看起来可以实现相同的目标,手动设置模块

lazy val fooSettings = Seq(
  name := "foo",
  version := "0.1-SNAPSHOT",
  scalaSource in Compile := baseDirectory.value / ".." / "shared" / "src" / "main" / "scala"
)

lazy val fooJVM = project.in(file("jvm"))
  .settings(fooSettings: _*)

lazy val fooJS = project.in(file("js"))
  .settings(fooSettings: _*)
  .enablePlugins(ScalaJSPlugin)

Does crossProject do something important or it's just more convenient way to setup stuff? crossProject做重要的事情还是只是更方便的设置方法?

It looks like, in your manual setup, you have separate jvm and js subdirectories under your "shared" area -- it doesn't look like they're the same code at all. 看起来,在您的手动设置中,“共享”区域下有单独的 jvm和js子目录-看起来它们根本不是相同的代码。

CrossProject is about letting you have a single directory structure, with the same code, that is compiled for both sides. CrossProject是关于让你有一个单一的目录结构, 相同的代码,被编译为双方 Possibly with some small subdirectories that are specific to one side or another, but in general the focus is that most of the code is shared -- the jvm and js subdirectories, if present (it depends on the mode), are usually just shims to adapt the common code to those sides. 可能有一些特定于一侧或另一侧的小子目录,但通常重点是共享大多数代码-jvm和js子目录(如果存在)(取决于模式)通常只是填充使通用代码适应这些方面。

All that said, it is really just to make it more convenient -- I believe you could achieve the same results without CrossProject. 所有这一切说,这真的只是为了更方便-我相信你可以实现无CrossProject相同的结果。 But in the common case where you're sharing a lot of code, it's significantly more convenient... 但是在通常情况下,如果您共享许多代码,它会更加方便...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM