8

I'm using nx.dev to build and test a web application. The workspace.json contains the scripts to build and test the app, however the build phase still need a pre processing of some files.

Is there any way of adding a pre build step (i.e. specify an external bash script or JavaScript code)?

The online documentation doesn't mention that https://nx.dev/react/cli/build

Amir
  • 1,328
  • 2
  • 13
  • 27
Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159

1 Answers1

12

There isn't an option to run shell scripts in the build builder itself, but you can use @nrwl/workspace:run-commands builder to do that.

In your workspace.json you should add to the architect section of the project in question.

"architect": {
  "prepare": {
    "builder": "@nrwl/workspace:run-commands",
    "options": {
      "commands": [
        {
          "command": "echo Hello!"
        }
      ]
    }
  }
}

I named the target prepare but you can choose any name you want. And then you can execute it with nx prepare [projectName].

You can use run-commands to compose existing targets like build and test with arbitrary shell commands to form new targets. It's up to you how you want to wire things up.

I put together an example repo here to give you some ideas: https://github.com/jaysoo/nx-run-commands-example

jay_soo
  • 1,278
  • 2
  • 13
  • 20
  • 3
    For anyone confused about the property keys nomenclature (architect, builder), read this line from the nx docs: "Note: There are a few property keys in workspace.json that have interchangeable aliases. You can replace *generators* with *schematics*, *targets* with *architect* or *executor* with *builder*." (https://nx.dev/latest/angular/executors/using-builders#executor-definitions) – Papooch Sep 09 '21 at 11:26
  • This is all nice and thanks, but it doesn't answer on the 'post' part. If I understand correctly this doesn't assist in creating automation 'tasks' (abstractly speaking) that will run following a 'build' target run. – Boaz Rymland Sep 13 '21 at 10:15
  • 5
    You can currently create a 'dependsOn' key with an array of targets which should be run 'BEFORE' the current target. So the npm 'pre-' part is already covered. Only the 'post-' script part is not covered, which is sad. see here: https://nx.dev/l/a/core-concepts/configuration and scroll to 'Target Dependencies' – Benjamin Jesuiter Dec 02 '21 at 13:44