Last updated on
npm scripts 参数传递:--
介绍
https://docs.npmjs.com/cli/v6/commands/npm-run-script
npm run-script <command> [--silent] [-- <args>...]
alias: npm run
在 npm 2.0.0 版本及以后,npm 提供了一种向脚本传递自定义参数的方式。这个功能的关键点如下:
- 双破折号(
--)的特殊用途:--是一个特殊的分隔符- 它用来标识选项的结束,在命令行中区分 npm 自身的参数和要传递给脚本的参数
- 参数传递机制:
--后面的所有参数会被直接传递给指定的脚本- 这些参数不会被 npm 本身处理或修改
- 使用示例:
npm run test -- --grep="pattern"
在这个例子中:
npm run test是执行 npm 脚本的命令--是分隔符--grep="pattern"是传递给 test 脚本的参数
- 重要说明:
- 这些参数只会传递给主要指定的脚本
- 不会传递给相关的 pre 和 post 脚本
- 例如,如果你有 pretest、test 和 posttest 脚本,参数只会传递给 test 脚本
下面是一个例子 🌰
workspace/npm-args-demo > npm test -- --grep="Addition"
> npm-args-demo@1.0.0 pretest
> echo 'Starting tests...'
Starting tests...
> npm-args-demo@1.0.0 test
> mocha 'test/**/*.test.js' --grep=Addition
Calculator
Addition
✔ should add two positive numbers correctly
✔ should handle negative numbers
2 passing (3ms)
> npm-args-demo@1.0.0 posttest
> echo 'Tests completed!'
Tests completed!
workspace/npm-args-demo > npm test --grep="Addition"
> npm-args-demo@1.0.0 pretest
> echo 'Starting tests...'
Starting tests...
> npm-args-demo@1.0.0 test
> mocha 'test/**/*.test.js'
Calculator
Addition
✔ should add two positive numbers correctly
✔ should handle negative numbers
Multiplication
✔ should multiply two positive numbers correctly
✔ should handle negative numbers
4 passing (3ms)
> npm-args-demo@1.0.0 posttest
> echo 'Tests completed!'
Tests completed!
实际场景
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
$ npm run build -- --profile
> my-storefront@0.1.0 build
> next build --profile