fiatjaf / sbt-esbuild   0.1.1

GitHub

transpile and bundle scala-js packages with esbuild

Scala versions: 2.12
sbt plugins: 1.0

sbt-esbuild Maven Central

A very simple plugin that bundles your generated ScalaJS code with its JavaScript dependencies and the JavaScript dependencies of its dependencies.

Uses sbt-npm-dependencies to gather the list of the dependencies, installs them with npm, yarn or bun and them bundles everything with esbuild.

Installation

Add this to your project/plugins.sbt:

addSbtPlugin("com.fiatjaf" %% "sbt-esbuild" % "0.1.1")

Usage

In your code, when writing facades, use @JSImport to refer to the JavaScript modules you're importing (they will get translated to require() or import calls and later transpiled by esbuild).

In your build.sbt, enable the plugin and include your dependencies:

-enablePlugins(ScalaJSPlugin)
+enablePlugins(ScalaJSPlugin, EsbuildPlugin)

+Compile / npmDependencies ++= Seq(
+  "left-pad" -> "latest",
+  "garbage" -> "0.0.0"
+)
+
+esPackageManager = Yarn

If you are importing another Scala library that has declared npmDependencies using sbt-npm-dependencies (for example, scoin) these dependencies will all be fetched automatically and the @JSImport annotations from that library will all work.

On sbt, call fastLinkJS / esBuild to install dependencies, build and bundle. Or ~fastLinkJS / esBuild to build continuously. The resulting JS bundle will be written to target/esbuild/bundle.js (same with fullLinkJS).

You can also:

  • set esPackageManager to Npm, Yarn or Bun for what to use when installing dependencies (defaults to Npm);
  • call esInstall to just install the JS modules; or
  • set esBuildOptions (defaults to Seq("--sourcemap")) to pass extra options to esbuild (it is great since most things work either out-of-the-box or as command-line arguments with no need for bloated configuration files).