Skip to content

solugo/gradle-nodejs-plugin

Repository files navigation

License Version

This plugin allows to use Node.js script and tools via gradle. NodeJs will be downloaded automatically and reused across execution.

Configuration

plugins {
    id("de.solugo.nodejs") version "..."
}

nodejs {
    version.set("...") // default: "18.16.0"
    cachePath.set("...") // default: "~/.gradle/nodejs"
    rootPath.set("...") // default: projectDir
}

Task rules

Node

Run node script

gradle node<ScriptName> -Pargs="..." // node <script-name>.js <args>

Npm

Run npm script

gradle npm<TaskName> -Pargs="..." // npm <task-name> <args>

Yarn

Run yarn task

gradle yarn<TaskName> -Pargs="..." // yarn <task-name> <args>

Npx

Run node module

gradle npx<ModuleName> -Pargs="..." // npx <module-name>.js <args>

Examples

Install development dependency using npm

gradle npmInstall -Pargs="--save-dev react"

Build react frontend

nodejs {
    rootPath("./frontend")
}

tasks.create("buildFrontend") {
    doFirst {
        nodejs.execNpm("ci")
        nodejs.execNpm("run", "build")
    }
}

Execute test for a specific nodejs package

tasks.create("testPackage") {
    doFirst {
        nodejs.exec {
            workingDir("../package")
            args(resolveBinary("npm"), "run", "test")
        }
    }
}