Automatically execute scripts before and after executing npm run build

Original link: https://www.mqllin.cn/archives/198.html

Automatically execute scripts before and after executing npm run build

Record chatGPT’s Q&A:

Question:

In the angluar project, I wrote a compress.js script to automatically compress files after packaging. How to automatically execute the script every time I execute npm run build

Answer:

To automatically execute the compress.js script every time the npm run build command is executed, add the script to the build command in the project’s package.json file.

First, open the package.json file, find the “scripts” field, and see if the current build command is similar to the following:

“scripts”: {

“build”: “ng build”

}

If so, add the script name to the end of the “ng build” command, using the “&&” connector, like this:

“scripts”: {

“build”: “ng build && node compress.js”

}

In this way, when the npm run build command is executed, the ng build command will be executed first, and then the compress.js script will be executed.

In addition, if you want to automatically trigger the build command every time you modify any source file, you can use the –watch parameter provided by Angular CLI, for example:

“scripts”: {

“build”: “ng build –watch && node compress.js”

}

This way, every time a file is modified, an automatic build is triggered and the compress.js script is executed.

Question:

What else can I do without modifying the build command

Answer:

If you don’t want to modify the build command and want to keep the original execution method, you can also use another method to automatically execute the compress.js script every time you execute npm run build.

In the package.json file, you can use the “pre” field to define scripts that need to be run before the build command is executed. For example:

“scripts”: {

“build”: “ng build”,

“prebuild”: “node-compress.js”

}

In this approach, we use “prebuild” to define that the “node compress.js” command needs to be run before executing the “build” command.

When we execute the “npm run build” command, the “prebuild” script is actually run first, and then the “build” command is executed. This ensures that the compress.js script is automatically executed every time the “npm run build” command is executed.

In addition, you can also use the –watch parameter to automatically trigger the build every time a file is modified. Just change the command above to:

“scripts”: {

“build”: “ng build –watch”,

“prebuild”: “node-compress.js”

}

This way, a build is automatically performed and the files are automatically compressed every time they are modified.

Question:

What I need is to execute compress.js after executing build

Answer:

If you need to execute compress.js after executing the build command, you can use the “post” field to define the script that needs to be run after the build command. For example:

“scripts”: {

“build”: “ng build”,

“postbuild”: “node-compress.js”

}

In this approach, we use “postbuild” to define that the “node compress.js” command needs to be run after the “build” command.

When we execute the “npm run build” command, the “build” command is actually executed first, and then the “postbuild” script is run. This ensures that the compress.js script is automatically executed after each “npm run build” command.

In addition, you can also use the –watch parameter to automatically trigger the build every time a file is modified. Just change the command above to:

“scripts”: {

“build”: “ng build –watch”,

“postbuild”: “node-compress.js”

}

This way, a build is automatically performed and the files are automatically compressed every time they are modified.

This article is transferred from: https://www.mqllin.cn/archives/198.html
This site is only for collection, and the copyright belongs to the original author.