将多个 Coffeescript 文件合并为一个文件? (多个子目录)
我有一堆 .coffee 文件,需要将它们合并到一个文件中。
我像 Rails 应用程序一样设置了文件夹:
/src/controller/log_controller.coffee
/src/model/log.coffee
/src/views/logs/new.coffee
Coffeescript 有一个命令,可以让您将多个 Coffeescript 加入到一个文件中,但它似乎只适用于一个目录。例如,这工作正常:
coffee --output app/controllers.js --join --compile src/controllers/*.coffee
但我需要能够包含一堆子目录,就像这个不起作用的命令:
coffee --output app/all.js --join --compile src/*/*.coffee
有办法做到这一点吗?有没有一种UNIXy方式来传递子目录中所有文件的列表?
我在 OSX 中使用终端。
它们都必须加入到一个文件中,否则每个单独的文件都会被编译和编译。用 this 包裹:
(function() { }).call(this);
这会破坏某些函数调用的范围。
I've got a bunch of .coffee files that I need to join into one file.
I have folders set up like a rails app:
/src/controller/log_controller.coffee
/src/model/log.coffee
/src/views/logs/new.coffee
Coffeescript has a command that lets you join multiple coffeescripts into one file, but it only seems to work with one directory. For example this works fine:
coffee --output app/controllers.js --join --compile src/controllers/*.coffee
But I need to be able to include a bunch of subdirectories kind of like this non-working command:
coffee --output app/all.js --join --compile src/*/*.coffee
Is there a way to do this? Is there a UNIXy way to pass in a list of all the files in the subdirectories?
I'm using terminal in OSX.
They all have to be joined in one file because otherwise each separate file gets compiled & wrapped with this:
(function() { }).call(this);
Which breaks the scope of some function calls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 CoffeeScript 文档:
因此,您可以在命令行(我使用 bash)中实现您的目标,如下所示:
其中 file1 - fileN 是您要编译的 CoffeeScript 文件的路径。
From the CoffeeScript documentation:
So, you can achieve your goal at the command line (I use bash) like this:
where file1 - fileN are the paths to the coffeescript files you want to compile.
您可以先编写一个 shell 脚本或 Rake 任务将它们组合在一起,然后进行编译。类似于:
find 。 -type f -name '*.coffee' -print0 | xargs -0 猫 > output.coffee
然后编译
output.coffee
根据您的需要调整路径。另请确保
output.coffee
文件不在您使用find
搜索的同一路径中,否则您将陷入无限循环。http://man.cx/find |
http://www.rubyrake.org/tutorial/index.html
此外,您还可以对 Stackoverflow 上有关跨目录搜索的其他帖子感兴趣:
You could write a shell script or Rake task to combine them together first, then compile. Something like:
find . -type f -name '*.coffee' -print0 | xargs -0 cat > output.coffee
Then compile
output.coffee
Adjust the paths to your needs. Also make sure that the
output.coffee
file is not in the same path you're searching withfind
or you will get into an infinite loop.http://man.cx/find |
http://www.rubyrake.org/tutorial/index.html
Additionally you may be interested in these other posts on Stackoverflow concerning searching across directories:
我刚刚发布了 CoffeeToaster 的 alpha 版本,我认为它可能对您有所帮助。
http://github.com/serpentem/coffee-toaster
I've just release an alpha release of CoffeeToaster, I think it may help you.
http://github.com/serpentem/coffee-toaster
使用咖啡命令行工具的最简单方法。
Coffee --output public --join --compile app
app 是我的工作目录,包含多个子目录,而 public 是放置 ~output.js 文件的位置。如果在 Nodejs 中编写应用程序,则可以轻松实现此过程的自动化
The most easy way to use coffee command line tool.
coffee --output public --join --compile app
app is my working directory holding multiple subdirectories and public is where ~output.js file will be placed. Easy to automate this process if writing app in nodejs
这对我很有帮助(
-o
输出目录,-j
加入到project.js,-cw
编译并全面查看 Coffeescript 目录):This helped me (
-o
output directory,-j
join to project.js,-cw
compile and watch coffeescript directory in full depth):使用 cake 编译它们全部包含在一个(或多个)生成的 .js 文件中。 Cakefile 用作配置,控制咖啡脚本的编译顺序 - 对于较大的项目非常方便。
Cake 非常容易安装和设置,在编辑项目时从 vim 调用 cake 非常简单
,您可以刷新浏览器并查看结果。
由于我也忙于学习构建文件的最佳方法以及将咖啡脚本与主干和蛋糕结合使用,因此我创建了一个 github上的小项目作为我自己的参考,也许它也会对你在蛋糕和一些基本的事情上有所帮助。所有编译后的文件都位于www文件夹中,以便您可以在浏览器中打开它们,所有源文件(除了蛋糕配置)都位于src文件夹中。在此示例中,所有 .coffee 文件均被编译并合并为一个输出 .js 文件,然后该文件包含在 html 中。
Use cake to compile them all in one (or more) resulting .js file(s). Cakefile is used as configuration which controls in which order your coffee scripts are compiled - quite handy with bigger projects.
Cake is quite easy to install and setup, invoking cake from vim while you are editing your project is then simply
and you can refresh your browser and see results.
As I'm also busy to learn the best way of structuring the files and use coffeescript in combination with backbone and cake, I have created a small project on github to keep it as a reference for myself, maybe it will help you too around cake and some basic things. All compiled files are in www folder so that you can open them in your browser and all source files (except for cake configuration) are in src folder. In this example, all .coffee files are compiled and combined in one output .js file which is then included in html.
或者,您可以使用
--bare
标志,编译为 JavaScript,然后在必要时包装 JS。但这可能会产生问题;例如,如果您有一个包含代码的文件,那么生成的 JavaScript 中只有一个
var i
声明,并且i
将递增。但是,如果您将foo
函数声明移动到另一个 CoffeeScript 文件,那么它的i
将位于foo
范围内,而外部我
不会受到影响。因此,连接 CoffeeScript 是一个更明智的解决方案,但仍然有可能造成混乱;连接代码的顺序几乎肯定很重要。我强烈建议您模块化您的代码。
Alternatively, you could use the
--bare
flag, compile to JavaScript, and then perhaps wrap the JS if necessary. But this would likely create problems; for instance, if you have one file with the codethen there's only one
var i
declaration in the resulting JavaScript, andi
will be incremented. But if you moved thefoo
function declaration to another CoffeeScript file, then itsi
would live in thefoo
scope, and the outeri
would be unaffected.So concatenating the CoffeeScript is a wiser solution, but there's still potential for confusion there; the order in which you concatenate your code is almost certainly going to matter. I strongly recommend modularizing your code instead.