LoginSignup
15
15

More than 5 years have passed since last update.

HubotをES6で書いてHeroku上で動かした時のメモ

Last updated at Posted at 2016-04-14

準備

上記の手順が済んでいてSlack上にhubotがいる状態を想定しています。

babelのインストール

npmでインストールします。

$ npm install --save babel babel-cli babel-preset-es2015

成功すれば、package.jsonが変更されます。

   "dependencies": {
+    "babel": "^6.5.2",
+    "babel-cli": "^6.7.5",
+    "babel-preset-es2015": "^6.6.0",
     "hubot": "^2.18.0",
     "hubot-diagnostics": "0.0.1",
     "hubot-google-images": "^0.2.6",

ES6でスクリプトを書く

  • 例 (src/sample.js)
module.exports = ( robot => {
  robot.hear(/ぬるぽ/, msg => {
    msg.send("ガッ")
  })
})
  • 以下のコマンドでコンパイルします。
$ node_modules/.bin/babel src --presets es2015 --out-dir compiled
src/sample.js -> compiled/sample.js
  • コンパイル後のディレクトリを指定してHubotを起動します。
$ bin/hubot --require compiled
hubot> ぬるぽ
hubot> ガッ

Heroku用設定

Herokuにデプロイした際に、babelによりES6がコンパイルされるように設定します。

package.jsonの修正

package.jsonを修正し、Heroku上で依存パッケージのインストールが完了した後に、babelのコンパイルが実行されるようにします。

   },
   "engines": {
     "node": "0.10.x"
+  },
+  "scripts": {
+    "postinstall": "node_modules/.bin/babel src --presets es2015 --out-dir compiled"
   }
 }

Procfileの修正

Procfileを修正し、Heroku上でコンパイル済みのjsを読み込むように修正します。

 web: bin/hubot -a slack -n myhubot --require compiled

Herokuにデプロイ

$ git add .
$ git commit
$ git push heroku master
15
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15