将 Closure 编译器与 Underscore.js _.template 一起使用

发布于 2024-12-14 07:22:42 字数 1081 浏览 3 评论 0原文

有什么方法可以在服务器上编译 Underscore.js 模板并让 Closure 编译器使用生成的代码吗?

主要问题是 _.template:

_.template = function(str, data) {
    var c  = _.templateSettings;
    var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
        'with(obj||{}){__p.push(\'' +
        str.replace(/\\/g, '\\\\')
        .replace(/'/g, "\\'")
        .replace(c.interpolate, function(match, code) {
            return "'," + code.replace(/\\'/g, "'") + ",'";
        })
        .replace(c.evaluate || null, function(match, code) {
            return "');" + code.replace(/\\'/g, "'")
                .replace(/[\r\n\t]/g, ' ') + "__p.push('";
        })
        .replace(/\r/g, '\\r')
        .replace(/\n/g, '\\n')
        .replace(/\t/g, '\\t')
        + "');}return __p.join('');";
    var func = new Function('obj', tmpl);
    return data ? func(data) : func;
};

生成带有 with 语句的 JavaScript。两个明显的路线是:

  1. 修改 Underscore.js 的 _.template 不生成强制
  2. 闭包

第二种选择可能吗?

Is there any way to compile Underscore.js templates on the server and get the Closure compiler to work with the generated code?

The main problem is that _.template:

_.template = function(str, data) {
    var c  = _.templateSettings;
    var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
        'with(obj||{}){__p.push(\'' +
        str.replace(/\\/g, '\\\\')
        .replace(/'/g, "\\'")
        .replace(c.interpolate, function(match, code) {
            return "'," + code.replace(/\\'/g, "'") + ",'";
        })
        .replace(c.evaluate || null, function(match, code) {
            return "');" + code.replace(/\\'/g, "'")
                .replace(/[\r\n\t]/g, ' ') + "__p.push('";
        })
        .replace(/\r/g, '\\r')
        .replace(/\n/g, '\\n')
        .replace(/\t/g, '\\t')
        + "');}return __p.join('');";
    var func = new Function('obj', tmpl);
    return data ? func(data) : func;
};

generates JavaScript with a with-statement in it. The two obvious routes are:

  1. modify Underscore.js's _.template not to generate withs
  2. coerce Closure into playing nice

Is the second option possible?

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

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

发布评论

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

评论(1

〆一缕阳光ご 2024-12-21 07:22:42

一般来说,JS 引擎在没有“with”的情况下表现得更好,因此如果生成它而不用“with”是一个选项,这可能是最好的解决方案。

否则,您的选择取决于您是否希望使用闭包编译器高级模式。在 SIMPLE 模式下,编译器不会重命名模板上的属性,并假定任何未声明的变量都是全局变量。因此,只要您的模板对象不会导致任何局部变量被隐藏,它就可能“正常工作”。

Generally, the JS engines perform better without "with", so if generating it without "with" is an option that is likely the best solution.

Otherwise your options depend on whether you are hoping to using Closure Compilers ADVANCED mode. In SIMPLE mode, the compiler won't rename your properties on the template, and will assume that any undeclared variable are globals. So as long are your template object isn't causing any local variables to be shadowed it might "just work".

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