我有一个 Visual Studio 解决方案(不是 VS Code),其中包含多个 tsconfig.json
文件,我希望这些文件在生产中表现不同。
我想要两个文件(也许更多),例如:
tsconfig.json
这部分解决方案的默认设置:
{
"compilerOptions": {
"sourceMap": true,
"removeComments": false,
...
},
... lots more settings
}
tsconfig.release.json
覆盖这些设置以排除注释和源映射:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": false,
"removeComments": true
}
}
如何告诉 VS2019/MSBuild 在进行发布构建或发布时使用 tsconfig.release.json
覆盖?
最佳答案
我找到了一种方法来做到这一点,但这很糟糕,我很乐意接受更好的答案。
tsc
可以采用自定义配置文件名,但如果添加 <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
则只能从 MSBuild 运行它到项目文件并添加其他内容来运行它。
MSBuild 对于发布和调试可以有不同的选项,但仅如果它拥有所有 Typescript 配置并且没有名为 tsconfig.json
的文件。项目中的任何位置。
MSBuild可以找到任何 tsconfig.json
在任何文件夹深度,但找不到任何其他文件名 - tsconfig.release.json
或tsconfig.debug.json
出来了。
MSBuild 还可以更改其包含的文件的规则,因此解决方案:
- 将版本覆盖移至文件夹,因此
tsconfig.release.json
变成releaseConfig/tsconfig.json
。现在需要一些相对路径来查找项目:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"sourceMap": false,
"removeComments": true
},
"include": [ "../**/*" ]
}
- 接下来我们需要对
.csproj
进行两处更改文件。首先,我们阻止在非发布版本中采用此配置:
<ItemGroup Condition="'$(Configuration)|$(Platform)'!='Release|AnyCPU'">
<Content Remove="whatever\releaseConfig\tsconfig.json" />
<None Include="whatever\releaseConfig\tsconfig.json" />
</ItemGroup>
- 最后在发布版本中我们交换文件:
<ItemGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<Content Remove="whatever\tsconfig.json" />
<None Include="whatever\tsconfig.json" />
<None Remove="whatever\releaseConfig\tsconfig.json" />
</ItemGroup>
默认tsconfig.json
已从 <Content>
中删除,但需要是 <None>
所以 releaseConfig\tsconfig.json
中的相对路径仍然可以找到它。
关于typescript - 在 VS2019 解决方案/MSBuild 中覆盖 tsconfig.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64700699/