scalasbtcross-compilingscala.js

SBT classpath dependency with ScalaJS crosscompile project


I have 2 different separate projects A and B, both of which use the ScalaJS default way of wiring cross compile projects (see here: https://www.scala-js.org/doc/project/cross-build.html).

Now I want to add a SBT Classpath Dependency from B to A in a manner similar to this:

projectA.dependsOn(projectB)

where the js part of project B can use the js and shared parts of project A and the jvm part can access jvm and shared parts of project A.

As I am using upickle for serializing my data, I cannot just use a libraryDependeny with a publishLocalof project A (as upickle requires compile time information about project A in my case).

How can I solve this?


Solution

  • For CrossProject's in the same build, you can simply use .dependsOn:

    lazy val a = crossProject
    lazy val b = crossProject.dependsOn(a)
    
    // snip aJS, aJVM, bJS, bJVM
    

    Currently, there is no way to import a CrossProject from a URI / File like a RootProject. However, a CrossProject is nothing more than a two normal sbt projects. You can import these individually:

    // a.sbt
    
    lazy val a = crossProject
    lazy val aJS = a.js
    lazy val aJVM = a.jvm
    
    // b.sbt
    
    lazy val b = crossProject.
      jsConfigure(_.dependsOn(aJS)).
      jvmConfigure(_.dependsOn(aJVM))
    
    lazy val aJS = ProjectRef(file("projectA"), "aJS")
    lazy val aJVM = ProjectRef(file("projectA"), "aJVM")
    

    Note that there doesn't seem a reason we cannot create a CrossProjectRef that abstracts this away from you. So if you need this often, feel free to open an issue so we can look at it in more detail.