NPM Scripts Integration
NPM allows us to define custom scripts in the
package.jsonfile. These can then execute tasks using the NPM CLI.
We rely on these scripts to manage most of our project tasks and webpack fits in as well.
The scripts are defined in the
scriptsproperty of the
package.jsonfile. For example:
"clean": "rimraf dist",
"prebuild": "npm run clean",
"build": "NODE_ENV=production webpack",
NPM allows pre and post task binding by prepending the word
postrespectively to the task name. Here, our
prebuildtask is executed before our
We can run an NPM script from inside another NPM script.
To invoke the
buildscript we run the command
npm run build:
prebuildtask runs the
cleantask, which executes the
rimraf(an NPM package) recursively deletes everything inside a specified folder.
buildtask is executed. This sets the
NODE_ENVenvironment variable to
productionand starts the webpack bundling process.
- 5.Webpack generates bundles based on the
webpack.config.jsavailable in the project root folder.