简体   繁体   中英

coffeescript + nodejs: caching the compiled server-side javascript using require

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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM