Supose我有這個簡單但還算嵌套生態模板移除不必要的空格:如何從編譯生態模板
<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
中移除這些不必要的空格輸出?
順便說一句,我使用鏈輪編譯,連接和服務這些資產。
我還沒有使用生態環境,沒有一個可以嘗試任何東西的環境。你能看到是否在''中包含不需要的空白有幫助嗎? –
感謝您的建議@AtesGoral,但不幸的是,這並沒有太大的幫助,因爲在JS輸出中看到的空格是因爲模板中使用的縮進(我編輯了我的問題評論)。在添加不必要的HTML註釋塊之前,我寧願刪除soruce模板的縮進,但這是遠遠不夠理想的解決方案(可能有一種方法可以在.eco - > .js編譯時刪除它們)。 – epidemian
如果Eco尊重XML評論,它可能仍然有效。我會發布我的意思作爲答案。 –