Basics of configuring projects

JavaScript and TypeScript projects are managed from the jsProjects extension. This contains the following blocks:

  • sourceSets: A collection of source set definitions. Each source set is an implementation of JseSourceSet.

  • lifecycleManagers: A collection of lifecycles. A lifecycle determines how the project is built, tested, and how its packages are managed. A lifecycle instance is a polymorphic class that implements JseLifecycleManager.

  • wrappers: For configuring wrapper generation defaults. It is primarily meant for cases when organisational environments would like the exact version of a tool to be available from the command-line without the user having to install it separately from something that Gradle might have bootstrapped. It is of type JseWrapperConfiguration.

jsProjects {
    lifeCycleManagers {
        pnpm(LifecyclePnpmNodeWithPackageJson) { (1)

        }
    }

    sourceSets {
        main { (2)
          srcDir = 'src/jse/main' (3)
          outputPath = './dist' (4)
          lifecycle('pnpm', LifecyclePnpmNodeWithPackageJson) (5)

          readOutputPathFromTsConfig() (6)
          readProjectVersionFromPackageJson() (7)
        }
    }

    wrappers {
        includeBat = true (8)
        includePs1 = false (9)
        preferPs1OverBat = false (10)
    }
}
1 Adds a lifecycle manager called pnpm with a set type. This is just an example. For actual development with pnpm see Developing applications and libraries with pnpm.
2 Defines a source set. It the source set is called main it is treated as the default source set and results in shorter task names. This is in a similar fashion to compileJava vs compileTestJava.
3 Sets the source directory. By default, this is src/jse/<SourceSetName>.
4 Sets an optional output directory relative to the source directory. This is optional as some projects do not create any output files.
5 Links the source set to a specific lifecycle. Once set in configuration, this cannot be changed anywhere else in the DSL.
6 If you are using TypeScript, then read the output directory from tsconfig.json. This overrides any previous settings of outputPath.
7 Reads the project version from the version in package.json. This overrides whatever version was set in a Gradle property. If you have more than one source set, only call this from one of the source sets, otherwise it is a case of last-source-set-wins.
8 Include .bat file generation.
9 If the lifecycle supports it, include .ps1 file generation.
10 If both .bat` and .ps1 are available, set which one is preferred.

Lifecycle Manager tasks

A lifecycle manager is responsible for creating specific tasks and adding them to Gradle’s lifecycle task dependencies. THe typical tasks that are created are:

  • An assembly task to validate JavaScript code and transpile TypeScript code.

  • A test task to execute a test suite.

  • A package installer task.

  • Optionally, tasks to manage tool wrappers.

A description of the requirements is also available in JseSourceSetTasks.