如何修改csproj PublishRunWebpack以包括来自外部目录的静态文件

时间:2019-04-30 15:04:30

标签: angular iis asp.net-core webdeploy asp-net-core-spa-services

我从ASP.NET Core Web应用程序-> Angular SPA开始,但是我已经将Angular应用程序移到了csproj之外的单独的存储库中。

所以我的文件夹结构如下:

frontend
  dist

backend
  wwwroot
  backend.csproj

现在,我想修改csproj,以便将副本frontend/dist/**/*.*发布到$(outputDirecory)/ClientApp/dist/**/*.*

我应该如何修改PublishRunWebpack目标以复制这些文件:

  <PropertyGroup>
    <SpaRoot>../Frontend</SpaRoot>
  </PropertyGroup>

  <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 -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <!-- Include the newly-built files in the publish output -->
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">


        <!-- THIS NEEDS TO BE CHANGED (I guess) -->
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,几个小时后我找到了解决方案。我想这对你来说太晚了,但其他人可能会发现它有帮助。 正如您所说,问题出在 RelativePath 上。默认值“%(DistFiles.Identity)”使用完整文件名并将其附加到根发布文件夹。解决方案是构建您自己的相对路径,就像我在下面的代码中所做的那样。

<Target Name="PublishRunWebpack"
          BeforeTargets="ComputeFilesToPublish">
    <!--User app-->
    <Exec WorkingDirectory="$(SpaRoot)"
          Command="npm install"
          Condition="  !Exists('$(SpaRoot)node_modules') " />
    <Exec WorkingDirectory="$(SpaRoot)"
          Command="npm run build -- --prod" />

    <ItemGroup>
      <ClientDistFiles Include="$(SpaRoot)dist/**/*.*"/>
      <ResolvedFileToPublish Include="@(ClientDistFiles->'%(FullPath)')"
                             Exclude="@(ResolvedFileToPublish)">
        <RelativePath>ClientApp\app\dist\%(RecursiveDir)%(Filename)%(Extension)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
</Target>

答案 1 :(得分:0)

根据您的描述,我发现前端文件夹不会复制到PubTmp \ Out文件夹。这意味着forentend spa应用程序将不会复制到发布文件夹。

我建议您尝试创建一个新目标,以将forentend dist文件夹复制到publish out文件夹。

更多详细信息,您可以参考以下代码:

      <Target Name="PublishRunWebpack" AfterTargets="CopyFileToOutput">
    <!-- 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 -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
  <Target  Name="CopyFileToOutput"  AfterTargets="ComputeFilesToPublish">
    <ItemGroup>
      <_CopyItems Include="obj\Release\netcoreapp2.2\PubTmp\Frontend\**\*.*" />
    </ItemGroup>
    <Copy
    SourceFiles="@(_CopyItems)"
    DestinationFolder="obj\Release\netcoreapp2.2\PubTmp\Out\Frontend\dist"
    />
  </Target>