Home > OS >  Executing a JavaScript file when running ng build
Executing a JavaScript file when running ng build

Time:05-27

I would like to run a JavaScript file in my Angular application every time I run ng build. To be more precise, I want this file to be executed before the build process so that the changes that it makes are present in the build.

Its a simple script that reads the app version and its dependencies and write them to an object.

The file is called pre-build.js and I have tried configuring the build command in package.json as follows, however I can see that the script was not executed:

{
   ...
   ...,
   "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "node pre-build.js && ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
   },
   ...,
   ...,
}

The path of the script is ./pre-build.js. I assume that I have to change more configurations in order to achieve this but I am not able to find out where. Any leads will be appreciated.

CodePudding user response:

You can create a ".sh" script to other execute everything you need. This might be helpful later on to add more pre or post build commands

Here is an example

package.json

"scripts": {
  "build:angular": "ng build",
  "build": ./build.sh
}

build.sh

#!/bin/bash
node ./pre-build.js

npm run build:angular

Make sure that pre-build is executable so is the build.sh (chmod https://askubuntu.com/questions/229589/how-to-make-a-file-e-g-a-sh-script-executable-so-it-can-be-run-from-a-termi )

CodePudding user response:

Try like this:

node ./pre-build.js && ng build
  • Related