假设我有这个简单但相当嵌套的 Eco 模板:
<div class="example">
<% for thing, i in @things: %>
<div class="nested">
<% if i % 2 == 0: %>
This block is fairly nested.
<% end %>
</div>
<% end %>
</div>
编译成JS结果是:
function(__obj) {
// ... A couple of auxiliary functions ...
(function() {
(function() {
var i, thing, _i, _len, _ref;
__out.push('<div class="example">\n ');
_ref = this.things;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
thing = _ref[i];
__out.push('\n <div class="nested">\n ');
if (i % 2 === 0) {
__out.push('\n This block is fairly nested.\n ');
}
__out.push('\n </div>\n ');
}
__out.push('\n</div>\n');
}).call(this);
}).call(__obj);
__obj.safe = __objSafe, __obj.escape = __escape;
return __out.join('');
}
现在,此函数(作为 JS 提供给客户端以进行客户端渲染)在字符串中包含一些不必要的空格,例如 ...
`'\n This block is fairly nested.\n '`
... 不能被 JS 压缩器删除,因为它们不是 JS 空白(但在渲染时变成 HTML 空白)。我知道 Eco 以这种方式编译模板以保持它们的输出很好地缩进,这在开发环境中很酷,但在生产环境中就不是那么多了:D
有没有办法从 eco.precompile
输出中删除这些不必要的空格?
顺便说一句,我正在使用 Sprockets 来编译、连接和提供这些 Assets 。
最佳答案
如果这样写会怎样
<div class="example">
<% for thing, i in @things: %>
<div class="nested"><%= "fairly nested" if i % 2 is 0 %></div>
<% end %>
</div>
如 ECO 模板中的建议 README ,在关于空白的注释下。
关于javascript - 如何从已编译的 Eco 模板中删除不必要的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10521878/