I have used CoffeeScript for a while. Now I need to write a npm package, can I write it in CoffeeScript, or I should compile CoffeeScript into JavaScript?
While I'm not sure if it's the best approach, technically it is possible to write your package mostly in CoffeeScript.
Basically, you can write a JS file that simply wraps the coffee
command, like so:
bin/howl.coffee
console.log 'Awwwooooo!'
bin/howl.js
#!/usr/bin/env node
var path = require('path');
var exec = require('child_process').exec;
var coffee = path.resolve(__dirname, '../node_modules/coffee-script/bin/coffee');
var howl = path.resolve(__dirname, './howl.coffee');
var command = coffee + ' ' + howl;
exec(command, function(error, stdout) {
if (error) { throw error };
console.log(stdout);
});
Running node howl.js
(or simply howl
when it's installed globally) will now output Awwooooo!
. You can do things like require
other CoffeeScript files and access arguments by passing them from the JavaScript "wrapper" to the CoffeeScript.
Anyway, there may be reasons not to do this, but it has worked for me so far so figured I'd submit this for an additional perspective.
For a simple example project using this technique, check out https://www.github.com/joshuabc/packdown.