【Rails】プリコンパイルがこける

ポイント

  • 本番環境にdeployしても、変更内容が適用されていない!となった
  • 初めはエラーに気づかず、なんで変更部分が反映しないのか小一時間、悩んでいた
  • 再度、本番環境で assets をコンパイルしたところエラーが発生していた

環境

  • Ruby 2.6.3
  • Rails 4.2.0
  • CentOS 6.4

エラー内容

rake assets:precompile RAILS_ENV=production

rake aborted!
ExecJS::RuntimeError: SyntaxError: Unexpected token: ~~~
/railspath/app/assets/config/manifest.js:2
JS_Parse_Error.Object.defineProperty.get ((execjs):3538:621)

Caused by:
V8::Error: Unexpected token: ~~~
at js_error (<eval>:3623:12181)
at croak (<eval>:3623:21897)
at token_error (<eval>:3623:22034)
at unexpected (<eval>:3623:22122)
at semicolon (<eval>:3623:22614)
at simple_statement (<eval>:3623:25718)
at <eval>:3623:23580
at <eval>:3623:22787
at <eval>:3624:3503
at parse (<eval>:3624:3743)
at parse (<eval>:3959:22)
at uglifier (<eval>:4007:13)
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

どうやら、ES6に基づいて書いた部分はコンパイルできないらしい
ローカルではうまくいっていたのは uglifier でコンパイルしていなかったからだろう

解決策

# config/environments/production.rb
config.assets.js_compressor = :uglifier
↓
config.assets.js_compressor = Uglifier.new(harmony: true)

上記修正し、再度プリコンパイルしたところ、正常に動作することを確認

まとめ

  • ログはちゃんと見よう!(当たり前)
  • ES6くらいデフォルトで対応してくれ

参考

Uglifier::Error: Unexpected token: punc ((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true) · Issue #127 · lautis/uglifier
Hi, When I run "rake assets:precompile RAILS_ENV=production" command, I have issue. rake assets:precompile RAILS_ENV=pro...
タイトルとURLをコピーしました