ローカル環境でjavascriptを実装して、herokuの本番環境にデプロイした際に、エラーが出て対応したときの記録を残します。
環境
macOS Catalina 10.15.5
Ruby: 2.5.1
Rails: 5.2.4.1
現象
$ git push memo-space master
Enumerating objects: 139, done.
Counting objects: 100% (139/139), done.
Delta compression using up to 16 threads
Compressing objects: 100% (107/107), done.
Writing objects: 100% (113/113), 15.19 KiB | 3.80 MiB/s, done.
Total 113 (delta 71), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
...
remote: Tasks: TOP => assets:precompile
remote: (See full trace by running task with --trace)
remote:
remote: !
remote: ! Precompiling assets failed.
remote: !
remote: ! Push rejected, failed to compile Ruby app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to memo-space.
remote:
To https://git.heroku.com/memo-space.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/memo-space.git'
heroku Build Log
Bundled gems are installed into `./vendor/bundle`
Bundle completed (3.47s)
Cleaning up the bundler cache.
-----> Installing node-v10.15.3-linux-x64
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
Uglifier::Error: Unexpected token name «of», expected punc «;». To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).
--
51469 var month = d.getMonth() + 1;
51470 var day = d.getDate();
51471 var hour = ( d.getHours() < 10 ) ? '0' + d.getHours() : d.getHours();
51472 var min = ( d.getMinutes() < 10 ) ? '0' + d.getMinutes() : d.getMinutes();
51473 var moment_end = year+"-"+month+"-"+day+" "+hour+":"+min;
51474 var end_time = moment(moment_end).add(-9, 'hour').format("YYYY-MM-DD HH:mm");
51475 var cookies = document.cookie;
51476 var cookiesArray = cookies.split(';');
=> for (var c of cookiesArray){
51478 var cArray = c.split('=');
51479 if (cArray[0] == " user_id"){
51480 var user_id = cArray[1]
51481 }
51482 }
51483 var data = {
51484 event: {
51485 title: title,
==
Running: rake assets:precompile からの
rake aborted! からの
Uglifier::Error: Unexpected token name «of», expected punc «;». To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true). と出ています。
メッセージを調べると、デプロイしたjavascriptの仕様(ES6)にrailsが対応してコンパイルするためには、harmony modeを有効にしてね。ということらしいです。
参考情報を参考に以下のように対応しました。
変更前
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
変更後
# Compress JavaScripts and CSS.
# config.assets.js_compressor = :uglifier
config.assets.js_compressor = Uglifier.new(harmony: true)
再度デプロイ
$ git push memo-space master
Enumerating objects: 147, done.
Counting objects: 100% (147/147), done.
Delta compression using up to 16 threads
Compressing objects: 100% (113/113), done.
Writing objects: 100% (119/119), 16.29 KiB | 3.26 MiB/s, done.
Total 119 (delta 74), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
...
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: I, [2020-06-27T03:30:44.728297 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/application-00e4778fee8872d97056857c240d7fb1daece9a066e9bf5801bd6075927cff8e.js
remote: I, [2020-06-27T03:30:44.728919 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/application-00e4778fee8872d97056857c240d7fb1daece9a066e9bf5801bd6075927cff8e.js.gz
remote: I, [2020-06-27T03:30:51.980973 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/application-f0b2b81e6c5ce3009be95d8ca8a958201d38ff5d8f2458e2102f7a093d200f57.css
remote: I, [2020-06-27T03:30:51.981882 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/application-f0b2b81e6c5ce3009be95d8ca8a958201d38ff5d8f2458e2102f7a093d200f57.css.gz
remote: I, [2020-06-27T03:30:51.991623 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/jquery-ui/ui-icons_444444_256x240-31d988765b4e6f56553c29588c500381dc3e6f0aa2980c8212202e5644aefd5d.png
remote: I, [2020-06-27T03:30:51.992765 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/jquery-ui/ui-icons_555555_256x240-32175261daee76c82bb0edf0eea16a56421866fbc31e94f3c1d570aa114502f5.png
remote: I, [2020-06-27T03:30:51.997797 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/jquery-ui/ui-icons_ffffff_256x240-350df1b7131037de20e83c5c0f3a41a770d2ac48b5762ea772b3f4a8a7b9d47a.png
remote: I, [2020-06-27T03:30:51.998674 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/jquery-ui/ui-icons_777620_256x240-0b020fc6e696d88d296e7bb1f61f1eb2ad827848e2c7382a4c3e0999e702dd9b.png
remote: I, [2020-06-27T03:30:51.999735 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/jquery-ui/ui-icons_cc0000_256x240-40985a64b4d5dd213fba27fcd862a1bd1b337a97674f6ff0b9ec20abcee4bc69.png
remote: I, [2020-06-27T03:30:52.001638 #364] INFO -- : Writing /tmp/build_972372ee6575db82336ea12d69910b2d/public/assets/jquery-ui/ui-icons_777777_256x240-faf32007ae120c302213557626e660dd10e711c5dd4f1113d35f26dc05b78d2f.png
remote: Asset precompilation completed (22.03s)
...
remote: Verifying deploy... done.
To https://git.heroku.com/memo-space.git
87fb9be..e4d0ecb master -> master
$ heroku run rails db:migrate
完了〜
参考
https://qiita.com/terufumi1122/items/27bf288414569e13e050
https://github.com/lautis/uglifier/issues/127