1

I wrote a simple module in CoffeeScript, but I want to publish the compiled JavaScript to NPM. I don't want to manually run the coffee command each time, that's too much typing and I'll probably forget and publish stale js every now and then.

I know there's some combination of npm package.json script hooks and CoffeeScript cli arguments that will do the trick, but I forget the particulars. How's it go again?

hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76

2 Answers2

5

A basic package.json setup for the conventional directory structure looks like

"scripts": {
  "prepublish": "coffee --compile --output lib/ src/"
}

If you also want to compile coffeescript before running tests, you likely want to pull the compilation step out as a reusable script:

"scripts": {
  "pretest": "npm run compile",
  "prepublish": "npm run compile",
  "test": "mocha",
  "compile": "coffee --compile --output lib/ src/"
}
Community
  • 1
  • 1
hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
  • It's usually a good idea to setup `Cakefile` or `Makefile` to handle building and testing processes. – Leonid Beschastny Oct 04 '14 at 22:22
  • @LeonidBeschastny worth noting for sure. [Some folks](http://substack.net/task_automation_with_npm_run) feel strongly that such a build tool is often unnecessary. Node modules are often (ideally?) so small that build tooling beyond scripts is excessive. – hurrymaplelad Oct 05 '14 at 00:02
  • @VasiliyBorovyak works for me with coffee-script 1.9.2. Would you tell me a bit more about the problems you're having? – hurrymaplelad May 05 '15 at 01:21
  • Works with latest coffee. Sorry for that. Can't upvote. Freaking SO says I can't revote unless the answer is changed . – Vasyl Boroviak May 05 '15 at 02:19
1

The prepublish script is considered deprecated later versions of npm@4.0.0. You should use prepare instead. There's another script that was introduced alongside the former prepublishOnly. They are similar but different, you can read more in the linked post.

"scripts": {
    "build": "coffee --compile --output lib/ src/",
    "prepare": "npm run build && npm test",
    "test": "jest"
}
codejockie
  • 9,020
  • 4
  • 40
  • 46