使用 CoffeeScript / Cake 组合并缩小模板

发布于 2024-11-03 08:11:12 字数 361 浏览 2 评论 0原文

我有一个充满胡子模板的 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 技术交流群。

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

发布评论

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

评论(1

花心好男孩 2024-11-10 08:11:12

首先,我假设您的模板正在导出到全局对象(例如,每个模板都执行 window.userpane = 而不仅仅是 userpane =)。这是最重要的事情。如果您这样做,并且连接并编译成功,那么剩下的唯一事情就是在每次连接后自动缩小。

简短的回答:目前还没有好的工具。您最好的选择是使用如下行扩展现有的 Cakefile:

fs.watchFile 'concatenated.js', ->
  exec 'uglifyjs concatenated.js'

(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 目录中的所有内容:

templateJs = ''
files = fs.readdirSync 'template'
for file in files
  contents = fs.readFileSync file, 'utf8'
  name = file.replace /\..*/, '' # remove extension
  templateJs += "window.#{name} = '#{contents}';"

然后在连接的 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 just userpane =). 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

fs.watchFile 'concatenated.js', ->
  exec 'uglifyjs concatenated.js'

(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 and templates.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:

templateJs = ''
files = fs.readdirSync 'template'
for file in files
  contents = fs.readFileSync file, 'utf8'
  name = file.replace /\..*/, '' # remove extension
  templateJs += "window.#{name} = '#{contents}';"

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.

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