Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using RequireJS in NodeJS with CoffeeScript without transpilation

Using RequireJS in NodeJS with CoffeeScript without transpilation

Problem

Probably pretty straightforward question, but still I haven't found any feasible Solution so far. Currently I am able to run the server purely from Coffee Files and even Mocha tests are able to work with coffee files. However RequireJS is still looking for *.js files :/ I am not feeling good about transpiling just to satisfy RequireJS.

Probably easier way would to be use NodeJS extensions, but since it's deprecated, it's not a good way. I am thinking about some solution like this:

requirejs.config({
    nodeRequire: require,
    compilers: [
        {
            extensions: ['.coffee','.litcoffee','.coffee.md']
            compiler: require('coffee-script').compile
        }
    ]
})

It would simply look for the file with these extensions and when found, compile it. Otherwise keep default behavior. Sure, it means some performance issues when looking for these files, but since it's meant for development only, I don't see a big problem.

Unfortunately it's hard for me to understand how RequireJS works under the hood. Otherwise I would have tried to work out some solution like this.

Is there some other solution that I am missing ?

SOLVED

After all of this I have decided for quite opposite approach. Use the require for the browser side too, so I don't need to change anything for server code.

Problem courtesy of: FredyC

Solution

Related (my question): how can I get connect-assets to recompile my coffee files when they change?

This is possible with connect-assets. This is a layer that sits between the request and the assets and compiles them on-demand. I tried going that route and found it to be a lot of work -- frequently ran into situations where connect wouldn't compile the coffee into js correctly. Or something, I was probably doing it wrong since I was new to node at the time. Also I wasn't really comfortable with relying on an asset layer like that in production.

In the end I settled on serving up js files and building them when they changed using grunt, grunt-contrib-watch and grunt-contrib-coffee. The advantage there is that if you run into an error on a particular line of js, you can just open up the file and look at the line that failed. If you're new to coffeescript you will occasionaly do it wrong and end up with some wacky javascript. It's helpful to be able to just pull up the js file and see it.

There are a lot of similar tasks that need to be automated in building a web app, so grunt is a useful tool in many situations. That would be my recommendation.

Here's a sample of my grunt setup:

  # Project configuration.
  grunt.initConfig
    watch:
      coffee:
        files: ['app/assets/src/coffee/**/*.coffee', 'app/assets/src/coffee/*.coffee', 'app/webserver.coffee']
        tasks: ['coffee:dev', 'replace', 'test']

With this setup (not all of which is here) I can transpile all my coffee files, run all my tests, perform a bit of text manipulation on some files and have a clean and ready to go web app inside of 2 seconds.

Require takes a bit to get your head around but in summary it just looks locally (client side) to see if it has a library and if it doesn't it gets it from the server. It won't do any transpiling and that seems a bit out of its 'mission parameters' if you will. Require is like the parts guy at an auto parts shop: it checks to see if a tool is available at the front counter; yes, it hands it to you; no, it goes and gets it.

If you just want to use coffee in node without transpiling it, try require-cs.

Solution courtesy of: jcollum

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Using RequireJS in NodeJS with CoffeeScript without transpilation

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×