requireJS 和 LESS
我正在使用 LESS 的客户端 javascript 版本来编译更少的代码,并且希望在最终的实时站点上继续使用它(我知道......形式很糟糕,但它使我能够允许用户自定义一些 less 变量,并让整个应用程序动态地使用该“主题”,因为它是一个一旦加载就永远不会刷新的 Web 应用程序,我认为编译 less 的额外加载时间是可以接受的)。
我也在使用 requireJS。
问题是:
A)如何让 requireJS 加载更少的代码?
B) 编译完成后 less 会分派任何事件吗? C
) 有没有办法触发 less 按命令重新编译?
谢谢。
I'm using the client-side javascript version of LESS to compile out less code, and would like to continue using this even on the final live site (I know... bad form, but it gives me the ability to allow users to customize a few less variables and have that "theme" their whole app on the fly, seeing as it's a webapp that once loaded never refreshes, I'm thinking the additional second of load time to compile the less is acceptable).
I'm also using requireJS.
The question is:
A) How do I get requireJS to load less code?
B) Does less dispatch any events when compilation is complete? and
C) Is there a way to trigger less to re-compile on command?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用了 RequireJS 的 文本加载器插件 将 .less 文件作为文本加载,然后创建一个新的 less。解析器实例来解析文本,然后自己添加样式文本:
您可以采用类似的方法,但为样式节点提供一个 ID 并删除该 ID,然后在需要时添加回另一个重新解析的 LESS 文本。
需要注意的是:当文本文件与网页位于同一域时,文本插件只能按需加载文本文件。如果您使用 RequireJS 优化器,您可以将文本内联到构建的、优化的 JS 文件中。
I have used the text loader plugin for RequireJS to load the .less file as text, then create a new less.Parser instance to parse the text, then add the style text myself:
You could take a similar approach, but give the style node an ID and remove that ID and then add back another reparsed LESS text when you want on demand.
A caveat: the text plugin can only load text files on demand when the text file is on the same domain as the web page. If you use the RequireJS optimizer, you can inline the text into a built, optimized JS file.
@jrburke:我根据您的代码组合了一个快速的 requirejs 插件:
“base/less”指向 less 源。您还可以提前加载它,并假设全局
less
对象存在。理想情况下,我想将 less Parser 对象拉入这个插件本身,这样它就不会创建全局的。使用这个我可以做类似的事情:
此时,global.less已被解析并添加到页面中,并返回指向样式元素的elem,以防我因某种原因需要删除或修改它。
有人有任何意见或知道更好的方法吗?
干杯
@jrburke: I've put together a quick requirejs plugin based on your code:
"base/less" points to the less source. You could also load this ahead of time, and assume the global
less
object exists. Ideally, I'd like to pull the less Parser object into this plugin itself so it doesn't create a global at all.Using this I can do stuff like:
At which point, global.less has been parsed and added to the page and gives back elem pointing to the style element in case I need to remove or modify it for some reason.
Does anyone have any input or know a better way of doing this?
Cheers
我在导入 @nicholas 插件时遇到了问题。我通过将文件的路径添加到搜索路径来修复它,并设置文件名以获得更好的错误消息:
I was having trouble with @nicholas plugin with imports. I fixed it by adding the path of the file to the search path, and set filename for better error messages: