Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

準備

上記の手順が済んでいて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
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away