使用 CoffeeScript / Cake 组合并缩小模板
我有一个充满胡子模板的 src/templates/ 目录。我如何组合和缩小这些内容,以便它们可以在我的 CoffeeScript 应用程序中使用?
我已经按照 https://github.com/jashkenas/coffee-script/wiki/%5BHowTo%5D-Compiling-and-Setting-Up-Build-Tools 用于将我的 CoffeeScript src 组合并缩小为 js。
I have a src/templates/
directory full of mustache templates. How would I combine and minify the contents of those, so they're available for use in my CoffeeScript app?
I'm already following the directions at https://github.com/jashkenas/coffee-script/wiki/%5BHowTo%5D-Compiling-and-Setting-Up-Build-Tools for combining and minifying my CoffeeScript src into js.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我假设您的模板正在导出到全局对象(例如,每个模板都执行
window.userpane =
而不仅仅是userpane =
)。这是最重要的事情。如果您这样做,并且连接并编译成功,那么剩下的唯一事情就是在每次连接后自动缩小。简短的回答:目前还没有好的工具。您最好的选择是使用如下行扩展现有的 Cakefile:
(To install UglifyJS,运行
npm install uglify-js
。)现在,这并不能解决确保脚本以合理的顺序连接的问题。 (例如,如果文件 A 中有
window.templates = {}
,文件 B 中有templates.userpane =
,那么文件 A 在文件之前连接起来非常重要B.) 为此,您应该关注 Sprockets,它可以让您在每个 JS 文件的顶部指示其依赖项是什么,然后按照尊重这些依赖关系的顺序将它们组合起来。 Sprockets 的创建者 Sam Stephenson 是 CoffeeScript 社区的活跃成员,Sprockets 中对 CoffeeScript 的一流支持将在 Sprockets 2 中提供(存储库 这里)。更新:这是一个 Cake 任务,用于实际读取和连接
template
目录中的所有内容:然后在连接的 JS 前面加上
templateJs
。请注意,这假设模板中没有单引号 ('
)。要么在它们前面加上反斜杠,要么始终使用双引号。First off, I'll assume that your templates are being exported to the global object (e.g. each one does
window.userpane =
rather than justuserpane =
). That's the most important thing. If you're doing that, and you're concatenating and compiling successfully, then the only thing left is to have automatic minification after each concatenation.Short answer: There's no good tool for this yet. Your best option is to extend your existing Cakefile with a line like
(To install UglifyJS, run
npm install uglify-js
.)Now, this won't solve the problem of ensuring that your scripts are concatenated in a sensible order. (For instance, if you have
window.templates = {}
in file A andtemplates.userpane =
in file B, then it's very important that file A be concatenated before file B.) For that, you should keep an eye on Sprockets, which lets you indicate at the top of each JS file what its dependencies are, then combine them in an order that respects those dependencies. The creator of Sprockets, Sam Stephenson, is an active member of the CoffeeScript community, and first-class support for CoffeeScript in Sprockets is coming in Sprockets 2 (repo here).Update: Here's a Cake task to do the actual reading and concatenating of everything in the
template
directory:Then prepend your concatenated JS with
templateJs
. Note that this assumes that there are no single quotes ('
) in the template. Either put backslashes in front of them or consistently use double quotes.