3

I'm running a node web server via "coffee my_server.coffee", and I load dependencies via

 require './my_library.coffee'

My codebase is pretty big, and it's starting to take a significant amount of time for my server to start up, which I believe is due to coffeescript compilation... when I convert the entire thing to javascript, it loads much faster.

What's the least painful way of caching the compiled javascript, so that when I restart my server it only compiles files that I've edited since I last started it? Ideally it would be totally transparent... I just keep on requiring the coffeescript and it gets cached behind the scenes.

Alternatively, I could run "node my_server.js", and have a watcher on my directory that recompiles coffeescript whenever I edit it, but I don't really like this idea because it clutters up my directory with a bunch of js files, makes my gitignore more complicated, and means I have to manage the watcher function. Is there a way for me to have my cake (running the "coffee" executable and requiring coffee files) and eat it too (fast load times)?

1
  • Just a note you can do require './my_library' without the file extension and it will look for .js, .coffee, and .json files. I recommend omitting the file extension. It will also help with module caching at runtime as the path passed to require is the key used to cache modules. Commented Jul 23, 2013 at 19:44

1 Answer 1

2

Well if you don't want to "clutter up your directory with a bunch of .js files", I think you're SOL. If you don't store the .js files on disk ever, you need to compile .coffee to javascript on the fly every time. The coffee command to my knowledge does not do any comparison of mtime between the .js and .coffee files, although in theory it could, in which case leaving the .js files around could help your situation. Given your preferences, the only thing that I can suggest is:

  • run a watcher that builds all your .coffee files into a separate build subdirectory tree
  • start your app with node build/app.js instead of coffee
  • ignore the build directory in your .gitignore

You'll have to give up on running things via coffee though. Curious to see if others have better suggestions. My projects don't suffer from the startup time issue. SSDs and small projects help to keep that short and not annoying.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree with pretty much all of this. My experiences with coffeescript have largely been paired with grunt, which with a coffeescript plugin can allow you to pretty easily automate compilation (and even concatenation) to javascript. I also find having the compiled code to be very helpful for testing. You can ignore these compiled files in version control to keep repo clutter down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.