excubo-ag / WebCompiler

Apache License 2.0
151 stars 30 forks source link

Re-Compile on save with changes in csproj instead of manually running dotnet watch in a seperate terminal #90

Closed shv07 closed 2 months ago

shv07 commented 7 months ago

Hi,

Is it possible to recompile scss on save by making changes in the csproj instead of running dotnet watch separately in a terminal? I tried to add this to csproj without any success. During the build, the project keeps getting restored and ultimately my visual studio crashes.

<Target Name="CompileStaticAssets" BeforeTargets="PreBuildEvent">
    <Exec Command="dotnet watch tool run webcompiler -r wwwroot -c webcompilerconfiguration.json" StandardOutputImportance="high" />
</Target>
stefanloerwald commented 7 months ago

Have you tried following the Readme? https://github.com/excubo-ag/WebCompiler?tab=readme-ov-file#msbuild

This should be a good starting point for you. If MSBuild supports build on save, then this should work out of the box.

shv07 commented 7 months ago

Thanks for the prompt reply. I had tried the approach in Readme as well. It didn't work (tried in both visual studio and in vscode with c# devkit. With that code, even the scss changes are not recompiled unless I do a rebuild.

However, if I modify the csproj to have BeforeTargets="PreBuildEvent" in both places, then the compilation takes place without re-build on just running the project but still not on making changes during the debugging process.

My csproj looks like this:-

<Target Name="ToolRestore" BeforeTargets="PreBuildEvent">
 <Exec Command="dotnet tool restore" StandardOutputImportance="high" />
</Target>
<Target Name="CompileStaticAssets" AfterTargets="AfterBuild">
 <Exec Command="dotnet webcompiler -r wwwroot -c webcompilerconfiguration.json" StandardOutputImportance="high" />
</Target>
stefanloerwald commented 7 months ago

Thanks for your reply.

I would suggest asking this on stackoverflow, as there will be a larger crowd familiar with MSBuild. Ultimately, you want to invoke an arbitrary command (webcompiler in this case) on changes to a set of files (scss files). I'd expect this to be a supported use case in MSBuild.

shv07 commented 7 months ago

Sure, will do that. I am using the cli meanwhile for updating the compilation during debugging.