DEV Community

t-o-d
t-o-d

Posted on

【NPM】Three useful ways to write a grouping in scripts configuration.

  • I think you need to write scripts configuration in package.json when you use npm. In this post, I'll write three useful ways to group scripts.

preparation

  • Install npm-run-all, which is useful when writing multiple commands in scripts.
    • It can be written as run-s ******(scripts name).
$ npm i -D npm-run-all 

1. Basic Writing

  • Write all of the scripts.
{ "scripts": { "build": "run-s build:clean build:copy build:js", "build:clean": "rimraf ./dist", "build:copy": "cpx -C public/** dist", "build:js": "esbuild src/index.js --bundle --outfile=dist/out.js" } } 

2. wildcard writing

  • Write with a wildcard using an asterisk.
{ "scripts": { "build": "run-s build:*", "build:clean": "rimraf ./dist", "build:copy": "cpx -C public/** dist", "build:js": "esbuild src/index.js --bundle --outfile=dist/out.js" } } 

3. grouping writing (Convenient and recommended)

  • Write it as you would an array using brackets.
  • It is recommended that the order and names are clearly marked.
{ "scripts": { "build": "run-s build:{clean,copy,js}", "build:clean": "rimraf ./dist", "build:copy": "cpx -C public/** dist", "build:js": "esbuild src/index.js --bundle --outfile=dist/out.js" } } 

That's all.
Thank you for reading.

Top comments (0)