【问题标题】:Dotless - Can't reference less variable in separate file with MVC BundlingDotless - 无法使用 MVC 捆绑在单独的文件中引用较少的变量
【发布时间】:2012-11-04 09:31:12
【问题描述】:

我希望我没有创建重复的主题,但是我已经搜索了两天,找不到解决方案。

我们正在 MVC4 中启动一个新项目,并且我们加载了较少版本的 bootstrap。我遇到的问题是当我尝试引用bootstrap.lessglobal.less 或当前文件之外的任何内容中的某些类或变量时。我可以在当前文件的顶部创建一个变量并很好地使用它,但是如果我想使用单独文件中的某些东西,我必须@import它。如果我的整个应用程序的 less 在一个文件中,这会很好,但我必须 @import 4+ 个文件到我创建的每个 page/sectionless 文件中。

我添加了来自 https://gist.github.com/2002958 的 MVC4 捆绑添加

在我看来,问题是每个文件都被独立评估并转换为 css。我试图简化这个过程并从 less 包中的所有文件构建一个大量的 less 字符串,然后将它们转换 (Less.Parse(lessString)),但我收到了错误:

“您正在导入一个无法找到的以 .less 结尾的文件”

所以这是我的终极问题:有没有办法在不引用物理文件的情况下简单地解析更少的字符串?这将解决我的问题。

如果由于某些奇怪的原因无法做到这一点,是否有一个我不知道的组件或流程已经在我不知道的地方实际将文件捆绑在一起,然后再缩小它们?

任何关于这个主题的光都将不胜感激,因为我正在转圈试图让它发挥作用。

这个问题也发在Dotless群里:
https://groups.google.com/forum/?fromgroups#!topic/dotless/j-8OP1dNjUY

【问题讨论】:

  • 你找到解决办法了吗?
  • 不幸的是,我从来没有这样做过。我花了比我认为明智的更多的时间,然后继续前进。由于没有解决这个问题,我在最近的几个项目中并没有少用。它似乎不是满足我需求的可行选择。

标签: c# less bundling-and-minification dotless


【解决方案1】:

这个解决方案对我有用:

我有两个 NuGet 包:
- dotless
- dotless adapter for system.web.optimization

在我的web.config 中,我有以下几行:

<configuration>
    <configSections>
        <section name="dotless" 
                 type="dotless.Core.configuration.DotlessConfigurationSectionHandler, dotless.Core" />
    </configSections>
    <system.web>
        <httpHandlers>
            <add path="*.less"
                 verb="GET"
                 type="dotless.Core.LessCssHttpHandler, dotless.Core" />
        </httpHandlers>
    </system.web>
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
            <add name="dotless" 
                 path="*.less" 
                 verb="GET" 
                 type="dotless.Core.LessCssHttpHandler,dotless.Core" 
                 resourceType="File" 
                 preCondition="" />
        </handlers>
    </system.webServer>
    <dotless minifyCss="true" cache="true" web="false" disableParameters="true" />
</configuration>

请注意,您应该根据需要定义无点参数。

BundleConfig.cs:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new LessBundle("~/bundles/styles/").Include("~/Content/site.less"));
    BundleTable.EnableOptimizations = true; // false if you want to debug css
}

最后,Site.less:

/* I have to redefine some bootstrap mixins and variables,
   so importing bootstrap and extending happens here: */
@import "bootstrap-extends.less";

/* all other needed less should be included here too, for example:
   @import "admin.less";
   @import "controls.less";
   etc
*/

body {

}

site.lessbootstrap-extends.less 位于 Content 文件夹内。 bootstrap-extends 包含所有需要的@import 指令,通常在~/Content/bootstrap/bootstrap.less 中列出。

【讨论】:

    【解决方案2】:

    你查看过LESS bundle transformer吗?

    【讨论】:

      猜你喜欢
      • 2014-01-21
      • 2017-11-19
      • 2012-11-14
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多