Search code examples
monoreponrwl-nxnrwl

NX how to pass arguments to target dependency


I have the project config set in the package.json as below:

"nx": {
  "targets": {
    "build": {
      "outputs": [
        "{projectRoot}/dist"
      ]
    },
    "build-site": {
      "dependsOn": [
        "build"
      ],
      "outputs": [
        "{projectRoot}/fractal/build"
      ]
    },
    "bundlesize": {
      "dependsOn": [
        "build"
      ]
    }
  }
}

I want to pass an argument to the build target it depends on.

nx run-many --target=build-site --all -- --foo=bar

This does not seem to get passed down to the build target. Is there any way to do this?

[EDIT]

I use npm scripts (in package.json) as targets and have target dependencies defined with Nx like below.

{
  "name": "project-a",
  "scripts": {
    "build": "gulp build",
    "build-site": "gulp buildFractal"
  },
  "nx": {
    "build-site": {
      "dependsOn": [
        "build"
      ],
    }
  }
}

And then I run nx run-many --target=build-site --all. However, the issue is that I want to pass a flag to the dependent build target which does not appear to work when I do nx run-many --target=build-site --all -- --foo=bar


Solution

  • dependsOn can be configured with a object instead of the shorthand:

    "dependsOn": [{"projects": "self", "target": "build", "params": "forward"}],
    

    Then the params will get forwarded- Ref: https://nx.dev/reference/project-configuration#dependson