visual-studioasp.net-coremsbuildmsbuild-target

Is there a way to hookup npm task while running aspnet core application in visual-studio?


I have an Asp.Net core application with WebpackDevMiddleware integrated to it. That WebpackDevMiddleware serves the client assets built with NPM command. Now I want to build and serve those client assests (JS/CSS) simultaneously when project runs.

The obvious solution I thought was to add (pre-build event command line) in project and provide npm command with it. The downside of that is when I build the docker image of my application, using dotnet core sdk, it asks for node environment which it doesn't have.

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
   <Exec Command="npm run build-webpack" />
</Target>

and this is my docker build step that doesn't recognize NPM command associated with dotnet build/publish

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 as builder
WORKDIR /app
COPY . .
RUN dotnet publish -c $ENVIRONMENT -o ../out

Now what I actually want is, my target only to be executed while running application in VS. A condition might work but what could it be?


Solution

  • Now what I actually want is, my target only to be executed while running application in VS. A condition might work but what could it be?

    Check this document: When building inside Visual Studio, the property $(BuildingInsideVisualStudio) is set to true. This can be used in your project or .targets files to cause the build to behave differently.

    So your target will look like:

    <Target Name="PreBuild" Condition="'$(BuildingInsideVisualStudio)'=='true'" BeforeTargets="PreBuildEvent">
       <Exec Command="npm run build-webpack" />
    </Target>
    

    Then this target will only execute when building in VS.