Merge coffeescript into your node project?

妖精的绣舞 提交于 2019-12-11 13:31:16

问题


Is setting a bunch of watchers to compile to js still the best approach in development or is there something more elegant?

I'm looking for techniques or packages that handle CoffeeScript in development and make it really nice instead of just watching and compiling to another js folder. Anything out there?

Thanks for any ideas!


回答1:


node.js server code in CoffeeScript

So for writing your node.js server code, you don't need to do anything special. Just start your app with coffee server.coffee instead of node server.js and coffeescript will transpile your code to javascript on the fly without ever needing to write .js files to disk.

Browser code in CoffeeScript (Basic)

For taking .coffee files on disk and serving the transpiled .js files to the browser, you can use the coffee-script node module to do the transpiling and serve the output. Coding it manually is just a few lines, but those few lines already exist as connect compatible middleware. Use the connect-coffee-script module. There are basic examples at that link, but it boils down to app.use(connectCoffeeScript({src: "#{__dirname}/public"})) or some variation thereof. This is compatible with express version 3.x. When a request for a .js URL comes in, the middleware will locate the corresponding .coffee file and transpile it from src to dest if needed. You should have the connect static middleware configured to serve files from your dest directory further down your middleware chain and it will be the connect static middleware that actually servers the .js file to the browser.

Full-on Asset Management

For a more advanced solution including dependency management, cache busting, concatenation, minifaction, etc, etc inspired by the Ruby on Rails asset pipeline, you can use connect-assets. It's a more complex solution, but the asset management problem in general is complex and this will fully solve many of the tricky problems for you. This will handle CoffeeScript for JS, Stylus for CSS as well as other transpilers and preprocessors.



来源:https://stackoverflow.com/questions/8998742/merge-coffeescript-into-your-node-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!