Building Assets for a Spring Application with Gradle, Yarn, esbuild, and Sass

Learn how to efficiently build and manage frontend assets in a Spring application using Gradle, Yarn, esbuild, and Sass, with streamlined configuration for SCSS and JavaScript bundling.

Managing frontend assets in a Spring application requires a robust setup to handle JavaScript and CSS efficiently. In this article, I’ll share how I configured a Spring project to build assets using Gradle, Yarn, esbuild, and Sass, ensuring a streamlined development and production workflow.

Project Setup

The foundation of the asset pipeline is a Spring Boot application built with Gradle using the Kotlin DSL (build.gradle.kts). The clue is the Gradle Plugin for Node this plugin enables us to use a lot of Node.js-based technologies as part of your build without having Node.js installed locally on our system.

Key plugins in build.gradle.kts enable this setup:

  • org.springframework.boot for Spring Boot.
  • com.github.node-gradle.node for Yarn integration.

Asset Pipeline Configuration

Yarn and Node Setup

The node block in build.gradle.kts enables Yarn integration by downloading Node.js automatically. Yarn manages frontend dependencies defined in package.json, including esbuild for JavaScript bundling, sass for SCSS compilation, and modern-normalize for consistent CSS resets.

{
  "name": "frostyfab",
  "version": "0.1.0",
  "license": "UNLICENSED",
  "dependencies": {
    "modern-normalize": "^2.0.0"
  },
  "devDependencies": {
    "esbuild": "^0.25.2",
    "sass": "^1.69.3"
  },
  "scripts": {
    "sass": "sass --load-path=node_modules ./src/main/resources/static/sass/index.scss",
    "esbuild": "esbuild ./src/main/resources/js/app.js"
  }
}

The scripts section defines commands for compiling SCSS (index.scss) and bundling JavaScript (app.js).

Gradle Tasks for Asset Building

Two custom Gradle tasks, sass and esbuild, handle asset compilation:

  • Sass Task: Compiles SCSS files from src/main/resources/static/sass into build/sass/static/style.css. The task depends on npmInstall to ensure Yarn dependencies are available. The --load-path=node_modules flag allows Sass to resolve imports like modern-normalize.

  • esbuild Task: Bundles JavaScript from src/main/resources/js/app.js into build/esbuild/static/. It also depends on npmInstall for consistency.

Both tasks use Gradle's YarnTask to execute Yarn scripts. To avoid errors when node_modules is missing (e.g., in a fresh build), the inputsYarnConfig function conditionally includes node_modules as an input only if it exists, alongside other inputs like package.json and source directories.

val sassTask = tasks.register<YarnTask>("sass") {
    val outputDir = layout.buildDirectory.dir("sass").get().asFile
    val sassOut = outputDir.resolve("static/style.css").path
    dependsOn(tasks.npmInstall)
    yarnCommand.set(listOf("run", "sass", sassOut))
    inputsYarnConfig(inputs)
    outputs.dirs(outputDir)
}

val esbuildTask = tasks.register<YarnTask>("esbuild") {
    val outputDir = layout.buildDirectory.dir("esbuild").get().asFile
    val esbuildOut = outputDir.resolve("static/").path
    dependsOn(tasks.npmInstall)
    yarnCommand.set(listOf("run", "esbuild", "--outdir=$esbuildOut"))
    inputsYarnConfig(inputs)
    outputs.dirs(outputDir)
}

fun inputsYarnConfig(inputs: TaskInputs) {
    if (project.file("node_modules").exists()) {
        inputs.dir("node_modules")
    }
    inputs.dir("src/main/resources/static/sass")
    inputs.file("package.json")
    inputs.file("yarn.lock")
}

Integrating Assets into the Build

The compiled assets are included in the Spring Boot application via the sourceSets configuration. The sassTask and esbuildTask output directories are added as resource directories, making the generated style.css and JavaScript files available under src/main/resources/static/. This ensures Spring serves them as static resources.

sourceSets {
    java {
        main {
            resources {
                srcDir(sassTask)
                srcDir(esbuildTask)
            }
        }
    }
}

Build and Development Workflow

Running ./gradlew build triggers npmInstall, followed by the sass and esbuild tasks, compiling assets and including them in the final JAR. For development, Spring Boot’s devtools enable hot reloading, though asset changes require re-running the sass or esbuild tasks. This can be done continuously with gradle :assemble --continuous, allowing for rapid iteration on frontend code.

Key Takeaways

This setup leverages Gradle’s flexibility to integrate Yarn, esbuild, and Sass into a Spring Boot project, creating a modern asset pipeline. Esbuild ensures fast JavaScript bundling, while Sass provides powerful CSS preprocessing. The conditional input handling avoids common build errors, and the sourceSets configuration seamlessly integrates assets into the application. This approach balances developer experience with production reliability, making it suitable for both small and large-scale Spring projects.