在生产站点中使用 less.js(LESS 样式表的客户端 javascript 编译器)会是一种不好的做法吗?
该脚本基本上将 .less
代码转换为 .css
代码。我认为只有当用户关闭了 javascript 时才会出现问题,但是很多人都会这样做吗?
有什么建议吗?
This script basically turns .less
code into .css
code. I think it will only cause problems if the user has javascript turned off, but do many people do that?
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将
.less
编译为.css
,然后再将其放入生产环境即可。这样它仍然会向那些没有启用 JS 的人显示,并且不会遇到 @Plynx 提到的问题。至于编译,有一些实时编译器,特别是:
都将
.less
编译为.css
在文件保存上,这太棒了,并且使 LESS 更易于使用!Just compile the
.less
into.css
before putting it into a production environment. That way it will still display to those without JS enabled, and won't suffer the issues @Plynx mentioned.As for compiling, there are some real-time compilers out there, specifically:
Both compile
.less
to.css
on file save, which is awesome, and makes LESS easier to use!很多人都这样做(我没有确切的数字,但 IIRC 小于 4%),但您没有义务在没有 JavaScript 的情况下提供功能。如今许多更先进的网站都没有。这取决于您所服务的内容以及您的受众是谁。
当然,如果可以的话,提供这个功能就太好了。我更关心 less.js 对网站加载时间的影响。如果在 javascript 之后才能加载 CSS,则必须延迟渲染直到 javascript 运行,以避免从无样式到样式的不恰当转换。
通过使用监视目录的工具,只要 .less 文件发生更改,您就可以将它们自动编译为 CSS。
Many people do (I don't have exact numbers, but IIRC less than 4%), but you are under no obligation to provide functionality without javascript. Many of the more advanced sites today do not. It depends on what you are serving and who your audience is.
Of course it's great to provide this functionality if you can. I would be more concerned about the effect less.js will have on your site's load time. If you can't load the CSS until after the javascript, you have to delay rendering until the javascript runs, in order to avoid an unseemly transition from unstyled to styled.
You can have your .less files auto-compile into CSS whenever they are changed by using tools that watch directories.
这实际上取决于您正在构建的内容以及您的目标受众是谁。支持 HTML5 的现代浏览器将在第一次加载时缓存生成的 CSS,因此如果大多数访问者将使用现代浏览器,那么这是非常安全的。
It really depends on what you're building and who your target audience is. Modern browsers that support HTML5 will cache the generated CSS the first time it loads so if the majority of your visitors will be using modern browsers it's pretty safe.
其他选项是在服务器中制作您自己的 less 编译器,然后您可以“玩”缓存时间 css 结果。
为了做到这一点,您可以检查 LessPHP 库。
Other options is making your own less compiler in server and then you can "play" with cache times css results.
You can check libraries as LessPHP in order to do that.