TypeScript 未使用 Azure 托管代理构建

TypeScript not building with Azure Hosted Agent

我有一个用 .Net5 编写的 ASP.NET MVC 应用程序。

这使用了打字稿文件,我有对以下内容的 NuGet 包引用:

<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />

在我的项目中我有:

package.json是:

{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "devDependencies": {
    "gulp": "4.0.2",
    "del": "5.1.0",
    "@types/bootstrap": "4.5.0",
    "@types/google.visualization": "0.0.53",
    "@types/jquery": "3.5.1",
    "@types/jqueryui": "1.12.13",
    "@types/jquery.datatables": "1.10.36",
    "@types/jquery.ui.datetimepicker": "0.3.29"
  }
}

当我在 Visual Studio 2019(最新)编译时,它编译完美,一切都按预期工作。

但是我确实得到了构建输出(信息):

Package restore on project open is disabled. Change the npm package management settings in Project Properties to enable restore on project open.

我找到了设置“在项目保存时恢复”并将其更改为 true。将以下内容添加到我的 csproj 文件中:

<ProjectExtensions><VisualStudio><UserProperties appsettings_1qaf_1json__JsonSchema="https://gitpod.io/schemas/gitpod-schema.json" NpmRestoreOnProjectOpen="True" /></VisualStudio></ProjectExtensions>

我将它推送到 Azure DevOps,但是当代理构建它时(为清楚起见删除了一些步骤):

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true

我收到以下错误:

##[error]MyApp\Scripts\MyTypeScript.ts(13,9): Error TS2581: Build:Cannot find name '$'. Do you need to install type definitions for jQuery? Try npm i @types/jquery.

因此,托管代理显然没有检索类型。我猜我需要调整我的 YAML 文件,但不确定如何...

根据错误消息和 Yaml 定义,您似乎缺少 NPM Install 安装所需 npm 包的步骤。

VSBuild任务本身只能恢复nuget包,不能安装npm包,所以需要添加额外的任务来安装npm包。

这是我的示例:您可以尝试在 VSBuild 任务之前添加一个 Npm Install task

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:

- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'

- task: Npm@1
  displayName: 'npm install'
  inputs:
    workingDir: '$(build.sourcesdirectory)'
    verbose: false


- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true

结果:

它可以正常工作。