【问题标题】:MSBuild: Conditionally run webpack only when there's changesMSBuild:仅在发生更改时有条件地运行 webpack
【发布时间】:2020-02-29 00:53:02
【问题描述】:

我们正在使用 Visual Studio 2019 并已成功配置 MSBuild 任务来为我们运行 webpack - 在调试和生产模式下。

但是每次构建项目时都运行它有点矫枉过正,例如当我们运行单元测试并且对 Web 项目没有任何更改时,我们不需要执行 webpack。

有没有办法将构建任务配置为仅在特定文件夹中的文件发生更改时执行?

这是我们当前的配置:

<Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug'">
  <!-- 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="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" />
  <Exec Command="node node_modules/webpack/bin/webpack.js" />
</Target>

<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
  <Exec Command="npm install" />
  <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
  <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />

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

【问题讨论】:

  • 请使用&lt;Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' and 'DeployOnBuild'=='true' "&gt;

标签: visual-studio webpack msbuild visual-studio-2019


【解决方案1】:

有没有办法将构建任务配置为仅在文件时执行 特定文件夹中的内容发生了变化?

您可以通过 DeployOnBuild 属性区分 publishbuild 过程。

解决方案

你只需要给DebugRunWebpack目标添加一个条件:

<Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' and 'DeployOnBuild'=='true'">
  <!-- Ensure Node.js is installed -->
......    
</Target>

并且它只会在发布过程中执行目标。

另外PublishRunWebpack目标运行在属于发布进程的ComputeFilesToPublish之后。所以你不用担心。

更新 1

要检测这些文件是否改变,可以试试这个条件:

&lt;Target Name="xxxx" Condition="$([System.DateTime]::Parse('%(ModifiedTime)').Ticks) &amp;gt; $([System.IO.File]::GetLastWriteTime('$(xxxxxxxx)%(RecursiveDir)%(Filename)%(Extension)').Ticks)" /&gt;

注意$(xxxxxxxx)%(RecursiveDir)%(Filename)%(Extension)表示该文件夹下的所有文件

另外可以参考this document

希望对你有帮助。

【讨论】:

  • 我不想区分发布和构建流程(也许重新阅读问题)。 DeployOnBuild 用于创建 Web 部署包。它不能帮助我将构建任务配置为仅在特定文件夹中的文件发生更改时执行 - 特别是 .ts 打字稿文件。
猜你喜欢
  • 2016-03-27
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 2019-12-12
  • 2010-09-18
  • 2018-01-15
相关资源
最近更新 更多