typescript 在VS 2019解决方案/MSBuild中覆盖tsconfig.json?

6psbrbz9  于 2023-06-24  发布在  TypeScript
关注(0)|答案(2)|浏览(152)

我有一个Visual Studio解决方案(不是VS Code),其中包含多个tsconfig.json文件,我希望在生产中有不同的行为。
我想有两个文件(也许更多)类似于:

  1. tsconfig.json此部分解决方案的默认设置:
{
  "compilerOptions": {
    "sourceMap": true,
    "removeComments": false,
    ...
  },
  ... lots more settings
}
  1. tsconfig.release.json覆盖这些设置以排除注解和源Map:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "sourceMap": false,
    "removeComments": true
  }
}

如何告诉VS 2019/MSBuild在进行发布或发布时使用tsconfig.release.json覆盖?

q43xntqr

q43xntqr1#

我已经找到了一个方法来做到这一点,但这太可怕了,我很乐意接受一个更好的答案。
tsc可以使用自定义的配置文件名,但只有在将<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>添加到项目文件中并添加其他内容来运行它的情况下,才能从MSBuild运行它。
MSBuild可以为Release和Debug提供不同的选项,但 * 仅 * 在它拥有所有Typescript配置并且项目中没有名为tsconfig.json的文件时。
MSBuild * 可以 * 在任何文件夹深度找到任何tsconfig.json,但找不到任何其他文件名-tsconfig.release.jsontsconfig.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>

<Content>中删除了默认的tsconfig.json,但需要是<None>,这样releaseConfig\tsconfig.json中的相对路径才能找到它。

dwbf0jvd

dwbf0jvd2#

遇到了类似的问题。在深入研究了TypeScript NuGet的内部之后,我决定使用这个,我已经将其填充到父目录中的Directory.Build.targets中。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="FindConfigFiles"
          Condition="'$(DesignTimeBuild)' != 'true'">
    <ItemGroup>
      <ConfigFiles Include="**\tsconfig.json" KeepDuplicates="false" 
                   Condition="'$(Configuration)' == 'Release'" />
      <ConfigFiles Include="**\tsconfig.Development.json" KeepDuplicates="false"
                   Condition="'$(Configuration)' == 'Debug'" />
    </ItemGroup>
  </Target>
</Project>

我使用了显式的文件名,但在上面,我已经用**\(在任何子目录中匹配)前缀Include s,为任何其他谷歌搜索他们的方式在这里。

相关问题