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?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- google-drive can't get push notifications
- Failed at the electron@1.8.2 postinstall script
- How to reimport module with ES6 import
- Webpack getting started, import error
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
- Get file created date in node
I have written npm packages in CoffeeScript from scratch. I encourage you to use CoffeScript for node as well as for the Browser. However, before you can use or publish your module, you have to compile the source CoffeeScript to JavaScript. That should not hold you back from using CoffeeScript, though.
Tip: While developing, use
coffee -cw yourfile.coffee
(command line) to watch the file for changes and compile on save.I'm going to suggest that you write your package in coffeescript, but only publish it in javascript. I do it like this:
src
lib
src
is committed to my git repo,lib
is in my.gitignore
lib
is published to npm,src
is in my.npmignore
coffee-script
package is in mydevDependencies
You can take a look at a simple package of mine, refix, for inspiration:
npm install refix
If a lot of your modules have
coffee-script
in theirdevDependencies
, it's useful to just globally installcoffee-script
instead of install it for each module (which takes much longer).coffee-build is a global version manager for
coffee-script
.Just add these 2 scripts to your
package.json
:Notice how
-v 1.11.x
is not an exact version, which allows implicit upgrades.The only downfall is that users must
npm install -g coffee-build
before they can install your module.You can write NPM modules in coffeescript, but in order for them to be usable by JS users they must be compiled to JS before you publish on NPM.
package.json
makes this easy with theirprepublish
script hook which runs the specified script before you publish. Heres an example of aprepublish
NPM hook in zombie.jshttps://github.com/assaf/zombie/blob/master/package.json#L16
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
bin/howl.js
Running
node howl.js
(or simplyhowl
when it's installed globally) will now outputAwwooooo!
. You can do things likerequire
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.