NPM Scripts Integration

NPM allows us to define custom scripts in the package.json file. 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 scripts property of the package.json file. For example:

  1. ...
  2. scripts: {
  3. "clean": "rimraf dist",
  4. "prebuild": "npm run clean",
  5. "build": "NODE_ENV=production webpack",
  6. }
  7. ...

NPM allows pre and post task binding by prepending the word pre or post respectively to the task name. Here, our prebuild task is executed before our build task.

We can run an NPM script from inside another NPM script.

To invoke the build script we run the command npm run build:

  • The prebuild task executes.
  • The prebuild task runs the clean task, which executes the rimraf dist command.
  • rimraf (an NPM package) recursively deletes everything inside a specified folder.
  • The build task is executed. This sets the NODE_ENV environment variable to production and starts the webpack bundling process.
  • Webpack generates bundles based on the webpack.config.js available in the project root folder.

原文: https://angular-2-training-book.rangle.io/handout/project-setup/npm_scripts_integration.html