Integrate typescript compilation in a gradle project.
The plugin allow to compile typescript files in a gradle project, producing javascript files from the typescript ones. Also, it can be used to run directly the typescript file.
In addition, the plugin let you manage the node installation. You can pick the version of node to install and the plugin will download it for you.
Just apply it to your project:
plugins {
id("io.github.zucchero-sintattico.typescript-gradle-plugin") version "<version>"
}
Note: you should create a
package.json
andtsconfig.json
in the root of your project with your desired configuration.
During the compilation, will run the command npm run build
:
typescript {
buildCommandExecutable = BuildCommandExecutable.NPM
buildCommand = "run build"
}
The plugin could be configured as follows (default values are shown):
typescript {
entrypoint = "app.js" // the entrypoint for execution within the output directory
outputDir = "build/dist" // the output directory
tsConfig = "tsconfig.json" // the tsconfig file
buildCommandExecutable = DEFAULT // (DEFAULT, NODE, NPM, NPX) the executable for custom build command
buildCommand = "" // the custom command to run during the compilation
}
node {
shouldInstall = false // if true, the plugin will download the specified version of node
// If specified both, zipUrl will be used
zipUrl = "" // the url to download the node zip
---OR---
version = "21.7.1" // the version of node to download
}
CheckNode
: checks if node is installed. If specified, it will download it.npmDependencies
: installs the npm dependencies from thepackage.json
.compileTypescript
: compiles the typescript file. Output is in theoutputDir
. IfbuildCommandExecutable
is notDEFAULT
, it will run the specified command inbuildCommand
using the specified executable inbuildCommandExecutable
runJS
: runs the javascript file specified in theentrypoint
.build
: alias forcompileTypescript
.
The critical part of the plugin is the node installation one. The plugin will download the node distribution zip in a temporary directory and extract it in the project build directory. Given that the node installation is a one-time operation, avoiding the download every time. Instead, the plugin will check if the node is installed and skip the download if it is already there.
In order to get the exact path of the node installation at runtime every run, the plugin will save it in a file in the project build directory (nodePaths.properties
).
This allow the plugin to get the path between different executions.
The shell command execution, in order to run typescript and node commands, is done using this utility library.
graph TD
npmDependencies --> CheckNode
compileTypescript --> npmDependencies
build --> compileTypescript
runJS --> compileTypescript