CoffeeScriptでnpmモジュールを書いたりしたことなかった(し、これからも多分しない)ので、もし書くとしたらこんな感じかなーとやってみたら出来たのでメモメモ。
モジュールを作る
とりあえずpackage.json
を書く。
package.json
{
"name": "test",
"version": "0.0.0",
"private": true,
"main": "./index.js",
"scripts": {
"postinstall": "coffee -bc index.coffee"
},
"dependencies": {
"coffee-script": "1.7.1"
}
}
scripts
のpostinstall
がキモかなー。
スクリプトを書く
とりあえずindex.coffee
を書く。
index.coffee
module.exports = -> console.log 'Hello, World!'
インストールしてみる
ディレクトリが現在こんな感じ。
|-- coffee/
| |-- index.coffee
| |-- package.json
|-- js/
で、カレントディレクトリがjs/
の状態から。
Terminal
$ npm i ../coffee
npm http GET https://registry.npmjs.org/coffee-script/1.7.1
npm http 200 https://registry.npmjs.org/coffee-script/1.7.1
npm http GET https://registry.npmjs.org/coffee-script/-/coffee-script-1.7.1.tgz
npm http 200 https://registry.npmjs.org/coffee-script/-/coffee-script-1.7.1.tgz
npm http GET https://registry.npmjs.org/mkdirp
npm http 304 https://registry.npmjs.org/mkdirp
> test@0.0.0 postinstall /Users/macbookpro/Work/js/node_modules/test
> coffee -bc index.coffee
test@0.0.0 node_modules/test
└── coffee-script@1.7.1 (mkdirp@0.3.5)
$ node
> require('test')()
Hello, World!
undefined
>
動いたぽい。