bundle exec rake assets:precompile fails with `unexpected token`

若如初见. 提交于 2019-11-28 11:10:10

I had this exact same problem and solved it, here are the details:

When you include one or more javascript files in your Rails 3.1 environment using application.js and //= require ..., Rails will wrap the contents of your file with a <script> ..filecontents.. </script>. You can verify this by running your site in development mode and doing a View | Source.

The problem is, in development mode, if you are not actually using that script, it may appear to work fine. But, Essentially, what you have is <script><script> ..filecontents.. </script></script>.

However, when you attempt to PRECOMPILE the assets, something in that compiling process (sorry - I do not know what exactly) gags on the "<" in the inner script token.

Double-check your included .js files and, if any of them are wrapped by <script> ... </script> remove those surrounding tags.

You should see that, in Development, everything still looks fine if you View Page Source. And, when you precompile your assets, the error should go away.

I ran into this problem with Google's recommended google-analytics javascript snippet and removing the script wrapper from the included file solved the problem.

Here's how I debugged this.

Locally, run this

RAILS_ENV=production bundle exec rake assets:precompile

Try to find out where it chokes (the script right before the choking is the file you'll want to look at). Look at all the requires.

In my case, I had a file that ended in .js when it should've been .jsx and that's what fixed it.

I had the very same problem.

Even though I did not have a <script> tag in my javascript files, Dave's suggestion helped me a lot!

I was able to spot a comment on a external lib I was loading in my application.js:

<!-- Hotjar Tracking Code for (...) ->

That single comment was breaking my uglifier compression and it took me a long time to find it and even possibly think it was the culprit.

So, also watch out for comments in the begining of JS files, as the might break your compression.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!