A first solution is to use WebJars. WebJars wrap .js files into .jar archives published on maven central, so that Scala developers can depend on them just like they depend on JVM libraries. However, WebJars have limitations.
First, npm packages are not automatically available as WebJars as soon as they are published on the npm registry. So, WebJars users have to explicitly publish these npm packages as .jar packages. This is just a matter of filling and submitting a form with the name and version of the package, but, still, this is one extra step.
Second, the tool that re-publishes npm packages as .jar packages does not keep track of
It means that when your program depends on a library
foo that itself depends on a library
then you have to explicitly convert both and depend on both in your program. This situation
might turn into a dependency management hell.
Another solution consists in having two build systems: one for the Scala world and one for the npm world. At some point, the npm build writes files consumed by the Scala application.
Typically, the npm build defines npm dependencies and bundles them into a single .js
file suitable for web browser consumption and that the Scala.js program can depend
on (e.g. using
This approach works well and allows developers to use whatever tools they want to process the npm dependencies.
However, having two build systems is not really an integrated developer experience: developers have to setup two builds, run two shells, and manually take care of the relationship between the two builds.
scalajs-bundler aims to provide an integrated solution to work with npm packages from Scala.js projects. It lets developers define their npm dependencies from within their sbt build, fetches them (using npm itself), and bundles them with the output of the Scala.js compilation. The result is a single .js file suitable for web browser consumption.