Here's the solution I came up with, using MSBuild. It's incremental, so it should only happen when the Less changes. It also correctly handles @import
.
First, add dotless to your project with NuGet. You don't need any of the magic it adds to your web.config
, so you can revert that - you're just using it to get the compiler executable.
Next, add your "root" Less files to your .csproj
, like so:
<ItemGroup>
<LessCssRootInput Include="example.less" />
</ItemGroup>
Finally, add this snippet at the bottom of your .csproj
:
<ItemGroup>
<LessCssSubInput Include="**\*.less" Exclude="@(LessCssRootInput)" />
<LessCssOutput Include="@(LessCssRootInput -> '%(RelativeDir)\%(Filename).css')" />
</ItemGroup>
<Target Name="CompileLessCss" BeforeTargets="Compile" Inputs="@(LessCssRootInput);@(LessCssSubInput)" Outputs="@(LessCssOutput)">
<Exec Command=""$(SolutionDir)\packages\dotless.1.3.1.0\tool\dotless.compiler.exe" --minify --keep-first-comment @(LessCssRootInput)" />
</Target>