javascriptnx.dev

Pre and post build steps in nx.dev


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


Solution

  • 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