使用 LESS 及其 @import 时的 CSS 冗余
我真的很喜欢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽管并不理想,但实际原因是理论上您导入的文件不需要包含任何 CSS。通常,您会有变量和动态混合,它们不会对 CSS 输出产生任何影响:
lib.less:
main.less:
output、main.css:
#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:
main.less:
output, main.css:
#colors {} and .button
will not be output in this case.LESS 现在支持
@import-once "stylename.less";
LESS now supports
@import-once "stylename.less";
也许您可以在开发环境中将它们分开,然后在部署到实时 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?
如果使用
$
而不是.
声明和混合动态混合,则可以在 LESS 配置文件中使用它们。在 config.less 中:
在 header.less 中:
来源。我也尝试过这个并且有效。
You can use dynamic mixins in your LESS config file if they are declared and mixed-in using
$
instead of.
.In config.less:
In header.less:
Source. I've also tried this and it works.