requireJS 和 LESS

发布于 2024-11-05 09:58:35 字数 328 浏览 4 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

梦里梦着梦中梦 2024-11-12 09:58:35

我使用了 RequireJS 的 文本加载器插件 将 .less 文件作为文本加载,然后创建一个新的 less。解析器实例来解析文本,然后自己添加样式文本:

(new less.Parser()).parse(lessText, function (err, css) {
  if (err) {
    if (typeof console !== 'undefined' && console.error) {
      console.error(err);
    }
  } else {
    var style = document.createElement('style');
    style.type = 'text/css';
    style.textContent = css.toCSS();
  }
});

您可以采用类似的方法,但为样式节点提供一个 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:

(new less.Parser()).parse(lessText, function (err, css) {
  if (err) {
    if (typeof console !== 'undefined' && console.error) {
      console.error(err);
    }
  } else {
    var style = document.createElement('style');
    style.type = 'text/css';
    style.textContent = css.toCSS();
  }
});

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.

怎会甘心 2024-11-12 09:58:35

@jrburke:我根据您的代码组合了一个快速的 requirejs 插件:

define({

    version: '0.1',

    load: function(name, req, onLoad, config) {

        req(['text!' + name, 'base/less'], function(lessText) {

            var styleElem;

            (new less.Parser()).parse(lessText, function (err, css) {
                if (err) {
                    if (typeof console !== 'undefined' && console.error) {
                        console.error(err);
                    }
                } else {
                    styleElem = document.createElement('style');
                    styleElem.type = 'text/css';

                    if (styleElem.styleSheet) 
                        styleElem.styleSheet.cssText = css.toCSS();
                    else 
                        styleElem.appendChild( document.createTextNode( css.toCSS() ) );

                    document.getElementsByTagName("head")[0].appendChild( styleElem );
                }

                onLoad(styleElem);
            });

        });     
    }

});

“base/less”指向 less 源。您还可以提前加载它,并假设全局 less 对象存在。理想情况下,我想将 less Parser 对象拉入这个插件本身,这样它就不会创建全局的。

使用这个我可以做类似的事情:

require(['less!global.less'], function(elem) {

});

此时,global.less已被解析并添加到页面中,并返回指向样式元素的elem,以防我因某种原因需要删除或修改它。

有人有任何意见或知道更好的方法吗?

干杯

@jrburke: I've put together a quick requirejs plugin based on your code:

define({

    version: '0.1',

    load: function(name, req, onLoad, config) {

        req(['text!' + name, 'base/less'], function(lessText) {

            var styleElem;

            (new less.Parser()).parse(lessText, function (err, css) {
                if (err) {
                    if (typeof console !== 'undefined' && console.error) {
                        console.error(err);
                    }
                } else {
                    styleElem = document.createElement('style');
                    styleElem.type = 'text/css';

                    if (styleElem.styleSheet) 
                        styleElem.styleSheet.cssText = css.toCSS();
                    else 
                        styleElem.appendChild( document.createTextNode( css.toCSS() ) );

                    document.getElementsByTagName("head")[0].appendChild( styleElem );
                }

                onLoad(styleElem);
            });

        });     
    }

});

"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:

require(['less!global.less'], function(elem) {

});

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

沐歌 2024-11-12 09:58:35

我在导入 @nicholas 插件时遇到了问题。我通过将文件的路径添加到搜索路径来修复它,并设置文件名以获得更好的错误消息:

// http://stackoverflow.com/questions/5889901/requirejs-and-less
// enables require(['share/less!whatever.less'], function(elem) {});
define({
    version: '0.1',
    load: function(name, req, onLoad, config) {
        req(['share/text!' + name, 'share/less-1.3.0'], function(lessText) {
            var styleElem;
            var parser = new(less.Parser)({
                filename: name,
                paths: [name.split('/').slice(0,-1).join('/') + '/'],
            });
            parser.parse(lessText, function (err, css) {
                if (err) {
                    if (typeof console !== 'undefined' && console.error) {
                        console.error(err);
                    }
                } else {
                    styleElem = document.createElement('style');
                    styleElem.type = 'text/css';

                    if (styleElem.styleSheet)
                        styleElem.styleSheet.cssText = css.toCSS();
                    else
                        styleElem.appendChild( document.createTextNode( css.toCSS() ) );

                    document.getElementsByTagName("head")[0].appendChild( styleElem );
                }
                onLoad(styleElem);
            });
        });
    }
});

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:

// http://stackoverflow.com/questions/5889901/requirejs-and-less
// enables require(['share/less!whatever.less'], function(elem) {});
define({
    version: '0.1',
    load: function(name, req, onLoad, config) {
        req(['share/text!' + name, 'share/less-1.3.0'], function(lessText) {
            var styleElem;
            var parser = new(less.Parser)({
                filename: name,
                paths: [name.split('/').slice(0,-1).join('/') + '/'],
            });
            parser.parse(lessText, function (err, css) {
                if (err) {
                    if (typeof console !== 'undefined' && console.error) {
                        console.error(err);
                    }
                } else {
                    styleElem = document.createElement('style');
                    styleElem.type = 'text/css';

                    if (styleElem.styleSheet)
                        styleElem.styleSheet.cssText = css.toCSS();
                    else
                        styleElem.appendChild( document.createTextNode( css.toCSS() ) );

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