久しぶりにherokuにrailsをpushしようとしたら色々変わっていたので、メモ。
Heroku CLIはインストールされている前提。
環境
- macOS Mojave 10.14.4
- Rails 5.2.2
- ruby 2.6.1
- ES6
- stimulusjs
- heroku/7.22.10 darwin-x64 node-v11.10.1
- PostgreSQL
Heroku CLIのアップデート
最新じゃないといろいろ面倒なので、とりあえずアップデートしときます。
1
2
3
|
$ brew update
$ brew upgrade heroku
$ heroku --version
|
herokuログインとcreate
前はログインするときはidとパスワードを入力してましたけど、今はブラウザ側でログインする方法でもログインできた。
1
2
3
|
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
$ heroku create
|
アセットプリコンパイル
config/environments/production.rb
を編集
1
2
3
4
|
# config/environments/production.rb
# Do not fallback to assets pipeline if a precompiled asset is missed.
省略
config.assets.compile = true <=falseをtrueにする
|
ES6の部分はコンパイルできないのでUglifier.new(harmony: true)
に変更 する。変更せずにコンパイルしようとするとUglifier::Error: Unexpected token: punc ()). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true)
って怒られる。
1
2
3
4
|
# config/environments/production.rb
省略
## Compress JavaScripts and CSS.
config.assets.js_compressor = Uglifier.new(harmony: true)
|
ここまで終わったらコンパイルします。アセットプリコンパイルは、assetsを作業したら必ずherokuに送る前にやる。
1
2
|
$ rails assets:precompile RAILS_ENV=production
|
herokuに送る
1
2
3
|
$ git add -A
$ git commit -m "first heroku push"
$ git push heroku master
|
herokuは自動時にデータベースを作成してくれるので、rails db:create
は不要。
1
|
$ heroku run rails db:migrate RAILS_ENV=production
|
ここまでできたらherokuにアクセス
See Also