Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundling less and css files together

I feel like bundling should be used to group a bunch of files which you use together into one single delivery to the browser. This would mean for example for my root style I would like to do something like the following:

var bundle = new StyleBundle("~/Content/style").Include(
    "~/Content/mystyle.less",
    "~/Content/bootstrap.css",
    "~/Content/bootstrap-responsive.css");
bundle.Transforms.Add(new LessTransform());
bundle.Transforms.Add(new CssMinify());
bundles.Add(bundle);    

But this doesn't seem to work very well with the custom transform technique for bundling less files as it looks like it pre-bundles all of the files into a single css file before passing it to the LessTransform. From what i can tell this method only works if you bundle all of your less files together.

Is there a way to get bundles to allow both less and css files in the same bundle?

like image 613
Not loved Avatar asked Sep 30 '12 11:09

Not loved


People also ask

What is CSS bundling?

Bundling is the process of rolling up a number of distinct resources together into a single downloadable resource. For example, a bundle may consist of multiple JavaScript or CSS files you bring down to the local machine by making a single HTTP request to an ad hoc endpoint.

What is difference between bundling and minification?

Bundling is one of the features of MVC. By implementing this, we can improve performance request load time. Minification is the process of removing unnecessary data without changing its functionality such as removing white spaces, comments, converting the large variable names to small, etc.

Which of the following options allows you to combine multiple files such as CSS and JavaScript into a single file?

Bundling is a new feature in ASP.NET 4.5 that makes it easy to combine or bundle multiple files into a single file. You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load performance.

How do I put all CSS in one file?

In your HTML header, you have two options: 1) place the HTML with CSS directly into your HTML header or 2) put the Standalone CSS into mycssfile. css and include that into your HTML header as <link href="mycssfile. css" /> . Either option depends on your preference.


1 Answers

Yes you are right, the transformer concatenates all the CSS/LESS contents before passing it along to the converter. That said, the converter should not be choking the presence of both LESS and CSS, as you can always include standard CSS within a LESS stylesheet.

The issue seems to be that you are using a StyleBundle object that already has a transformer associated. Try using a generic Bundle as I do in my config, like this:

var customStyles = new Bundle("~/bundle/style/css")
                       .Include("~/Content/normalize.css","~/Content/*.less");
bootstrapStyles.Transforms.Add(new LessTransform());
bootstrapStyles.Transforms.Add(new CssMinify());
bundles.Add(customStyles);

As for your own answer below, the issue with the bootstrap.css file is not that the LESS transformer doesn't like it, but probably a path issue for @imports, make sure your LESS transformer uses something to ensure it resolves the proper paths to the any dependency stylesheets.

like image 62
one.beat.consumer Avatar answered Sep 29 '22 23:09

one.beat.consumer