简体   繁体   English

将less和css文件捆绑在一起

[英]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: 这意味着例如我的root风格,我想做类似以下的事情:

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. 但是, 对于捆绑较少文件自定义转换技术来说,这似乎并不能很好地工作,因为它看起来像将所有文件预先捆绑到单个css文件中,然后再将其传递给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? 有没有办法让bundle在同一个bundle中允许less和css文件?

Yes you are right, the transformer concatenates all the CSS/LESS contents before passing it along to the converter. 是的,你是对的,变换器在将所有CSS / LESS内容传递给转换器之前将其连接起来。 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. 也就是说,转换器不应该阻塞LESS和CSS的存在,因为你总是可以在LESS样式表中包含标准CSS。

The issue seems to be that you are using a StyleBundle object that already has a transformer associated. 问题似乎是您正在使用已经与变换器关联的StyleBundle对象。 Try using a generic Bundle as I do in my config, like this: 尝试使用通用的Bundle就像我在配置中一样,如下所示:

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. 至于你自己的答案,bootstrap.css文件的问题不是LESS转换器不喜欢它,而是@imports的路径问题,请确保你的LESS转换器使用某些东西来确保它解析正确的路径到任何依赖样式表。

I believe all CSS is valid LESS (but not all LESS is valid CSS) 我相信所有的CSS都是有效的(但不是所有的LESS都是有效的CSS)

So having the LessTransform applied to your bootstrap css files shouldn't make any difference. 因此,将LessTransform应用于您的bootstrap css文件应该没有任何区别。

It will still bundle all files into one css file to deliver to the browser. 它仍然会将所有文件捆绑到一个css文件中以传递给浏览器。

There might only be a performance problem during design-time . 设计时可能只存在性能问题。

The correct output will still be produced and caching will prevent any run-time performance issues. 仍将生成正确的输出,缓存将阻止任何运行时性能问题。

I know this is a bit of an old post, but I've noticed something weird that may be of use to someone... 我知道这是一个古老的帖子,但我注意到一些可能对某人有用的奇怪......

I've found that Bundle object doesn't like hyphens in the name of the css file when less files are also included. 我发现当包含较少的文件时, Bundle对象不喜欢css文件名中的连字符。 ie: 即:

var frontendStyles = new Bundle("~/bundles/frontend.main.css")
            .IncludeDirectory("~/Content/less", "*.less")
            .Include(
                "~/Content/ladda.css",
                "~/Content/jquery.mobile.custom.structure.min.css",
                "~/Content/jquery-ui.1.9.2.custom.css"
            );

Doesn't work for me, but the following does (change to jqueryui filename): 对我不起作用,但以下情况(更改为jqueryui文件名):

var frontendStyles = new Bundle("~/bundles/frontend.main.css")
            .IncludeDirectory("~/Content/less", "*.less")
            .Include(
                "~/Content/ladda.css",
                "~/Content/jquery.mobile.custom.structure.min.css",
                "~/Content/jqueryui.1.9.2.custom.css"
            );

So, changing the filename from "~/Content/bootstrap-responsive.css" to "~/Content/bootstrapresponsive.css" could very well sort the issue for you. 因此,将文件名从"~/Content/bootstrap-responsive.css"更改为"~/Content/bootstrapresponsive.css"可以很好地为您排序问题。

I had a bit more of a look into this and tried using Bundle instead of StyleBundle but still had issues. 我对此有了更深入的了解并尝试使用Bundle而不是StyleBundle,但仍然存在问题。 I think the issue is specifically to do with the bootstrap.css file having some syntax which less doesn't like. 我认为这个问题特别与bootstrap.css文件有关,该文件有一些不太喜欢的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM