Visual studio - precompile - dotless

后端 未结 7 1598
萌比男神i
萌比男神i 2020-12-13 04:48

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

相关标签:
7条回答
  • 2020-12-13 05:27

    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>
    
    0 讨论(0)
  • 2020-12-13 05:27

    There is also another way to precompile during development.

    The dotless project features a commandline compiler (dotless.Compiler.exe) that can compile and minify the CSS.

    You can also use the compiler.exe with the --watch parameter where it will keep running and scan your input file for changes, regenerating whenever you make changes to the file. Thus making you independent from Visual Studio.

    0 讨论(0)
  • 2020-12-13 05:37

    Phil Haack to the rescue: http://haacked.com/archive/2009/12/02/t4-template-for-less-css.aspx

    Whenever you want to have something generated in your solution at compile time, T4 is usually the way to go...

    0 讨论(0)
  • 2020-12-13 05:37

    In my search for working with DotLess I also found this library:

    http://www.codethinked.com/post/2010/03/17/Bundler-Now-Supports-Css-And-less.aspx

    Adding it to my own question because it might help others.

    0 讨论(0)
  • 2020-12-13 05:37

    You may want to take a look at Chirpy. It has a lot more support than just LESS. I wish I would have found it prior to writing my own.

    Speaking of which I also wrote a Visual Studio Custom Build Tool that executes using the JS file (instead of the .NET port) you can take a look at the source here: https://github.com/paultyng/JsBuildTools

    Or it is also on the extensions gallery under JsBuildTools.

    0 讨论(0)
  • 2020-12-13 05:49

    All,

    After using just about all the alternatives discussed here and not being satisfied, I wrote a LessCss compiler addin for Visual Studio. It takes .less files and generates .css files only when the .less file changes. It uses the latest and greatest less.js compiler.

    See it in use here.

    Download the signed extension.

    Source code is here.

    I just submitted it to the VS extension gallery. Hopefully it will be up there soon but in the meantime please install (or compile then install) and check it out.

    0 讨论(0)
提交回复
热议问题