使用 LESS 及其 @import 时的 CSS 冗余

发布于 2024-08-18 21:43:27 字数 643 浏览 7 评论 0原文

我真的很喜欢 LESS 的想法和概念。然而我偶然发现了一个错误,我很早就向作者报告了该错误,但尚未得到任何反馈。也许只有我做错了事。

我的 application.less - 文件看起来与此类似:

@import "reset";
@import "config";
@import "header";
@import "forms";
[…]

我喜欢可以使用 @import 规则来拆分我的文件,以便更好地了解我的文件CSS 声明。然而,每个导入的文件都需要再次重新导入 config.less-File 才能使用我在其中定义的 mixin 和变量。

我打赌你已经知道我正在推动什么样的冗余:每次导入 config.less 时,它的“输出”都会成为 application.css 的一部分。

我的配置文件包含大约 200 行的代码。由于我将 CSS 分成大约 5 个需要重新导入配置的文件(基于我的控制器名称),因此我最终生成了大约 1000 行 100% 冗余的 CSS 代码。

我能想到的唯一解决方案是不要拆分我的文件,这是我真正想避免的。

I really like the idea and the concept of LESS. Yet I stumbled upon a bug, which i reported quite a while ago to the author but did not yet get any feedback. Maybe it's just me who is doing something wrong.

My application.less-File that looks similar to this:

@import "reset";
@import "config";
@import "header";
@import "forms";
[…]

I like that it is possible to use the @import rule to split up my files to gain a better overview of my css-declarations. Yet every imported file needs to re-import the config.less-File again to be able to make use of the mixins and variables i defined in there.

I bet you already know about what kind of redundancy I am driving at: Everytime the config.less is imported, its "output" becomes part of the application.css.

My config-file contains about 200 lines of code. Since I split up my CSS-into about 5 files (based on my controller names) that need to re-import the config, I end up having about 1000 lines of generated CSS-Code that are 100% redundant.

Only solution that I can come up with is not to split up my files, what I really like to avoid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

会傲 2024-08-25 21:43:27

尽管并不理想,但实际原因是理论上您导入的文件不需要包含任何 CSS。通常,您会有变量和动态混合,它们不会对 CSS 输出产生任何影响:

lib.less:

#colors {
    @blue: #0011ff;
    @red: #ee2222;
}
.button (@width: 10px) {...}

main.less:

@import "lib";

a { color: #colors[@blue]; }

output、main.css:

a { color: #0011ff; }

#colors {} 和 .button 不会在这种情况下被输出。

Although not ideal, the practical reason for this is that the files you import theoretically don't need to contain any CSS. Typically, you would have variables and dynamic mixins, which don't contribute to your CSS output:

lib.less:

#colors {
    @blue: #0011ff;
    @red: #ee2222;
}
.button (@width: 10px) {...}

main.less:

@import "lib";

a { color: #colors[@blue]; }

output, main.css:

a { color: #0011ff; }

#colors {} and .button will not be output in this case.

窗影残 2024-08-25 21:43:27

LESS 现在支持 @import-once "stylename.less";

LESS now supports @import-once "stylename.less";

爱格式化 2024-08-25 21:43:27

也许您可以在开发环境中将它们分开,然后在部署到实时 Web 服务器时将它们合并在一起,而不需要所有额外的代码?

Maybe you can split them up in your development environment and then merge them together, not needing all the extra code, when you deploy to your live web server?

冰之心 2024-08-25 21:43:27

如果使用 $ 而不是 . 声明和混合动态混合,则可以在 LESS 配置文件中使用它们。

在 config.less 中:

$mixin
{
  a { color: @light; }
  h2 { //etc.
}

在 header.less 中:

@import "config";
.header
{
  $mixin;
}

来源。我也尝试过这个并且有效。

You can use dynamic mixins in your LESS config file if they are declared and mixed-in using $ instead of ..

In config.less:

$mixin
{
  a { color: @light; }
  h2 { //etc.
}

In header.less:

@import "config";
.header
{
  $mixin;
}

Source. I've also tried this and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文