npm run:执行另一个(不正确的)脚本

问题描述 投票:1回答:2

这是我的scriptspackage.json部分:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "compile-prebuild": "tsc -p prebuild-tsconfig.json --pretty",
    "prebuild": "ts-node --project PreBuild/tsconfig.json PreBuild/prebuild.ts",

    "testJs": "node test.js",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "extract-i18n": "ng xi18n Paradise --i18n-format=xlf2 --output-path=i18n --i18n-locale=en && ng run Paradise:xliffmerge"
},

奇妙的是当我尝试npm run buildnpm run build -- --prod执行另一个脚本(prebuild)时:

> npm run build -- --prod

> [email protected] prebuild ...
> ts-node --project PreBuild/tsconfig.json PreBuild/prebuild.ts

现在,如果我将prebuild脚本重命名为pre-build(在package.json中),一切都会好起来的:

> npm run build -- --prod

> [email protected] build ...
> ng build "--prod"
...

现在,如果我回复,问题又出现了!


> npm -v
6.7.0
npm npm-scripts
2个回答
1
投票

这是“正确的”,因为它是npm的记录行为 - 请参阅here

此外,可以通过运行npm run-script <stage>来执行任意脚本。具有匹配名称的前后命令也将针对这些命令运行(例如premyscriptmyscriptpostmyscript)。

通常,脚本可以使用prepost作为前缀,以便在脚本之前或之后执行操作。

在选择npm脚本名称时,最好将前缀prepost视为保留(当然,除非您打算在主任务之前或之后始终运行它们)。


1
投票

前后挂钩由npm自动运行。如果你在package.json中定义了prebuild,当你要求它运行build时,npm会自动运行它。同样适用于后挂钩。

你可以在这里查看文档。 https://docs.npmjs.com/misc/scripts

© www.soinside.com 2019 - 2024. All rights reserved.