javascript 咖啡脚本编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5060991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:48:42  来源:igfitidea点击:

Coffee script compilation

javascriptcompilationcoffeescript

提问by Arnis Lapsa

I'm looking for simplest possible way to automatically recompile coffee scripts into JS.

我正在寻找最简单的方法来自动将咖啡脚本重新编译为 JS。

Reading documentationbut still having troubles to get exactly what I want.

阅读文档但仍然无法准确获得我想要的内容。

I need it to watch folder src/for any *.coffeefiles modifications and compile them into concatenated javascript file into lib/something.js.

我需要它来监视文件夹src/中的任何*.coffee文件修改并将它们编译成连接的 javascript 文件到lib/something.js.

Somehow can't combine watching, compiling and concatenating together. :/

不知何故不能将观察、编译和连接结合在一起。:/

采纳答案by arnorhs

The coffee script documentation provides an example for this:

咖啡脚本文档为此提供了一个示例:

Watch a file for changes, and recompile it every time the file is saved:

观察文件的变化,并在每次保存文件时重新编译它:

coffee --watch --compile experimental.coffee

If you have a particular script you want to execute, you could use the linux command dnotify: http://linux.die.net/man/1/dnotify

如果你想执行一个特定的脚本,你可以使用 linux 命令 dnotify:http: //linux.die.net/man/1/dnotify

dnotify --all src/ --execute=command

Edit: I had some problems with the --execute part of dnotify - might be a bug, but this is what I got working:

编辑:我在 dnotify 的 --execute 部分遇到了一些问题 - 可能是一个错误,但这就是我的工作:

dnotify --all . -e `coffee -o lib/ --join --compile *.coffee`

That executed the compile command each time a file was modified.

每次修改文件时都会执行编译命令。

If you append the command with an ampersand, like this:

如果在命令后附加一个 & 符号,如下所示:

dnotify --all . -e `coffee -o lib/ --join --compile *.coffee` &

it will be started in a separate process. To get the process ID, you can use this:

它将在一个单独的进程中启动。要获取进程 ID,您可以使用以下命令:

ps ux | awk '/dnotify/ && !/awk/ {print }'

And then, you can kill the process by using something like this:

然后,您可以使用以下方法终止该进程:

kill `ps ux | awk '/dnotify/ && !/awk/ {print }'`

But if that's your objective (to kill by process name), you can do it in a simpler way by using:

但是,如果这是您的目标(按进程名称杀死),您可以使用以下更简单的方式:

killall dnotify

回答by Steve

Try jitter

试试抖动

https://github.com/TrevorBurnham/jitter

https://github.com/TrevorBurnham/jitter

It watches a directory of .coffee files, and when it detects that a file has changed it automatically recompiles it to .js

它监视 .coffee 文件的目录,当它检测到文件已更改时,它会自动将其重新编译为 .js

jitter /path/to/coffee/dir /path/to/js/dir

抖动 /path/to/coffee/dir /path/to/js/dir

I've been trying it out with a project using coffescript and sencha touch, it seems to work pretty well. Doesn't take care of the concatenation problem, but it's really simple to use for someone who just needs auto-compilation.

我一直在尝试使用 coffescript 和 sencha touch 的项目,它似乎工作得很好。不处理连接问题,但对于只需要自动编译的人来说使用起来非常简单。

回答by Ikrom

This helped me (-ooutput directory, -jjoin to project.js, -cwcompile and watch coffeescript directory in full depth):

这对我有帮助(-o输出目录,-j加入到 project.js,-cw编译和观看完整深度的咖啡脚本目录):

coffee -o web/js -j project.js -cw coffeescript

回答by gsklee

Well coffee --watchhas 2 major flaws:

Wellcoffee --watch有2个主要缺陷:

  • New files created after command has been issued aren't being watched
  • Requires manual initiation so there can be a chance you forget to do it, which doesn't sound more brilliant than forget to compile before you git commitit
  • 命令发出后创建的新文件不被监视
  • 需要手动启动这样可以有一个机会,你忘记这样做,这不健全的更加辉煌的比之前忘记你编译git commit

The solution I came up with is a rather simple Bash script that takes coffee --watcha few steps further which will allow your working directory tree to be watched ever since system login, and automatically get compiled into JavaScript on each file save/change or new file creation:

我想出的解决方案是一个相当简单的 Bash 脚本,它需要coffee --watch进一步执行几个步骤,这将允许自系统登录以来一直观察您的工作目录树,并在每个文件保存/更改或新文件创建时自动编译为 JavaScript:

http://blog.gantrithor.com/post/11609373640/carefree-coffeescript-auto-compiler

http://blog.gantrithor.com/post/11609373640/carefree-coffeescript-auto-compiler

There may be more elegant way to do this, but this implementation works great =)

可能有更优雅的方法来做到这一点,但这个实现效果很好 =)

回答by Dino Reic

find -type f | grep .coffee | xargs ls -t | head -n 1 | xargs coffee -cw

find last modifed coffee script and put it in compile-watch mode

找到最后修改的咖啡脚本并将其置于编译监视模式

回答by mystrdat

Being one level above /src, this will work for all .coffee files found no matter the depth.

比 /src 高一级,这将适用于所有找到的 .coffee 文件,无论深度如何。

coffee -w -c src/

回答by ckpcw

I have found the command-line coffeescript compiler to be poorly suited for complex project structures.

我发现命令行 coffeescript 编译器不太适合复杂的项目结构。

If you and your build process are as needy as I am, check out Grunt- http://gruntjs.com/

如果您和您的构建过程和我一样需要,请查看Grunt- http://gruntjs.com/

It allows for highly complex build processes - for example, you might

它允许高度复杂的构建过程 - 例如,您可能

  1. concatenate coffee to new file(s)
  2. compile
  3. concatenate some additional JS
  4. minify
  1. 将咖啡连接到新文件
  2. 编译
  3. 连接一些额外的 JS
  4. 缩小

Tasks can be strung together, and watched files/folders are highly customizable as well.

任务可以串在一起,并且监视的文件/文件夹也可以高度自定义。

回答by Trevor Burnham

The short answer to your question is that the coffeeutility wasn't designed for this; combining file-watching and concatenation is actually pretty complex. Expect more sophisticated build tools for CoffeeScript in the near future; until then, you might want to do your project's builds by writing a Cakefile or Ruby Watchr script. Then you can also throw in minification, documentation-generation, and whatever else you need for your particular project (not to mention guaranteeing a particular concatenation order).

对您问题的简短回答是,该coffee实用程序并非为此而设计;结合文件监视和连接实际上非常复杂。期待在不久的将来为 CoffeeScript 提供更复杂的构建工具;在此之前,您可能希望通过编写 Cakefile 或 Ruby Watchr 脚本来构建项目。然后,您还可以加入缩小、文档生成以及您的特定项目所需的任何其他内容(更不用说保证特定的串联顺序)。

回答by AAA

find `pwd` | grep .coffee | xargs coffee -w -c

try this one in root directory of the application

在应用程序的根目录中尝试这个

回答by Arnis Lapsa

Changed mind about concatenation.
Created small compiler.sh file which contains:

改变了对串联的看法。
创建了小的 compiler.sh 文件,其中包含:

dnotify -M src/ -e coffee -o lib/ -c src/ &
dnotify -M spec/ -e coffee -o lib/ -c spec/ &

Kind a suits my needs.

一种适合我的需要。