Visual studio - precompile - dotless

2019-01-21 14:01发布

I wonder if there is a way to precompile *.less files(http://www.dotlesscss.org/) with visual studio.

The site gives me a dotless.compiler.exe but I am not sure how to hook this up to visual studio. I am looking for a solution for both Webforms and ASP.NET MVC.

7条回答
冷血范
2楼-- · 2019-01-21 14:34

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="&quot;$(SolutionDir)\packages\dotless.1.3.1.0\tool\dotless.compiler.exe&quot; --minify --keep-first-comment @(LessCssRootInput)" />
</Target>
查看更多
登录 后发表回答