DotNet Core 2.0 WebDeploy调用npm build不能在Publish上工作

时间:2017-12-07 14:18:13

标签: npm msbuild .net-core webdeploy

我过去使用的策略是我们的后端开发人员在VisualStudio中工作,我们的UI人员纯粹在Sublime / VSCode等工作。在VS2017网站发布期间,我使用发布目标首先运行npm install,然后是npm build(捆绑我们的app.js逻辑),然后将前端内容包含在WebDeploy的后端包中。

我试图在dotnet core 2.0项目中复制它,但是从不调用构建前端的发布目标调用(即你永远不会看到echo命令内容,也不会运行npm install / build)。因此,发布时不会包含UI资源,除非我以前手动运行构建过程从VSCode生成它们并且它们已经存在于磁盘上。 IE浏览器。 UI生成的文件的COPYING效果很好(如果存在的话)......但是VS2017永远不会调用npm命令来构建资源吗?

虽然很有趣,但这在几个.NET 4.6+项目中运行良好。我刚刚将完全相同的逻辑复制到Core 2.0项目中,并且由于某种原因它不按我期望的方式工作?有什么想法吗?

E.g。我的ProjectDeploy.pubxml文件包含(在最终关闭'Project'标记之前的底部):

  <!-- 
  make sure that we BUILD the UI *before* we copy the files to the package
  See: http://byalexblog.net/using-msbuild-to-deploy-composite-web-application
  See: http://www.codecadwallader.com/2015/03/15/integrating-gulp-into-your-tfs-builds-and-web-deploy/
  -->
  <PropertyGroup>
    <!-- relative path back out of 'current' folder to outside location of the UI files -->
    <FrontEndLocalPath>..\..\src-app</FrontEndLocalPath>
  </PropertyGroup>
  <!-- 
  Build the mobile web app assets , ensuring that all packages are installed and up to date
  -->
  <Target Name="BuildFrontEnd">
    <Exec Command="echo Got Here also Dale" />
    <!-- requires that NPM be installed in environment variables, which we will assume rather than use the NPM env variable above -->
    <!-- call npm install -->
    <exec command="npm install" WorkingDirectory="$(FrontEndLocalPath)" />
    <!-- Run npm run build to populate the www folder with your latest minified production build. -->
    <exec command="npm run build" WorkingDirectory="$(FrontEndLocalPath)" />
  </Target>
  <!--
  On each package and/or deploy (because they are different), we want to ensure that we bundle up all the Angular dist code and include that as part of the package / deployment.
  See: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-extra-files
  -->
  <Target Name="CustomCollectFiles" DependsOnTargets="BuildFrontEnd">
    <Exec Command="echo Got Here Dale" />
    <ItemGroup>
      <_CustomFiles Include="$(FrontEndLocalPath)\www\**\*" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>wwwroot\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>
  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>CustomCollectFiles;</CopyAllFilesToSingleFolderForPackageDependsOn>
    <CopyAllFilesToSingleFolderForMsdeployDependsOn>CustomCollectFiles;</CopyAllFilesToSingleFolderForMsdeployDependsOn>
  </PropertyGroup>

1 个答案:

答案 0 :(得分:4)

好的,所以我不知道这是否特定于新的dotnetcore 2.0,但我遵循这些链接并使事情完美无缺。使用内置的VS2017 SPA模板,我现在不再按照我们在.NET 4.6中使用的方法来修改.pubxml发布配置文件以尝试构建/复制文件..而不是gulp / webpack build-SPA-and-copy-文件构建逻辑包含在.csproj文件中。请注意,这样做的好处是,您可以快速构建解决方案,而无需每次都构建UI,但仍然可以在部署时捆绑所有新的UI更改。我的工作示例如下:

https://stackoverflow.com/a/44429390/4413476

<PropertyGroup>
    <!-- 
    relative path back out of 'current' folder to outside location of the 
    custom single page app UI files (and any other paths we require)
    -->
    <FrontEndLocalPath>..\..\src-app</FrontEndLocalPath>
  </PropertyGroup>

  <Target Name="DebugBuildSPA" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('wwwroot\dist') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
    <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />

    <!-- 
    In development, the dist files won't exist on the first run or when cloning to
    a different machine, so rebuild them if not already present. 
    -->
    <Message Importance="high" Text="Performing first-run Webpack build..." />
    <exec command="npm install" WorkingDirectory="$(FrontEndLocalPath)" />
    <exec command="npm run build" WorkingDirectory="$(FrontEndLocalPath)" />
  </Target>

  <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' ">
    <!-- Build our single page application content -->
    <exec command="npm install" WorkingDirectory="$(FrontEndLocalPath)" />
    <exec command="npm run build" WorkingDirectory="$(FrontEndLocalPath)" />
    <ItemGroup>
      <Dist Include="$(FrontEndLocalPath)\www\**;" />
    </ItemGroup>
    <Copy SourceFiles="@(Dist)" DestinationFolder="$(PublishDir)\wwwroot\%(RecursiveDir)" SkipUnchangedFiles="true" />
  </Target>