typescript - 在 VS2019 解决方案/MSBuild 中覆盖 tsconfig.json?

标签 typescript msbuild visual-studio-2019 tsconfig

我有一个 Visual Studio 解决方案(不是 VS Code),其中包含多个 tsconfig.json 文件,我希望这些文件在生产中表现不同。

我想要两个文件(也许更多),例如:

  1. 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.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>
    

    默认tsconfig.json已从 <Content> 中删除,但需要是 <None>所以 releaseConfig\tsconfig.json 中的相对路径仍然可以找到它。

    关于typescript - 在 VS2019 解决方案/MSBuild 中覆盖 tsconfig.json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64700699/

    相关文章:

    angular - 如何在 angular-2 中创建 GUID?

    javascript - 从 Jenkins 下载 zip

    visual-studio-2013 - MSBuild错误: "The solution file has two projects named ..."

    msbuild - 是否可以知道 ReSharper 的测试运行程序何时构建我的项目?

    c# - 在一个库/NuGet 包中支持多个版本的 NuGet 包

    c# - 运行时浮点计算与评估计算

    javascript - 如何根据屏幕上的 View 外观为自动页面滚动制作动画? ( Angular )

    typescript :使用来自 redux-saga 的 call() 类型

    javascript - Visual Studio 2019 中用于 Node js 应用程序的 Mocha 在引用或需要另一个文件时未在测试资源管理器中显示测试

    visual-studio - ESLint 支持 Visual Studio 2017