liveBook · Manning

Npm script

As explained in Web Components in Action

npm scripts are named commands declared in package.json and executed through npm to automate project tasks.

An npm script is a named command stored in the scripts object of package.json and run through npm. It gives a project a short, repeatable way to invoke tasks instead of typing a long tool path and its arguments each time. section 12.3 section 12.4

A first script

Add a scripts object to package.json: { "scripts": { "test": "echo 'Hello from package.json'" } }. Run it with npm run test. The name test is only the label; the command is the shell text assigned to it. section 12.3

A build workflow can grow incrementally:

  • Install a tool locally as a development dependency, so the project records it in package.json and others can install the same project dependencies with npm install. section 12.3 section 12.4
  • Add a build script whose command invokes the local Rollup executable with the component source, output file, module format, name, and source-map option. section 12.4
  • Run the command with npm run build, then split larger workflows into names such as build-slider, build-coordpicker, and build-colorpicker. section 12.4
  • Create an aggregate script such as build-rollup to chain component builds with &&, then run the full sequence with npm run build-rollup. section 12.4
Listing 12.7. A simple package.json script
{
 "name": "wcia",
 "version": "1.0.0",
 "scripts": {
   "test": "echo 'Hello from package.json'"       #1
 }
}

Inspect a minimal package.json script before extending the same pattern to component builds. section 12.3

Listing 12.9. Adding a Rollup script in package.json
{
 "name": "wcia",
 "version": "1.0.0",
 "dependencies": {},
 "devDependencies": {
   "rollup": "^1.0.2"
 },
 "scripts": {
 "build": "./node_modules/.bin/rollup chapter12and13/components/slider/src/#1
   slider.js --file chapter12and13/components/slider/slider.js --format    #1
   umd --name slider -m"                                                   #1
}

Compare a named build script with its long local Rollup command and output settings. section 12.4

How Web Components in Action covers it

How deep it goes
Defines scripts, demonstrates npm run execution, and chains component builds, but stops short of general orchestration theory.
Examples
Uses echo, local Rollup commands, component-specific build scripts, and an aggregate script joined with &&.
What it assumes
Assumes terminal and basic Node.js familiarity while introducing package.json scripts from a minimal example.
What you can do after
Operate/configure named npm commands for individual component bundles and complete multi-component builds.
How it connects
Connects package.json, npm, Node.js, development dependencies, Rollup, bundling, and build processes.

Also explained in

Compare how 4 books explain npm script →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.