title: MSBuild: Conditionally run webpack only when there's changes
body: <p>We're using Visual Studio 2019 and have successfully configured MSBuild tasks to run webpack for us - both in Debug and Production mode.</p>
  
  <p>But it is a bit overkill to run everytime the project builds e.g. when we run unit tests and there is no changes to the Web project, we don't need webpack to execute.</p>
  
  <p>Is there a way to configure the build task to only execute when files in a particular folder have changed?</p>
  
  <p>Here is our current configuration:</p>
  
  <pre><code>&lt;Target Name="DebugRunWebpack" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug'"&gt;
    &lt;!-- Ensure Node.js is installed --&gt;
    &lt;Exec Command="node --version" ContinueOnError="true"&gt;
      &lt;Output TaskParameter="ExitCode" PropertyName="ErrorCode" /&gt;
    &lt;/Exec&gt;
    &lt;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." /&gt;
  
    &lt;!-- 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. --&gt;
    &lt;Message Importance="high" Text="Performing first-run Webpack build..." /&gt;
    &lt;Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" /&gt;
    &lt;Exec Command="node node_modules/webpack/bin/webpack.js" /&gt;
  &lt;/Target&gt;
  
  &lt;Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish"&gt;
    &lt;!-- As part of publishing, ensure the JS resources are freshly built in production mode --&gt;
    &lt;Exec Command="npm install" /&gt;
    &lt;Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" /&gt;
    &lt;Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" /&gt;
  
    &lt;!-- Include the newly-built files in the publish output --&gt;
    &lt;ItemGroup&gt;
      &lt;DistFiles Include="wwwroot\dist\**" /&gt;
      &lt;ResolvedFileToPublish Include="@(DistFiles-&gt;'%(FullPath)')" Exclude="@(ResolvedFileToPublish)"&gt;
        &lt;RelativePath&gt;%(DistFiles.Identity)&lt;/RelativePath&gt;
        &lt;CopyToPublishDirectory&gt;PreserveNewest&lt;/CopyToPublishDirectory&gt;
      &lt;/ResolvedFileToPublish&gt;
    &lt;/ItemGroup&gt;
  &lt;/Target&gt;
  </code></pre>