English 中文(简体)
Visual studio - precompile - dotless
原标题:

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.

最佳回答

Depending on your build environment, you can kick off dotless.Compiler.exe as a build task.

For example, using a Pre-Build task in Visual Studio (all 1 line):

$(SolutionDir)ToolsdotLessdotless.compiler.exe -m 
    $(ProjectDir)contentcsssite.less $(ProjectDir)contentcsssite.css

The macros ($(SolutionDir), etc) allow a bit of flexibility to project and file locations. Rather than using the standard .less files, simply reference the new .css files in your markup.

问题回答

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.

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...

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)packagesdotless.1.3.1.0	ooldotless.compiler.exe&quot; --minify --keep-first-comment @(LessCssRootInput)" />
</Target>

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.

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.

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.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签