14

I'm new to Scala.js. I'd like to use the Argonaut json library.

https://github.com/argonaut-io/argonaut

Its only dependencies appear to be Monocle and Scalaz which both have versions compiled for Scala.js. I'd be happy to work on porting Argonaut to Scala.js, but don't have a firm idea on how to begin. Does anyone have any pointers?

Thanks.

seanmcl
  • 9,740
  • 3
  • 39
  • 45

1 Answers1

26

Quick proof-of-concept

The first thing to try is to convert the build so that the JVM projects become Scala.js projects. The basis for this is pretty easy:

In project/plugins.sbt, add the dependency to the Scala.js sbt plugin:

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.2")

In build.sbt or project/Build.scala (or similar, depending on what the given project uses), turn projects into Scala.js projects by adding:

.enablePlugins(ScalaJSPlugin)

to their definitions. For their dependencies, replace %% dependencies by %%% dependencies to depend on the Scala.js artifacts.

At this point, the code can be compiled, and can probably be used to write examples or to directly in your application. Complex builds can require more work.

If everything works fine in your application, then you've made a successful proof-of-concept that this library can be ported to Scala.js.

Going further: cross-compiling build

OK, so now that you have a quick proof-of-concept that the library can compile and work on Scala.js, you'll want to make a proper cross-compiling build instead of the quick fork. Indeed, now the build does not produce JVM artifacts anymore.

For this, you will need to re-transform all projects that need to cross-compiled into crossProjects. For this, I recommend the cross-building documentation page as a source for further documentation.

erip
  • 16,374
  • 11
  • 66
  • 121
sjrd
  • 21,805
  • 2
  • 61
  • 91
  • 1
    So basically you need to have access to the scala project to port it to scalajs, correct me if I am wrong? What if I have access to this project and I could port it to a scalajs project but then it has dependencies which are not already ported to scala.js?? Please explain. – iamsmkr Feb 13 '19 at 11:49
  • 1
    Then you need to transitively port all dependencies. – sjrd Feb 13 '19 at 15:19
  • Hmm, alright. Isn't there any way around that; e.g. depending on the sources of a project to make it a scalajs project (without changing the project's configuration)? – 6infinity8 Feb 28 '22 at 10:36
  • Sure, you could depend on the source in some way. You'll have to re manage the transitive dependencies, but that's doable. – sjrd Mar 01 '22 at 22:52