Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard way of developing a node module using Coffeescript?

Is there a recommended way of developing a node module if I want to write it in Coffeescript, but don't want to force the module's users to require the coffee-script module?

like image 618
Marcel M. Avatar asked Jan 18 '12 16:01

Marcel M.


People also ask

Which of the following are examples of node modules?

Nodejs Third Party Modules: Examples of third party modules are express, mongoose, etc. To install third party modules refer to the previous blog where we have discussed how to install modules using npm.


3 Answers

Put your CoffeeScript codes in the src folder and the compiled JavaScript codes in lib folder.

Then in your package.json file, declare main to be the js file in the lib folder. Then the users of your package will require the js file instead of the coffee file.

You may take @TrevorBurnham's repository as an example.

like image 178
qiao Avatar answered Nov 07 '22 15:11

qiao


I ended with only a src folder on my git repository; a .gitignore file with an line for lib; and an empty .npmignore file. The empty .npmignore file is needed because if it's not on your module, your .gitignore is used instead.

I just added a Cakefile with a task to build my src directory using coffee --compile --output lib/ src/ and a pretest and prepublish task to package.json to build before testing and publishing.

"scripts": { "pretest": "cake build", "prepublish": "cake build", }

This solution keeps my git repository clean (without compiled code), but adds my javascript code to lib when publishing to npm.

like image 34
Marcel M. Avatar answered Nov 07 '22 13:11

Marcel M.


I'm just getting started with CoffeeScript, but I'd suggest the following:

  1. Store your CoffeeScript code in src/*.coffee,
  2. Write a main.js in the project's root that NPM will catch, and have it simply do something like require('coffee-script'); require('./src/my_lib.coffee').

There. You never, ever compile your code; it's all handled transparently. You don't check compiled code into git, nor do you publish superfluous compiled JavaScript alongside the uncompiled CoffeeScript to NPM.

Edit: In more recent versions coffee-scirpt, you should require('coffee-script/register');

like image 1
ELLIOTTCABLE Avatar answered Nov 07 '22 15:11

ELLIOTTCABLE