Skip to content

Latest commit

 

History

History
130 lines (98 loc) · 3.88 KB

faq.md

File metadata and controls

130 lines (98 loc) · 3.88 KB

FAQ

This page contains a collection of frequently asked questions.

How to avoid node/npm/yarn task execution if no changes in web files?

Just add to your bundle task filesets (in and out) which this task depends on:

task bundle(type: YarnTask) {
    inputs.files(fileTree('node_modules'))
    inputs.files(fileTree('src'))
    inputs.file('package.json')
    inputs.file('webpack.config.js')
    
    outputs.dir('build/resources/static')
 
    dependsOn yarn_install
    args = ['run', 'build']
}

More info in Gradle doc

How do I use npm ci instead of npm install?

node {
    npmInstallCommand = System.getenv("CI") ? 'ci' : 'install'
}

How do I set log level for NPM install task?

This can be done adding some arguments to the already defined npmInstall-task. To set the log level to silly do this:

npmInstall.args = ['--loglevel', 'silly']

How do I specify a registry for the NPM setup task?

This can be done by adding to the arguments for the already defined npmSetup task.

tasks.npmSetup {
    doFirst {
        args = args + ['--registry', 'http://myregistry.npm.com']
    }
}

You can also add any other arguments to this list that work with npm install i.e. more verbose modes.

How do I customize the way the processes are launched?

NodeTask, NpmTask, NpxTask and YarnTask are some wrappers around the core Exec task. They have several parameters that enable to customize the way the corresponding command is launched.

The ignoreExitValue property enables to avoid the task failing if the process exit value is not 0:

task myScript(type: NodeTask) {
  script = file('src/scripts/my.js')
  ignoreExitValue = true
}

The workingDirectory option enables to change the working directory of the process. Note that some commands such as npm force the working directory to be the one in which the package.json file is located. This option is most of the time useless.

task myScript(type: NodeTask) {
  script = file('src/file.js')
  workingDir = file('./customWorkingDirectory')
}

The environment option enables to define some new environment variables or override some existing ones:

task command(type: NpxTask) {
  command = 'aCommand'
  environment = ['CUSTOM_VARIABLE': 'Hello world']
}

The execOverride option enables to customize all the other thinks that can be configured in an ExecSpec thanks to a closure that takes the ExecSpec as parameter. Note that it is executed last, possibly overriding already set parameters such as the working directory.

task myScript(type: NpmTask) {
  npmCommand = ['run', 'hello']
  execOverrides {
    // The it variable contains the `ExecSpec`
    it.ignoreExitValue = true
    it.workingDir = file('./myWorkingDirectory')
    it.standardOutput = new FileOutputStream('logs/my.log')
  }
}

How do I ignore some files of the node_modules directory that are modified by the build and prevent tasks from being up-to-date ?

NpmInstallTask and YarnInstallTask have an option that enables to exclude some files from the task's output. Its type is a closure that contains a FileTree whose root directory is node_modules.

With npm:

npmInstall {
  nodeModulesOutputFilter = { it.exclude("package/package.json") }
}

Note that it is the implicit closure variable containing the FileTree instance. It could also be written this way:

nodeModulesOutputFilter = { fileTree -> fileTree.exclude("package/package.json") }

With yarn:

yarn {
    nodeModulesOutputFilter = { it.exclude("package/**").exclude("anotherPackage") }
}

Note: the Gradle's up-to-date checking is much slower when using this option. See issue #63.