|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +post-type: blog |
| 4 | +by: Julien Richard-Foy |
| 5 | +title: "Use npm packages in Scala.js with scalajs-bundler" |
| 6 | +--- |
| 7 | + |
| 8 | +This week we release the first version of |
| 9 | +[*scalajs-bundler*](https://scalacenter.github.io/scalajs-bundler), a tool that makes it easier |
| 10 | +to use [npm](https://www.npmjs.com/) packages for [Scala.js](https://www.scala-js.org/) |
| 11 | +developers. This effort follows a Scala Center Advisory Board |
| 12 | +[proposal](https://scala.epfl.ch/minutes/2016/06/06/may-9-2016.html#proposal-scp-005-ensurance-of-continuity-of-scalajs-project): |
| 13 | +“ensurance of continuity of the Scala.js project”. |
| 14 | + |
| 15 | +## Vision |
| 16 | + |
| 17 | +Our goal is to make npm dependencies management as easy as JVM dependencies management. |
| 18 | + |
| 19 | +Basically, if your application needs to use an npm package `foo`, all you have to do is to |
| 20 | +add to your build a line like the following: |
| 21 | + |
| 22 | +~~~ scala |
| 23 | +npmDependencies in Compile += "foo" -> "1.0" |
| 24 | +~~~ |
| 25 | + |
| 26 | +And then the usual `run` and `test` sbt commands just work. |
| 27 | + |
| 28 | +## Challenge |
| 29 | + |
| 30 | +npm is the most popular JavaScript package registry. How can Scala.js applications benefit from |
| 31 | +the many libraries published on npm? First, this requires to resolve and download these libraries |
| 32 | +including their transitive dependencies. Then, the artifacts |
| 33 | +need to be *linked* with the Scala.js code. This last step is subtler than it seems |
| 34 | +because the linking process varies according to the target execution environment. For instance, |
| 35 | +Node.js expects artifacts to conform to the CommonJS format, whereas this format is not compatible |
| 36 | +with the execution from a web browser. Furthermore, in the case of web development, when the |
| 37 | +application is shipped to production it is better to pack all the code and its dependencies into a |
| 38 | +single bundle whose format is executable by web browsers. |
| 39 | + |
| 40 | +## Existing solutions |
| 41 | + |
| 42 | +There are mainly two existing approaches to tackle these challenges: using |
| 43 | +[WebJars](http://www.webjars.org/) or combining two build systems (one for the Scala |
| 44 | +world and one for the npm world). Both of them require extra efforts from developers |
| 45 | +or have limitations (you can find more details about that in scalajs-bundler’s |
| 46 | +[documentation](https://scalacenter.github.io/scalajs-bundler/motivation.html)). |
| 47 | + |
| 48 | +## Current state |
| 49 | + |
| 50 | +*scalajs-bundler* leverages the |
| 51 | +[CommonJS modules support](https://www.scala-js.org/doc/project/module.html) brought by the latest |
| 52 | +Scala.js release. |
| 53 | + |
| 54 | +The 0.1 release contains an sbt plugin that: |
| 55 | + |
| 56 | +- lets developers define their npm dependencies (as in the introductory example), |
| 57 | +- keeps track of transitive npm dependencies between Scala.js artifacts, |
| 58 | +- fetches these dependencies from the npm registry, |
| 59 | +- provides tasks to bundle the application into a single artifact executable by web browsers. |
| 60 | + |
| 61 | +The plugin uses [npm](https://www.npmjs.com/) and [webpack](https://webpack.github.io/) |
| 62 | +under the hood. |
| 63 | + |
| 64 | +We also provide a second sbt plugin that integrates with |
| 65 | +[sbt-web-scalajs](https://github.com/vmunier/sbt-web-scalajs): it basically turns bundles |
| 66 | +into sbt-web assets. |
| 67 | + |
| 68 | +## Future work |
| 69 | + |
| 70 | +There is still some work to do (in particular to shorten the duration of the bundling process |
| 71 | +in the context of live reloading workflows) and we expect to release a |
| 72 | +[0.2 version](https://github.com/scalacenter/scalajs-bundler/milestone/1) soon, but this |
| 73 | +first release is already usable, so don’t hesitate to |
| 74 | +[give it a try](https://scalacenter.github.io/scalajs-bundler/getting-started.html), we’d love |
| 75 | +to get your feedback! |
0 commit comments