将多个 Coffeescript 文件合并为一个文件? (多个子目录)

发布于 2024-10-09 19:14:06 字数 682 浏览 4 评论 0原文

我有一堆 .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 技术交流群。

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

发布评论

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

评论(7

起风了 2024-10-16 19:14:06

来自 CoffeeScript 文档

-j, --join [FILE] :在编译之前,将所有脚本按照传递的顺序连接在一起,并将它们写入指定的文件中。对于构建大型项目很有用。

因此,您可以在命令行(我使用 bash)中实现您的目标,如下所示:

coffee -cj path/to/compiled/file.js file1 file2 file3 file4

其中 file1 - fileN 是您要编译的 CoffeeScript 文件的路径。

From the CoffeeScript documentation:

-j, --join [FILE] : Before compiling, concatenate all scripts together in the order they were passed, and write them into the specified file. Useful for building large projects.

So, you can achieve your goal at the command line (I use bash) like this:

coffee -cj path/to/compiled/file.js file1 file2 file3 file4

where file1 - fileN are the paths to the coffeescript files you want to compile.

一影成城 2024-10-16 19:14:06

您可以先编写一个 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 with find 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:

假情假意假温柔 2024-10-16 19:14:06

我刚刚发布了 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

够运 2024-10-16 19:14:06

使用咖啡命令行工具的最简单方法。

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

节枝 2024-10-16 19:14:06

这对我很有帮助(-o 输出目录,-j 加入到project.js,-cw 编译并全面查看 Coffeescript 目录):

coffee -o web/js -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):

coffee -o web/js -j project.js -cw coffeescript
故人如初 2024-10-16 19:14:06

使用 cake 编译它们全部包含在一个(或多个)生成的 .js 文件中。 Cakefile 用作配置,控制咖啡脚本的编译顺序 - 对于较大的项目非常方便。

Cake 非常容易安装和设置,在编辑项目时从 vim 调用 cake 非常简单

:!cake build

,您可以刷新浏览器并查看结果。

由于我也忙于学习构建文件的最佳方法以及将咖啡脚本与主干和蛋糕结合使用,因此我创建了一个 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

:!cake build

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.

深陷 2024-10-16 19:14:06

或者,您可以使用 --bare 标志,编译为 JavaScript,然后在必要时包装 JS。但这可能会产生问题;例如,如果您有一个包含代码的文件

i = 0
foo = -> i++
...
foo()

,那么生成的 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 code

i = 0
foo = -> i++
...
foo()

then there's only one var i declaration in the resulting JavaScript, and i will be incremented. But if you moved the foo function declaration to another CoffeeScript file, then its i would live in the foo scope, and the outer i 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.

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