You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Angular CLI命令行设置带hidden的SourceMap构建参数?

Angular CLI命令行传递SourceMap对象配置的方法

你提到的ng build --source-map=scripts:true,hidden:true这种格式无法直接生效,因为Angular CLI默认只会把--source-map的参数解析为布尔值或字符串,不会自动将逗号分隔的键值对转换成配置对象。不过有两种可行的方式实现命令行传递复杂的sourceMap配置:

1. 传递JSON格式的字符串参数

Angular CLI支持将--source-map参数设置为JSON字符串(注意根据不同终端做转义处理):

Linux/macOS(Bash)

ng build --source-map='{"scripts": true, "hidden": true}'

Windows(PowerShell)

需要转义内部的双引号:

ng build --source-map="{\"scripts\": true, \"hidden\": true}"

Windows(CMD)

转义方式略有不同:

ng build --source-map="{\"scripts\": true, \"hidden\": true}"

这种方式会让CLI将JSON字符串解析为sourceMap的配置对象,等同于在angular.json中配置:

"sourceMap": {
  "scripts": true,
  "hidden": true
}

2. 使用环境变量传递配置

你也可以通过NG_BUILD_SOURCE_MAP环境变量来传递JSON格式的配置,避免终端转义的麻烦:

Linux/macOS(Bash)

NG_BUILD_SOURCE_MAP='{"scripts": true, "hidden": true}' ng build

Windows(PowerShell)

$env:NG_BUILD_SOURCE_MAP='{"scripts": true, "hidden": true}'; ng build

额外建议

如果需要频繁使用这种配置,更高效的方式是在package.json中封装npm脚本,比如:

{
  "scripts": {
    "build:hidden-sourcemap": "ng build --source-map='{\"scripts\": true, \"hidden\": true}'"
  }
}

之后只需执行npm run build:hidden-sourcemap即可,不用每次手动输入复杂的命令。

内容的提问来源于stack exchange,提问作者Rupert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 14:21:37