使用 .NET Framework 发布时如何复制 webpack 构建

How to copy webpack builds when publishing using .NET Framework

我有一个旧项目在 .NET Framework 上使用 ASP.NET MVC/WebAPI、运行s,并发布到 Azure 云服务。我正在尝试迁移到 React,并希望避免将 webpack 构建添加为 .csproj 上的文件。我已经设法获得 运行 npm run build 生成输出的构建步骤,但是当我尝试发布时构建的文件没有添加到已发布的包中。

在 .NET 5 应用程序中,当您使用 React/SPA 模板时,.csproj 文件包含如下内容:

    <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
        <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />

        <!-- Include the newly-built files in the publish output -->
        <ItemGroup>
            <DistFiles Include="$(SpaRoot)build\**" />
            <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
                <RelativePath>%(DistFiles.Identity)</RelativePath>
                <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
                <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
            </ResolvedFileToPublish>
        </ItemGroup>
    </Target>

我想我明白这是在做什么,但我在想出一个适用于 .NET Framework 的模拟时遇到了麻烦。当我 运行 时,它就像这样,命令甚至没有 运行。我猜 ComputeFilesToPublish 目标不会在这些旧式项目中被调用。我已经尝试使用 BuildPublishBeforeTargetsAfterTargets 对其进行 运行 设置,当我这样做时,我可以从日志中看到 npm installnpm run build 被调用,但文件未复制到发布目录(在本例中为 csx)。

我可以在 .csproj 中做什么来将我的构建文件拉入发布包?

问题是 .NET 5 Azure Web 应用程序的构建目标不同于 .NET Framework Azure 云服务。这是我需要的秘方:

  <Target Name="BuildRunWebpack" BeforeTargets="BeforeBuild">
    <Message Importance="normal" Text="Running npm install as a Pre-build step" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Message Importance="normal" Text="Running npm run build (compile webpack) as a Pre-Build step" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
  </Target>
  <Target Name="PublishRunWebpack" BeforeTargets="CollectFilesFromContent">
    <!--On publish, this will pull any files in the ClientApp\dist folder that were built by webpack. These files need to be in the published package.-->
    <Message Importance="normal" Text="Collecting webpack bundle for publish" />
    <ItemGroup>
      <Content Include="$(SpaRoot)dist\**" />
    </ItemGroup>
  </Target>