Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assets:precompile fails if using js.erb and <%= %> #6892

Closed
dongennl opened this issue Jun 28, 2012 · 8 comments
Closed

assets:precompile fails if using js.erb and <%= %> #6892

dongennl opened this issue Jun 28, 2012 · 8 comments

Comments

@dongennl
Copy link

Hi
after upgrading to 3.2.6 my assets:precompile fail if I use erb with a js file:

function roundNumber(num, dec) {
        var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
        return result;
    }
$('#invited_user').val(roundNumber(this.value * <%= ENV["FEE_CALC"] %>, 2));

It works fine as soon as I replace the <%= ... %> with a constant. Have also tried with single quotes. It works fine in development environment.

The error is:

rake aborted!
Unexpected token: punc (,) (line: 9905, col: 70, pos: 271107)

Error
    at new JS_Parse_Error (<eval>:3096:22)
    at js_error (<eval>:3104:15)
    at croak (<eval>:3557:17)
    at token_error (<eval>:3564:17)
    at unexpected (<eval>:3570:17)
    at <eval>:3931:25
    at maybe_unary (<eval>:4028:27)
    at expr_op (<eval>:4048:45)
    at expr_ops (<eval>:4055:24)
    at maybe_conditional (<eval>:4059:28)
@steveklabnik
Copy link
Member

Confirmed, and I made a simple test app: https://github.com/steveklabnik/rails_bug_6892

@brettgoulder
Copy link

Heyo, just wanted to chime in. This isn't an issue with js.erb. Asset precompiling requires your JavaScript to not have syntax errors which is totally. In this case, you are most likely running rake assets:precompile without setting that FEE_CALC environment variable causing a syntax error in the example you provided.

Try running FEE_CALC=2 rake assets:precompile and see what you get.

@steveklabnik
Copy link
Member

Ahh, nice catch! Yep, it 100% works if the env is correct.

@dongennl if you can demonstrate that this fails somehow with the variable set, please re-open, but I'm closing.

@dongennl
Copy link
Author

dongennl commented Jul 1, 2012

hi guys, thanks for chiming in, but I'm not sure I understand completely. Isn't the point of an .erb extension that the file gets processed and the <% .. %> bits get replaced before being handed to any compressors?
I thought the reason the rake:precompile task takes so long is that it starts up an instance of the application, so that it does have access to any data it might need. In my case ENV["FEE_CALC"] is defined in an initialiser so if any part of the app is 'awake' it should know what it is.
Maybe I'm misunderstanding. It made sense to me but haven't used assets much.
Anyway, all I want to do is have a single place in my app to define a constant, I need to use this in my javascript, is there a better way of accessing a constant in the app from js ??
thanks

@steveklabnik
Copy link
Member

Isn't the point of an .erb extension that the file gets processed and the <% .. %> bits get replaced before being handed to any compressors?

Yes. So what's happening here is that ENV['FEE_CALC"] is nil. After the processor happens, you have this line of JS:

$('#invited_user').val(roundNumber(this.value * , 2));

This is obviously wrong, and hence, 'unexpected puntuation ,'.

In my case ENV["FEE_CALC"] is defined in an initialiser so if any part of the app is 'awake' it should know what it is.

The task does load the environment, so yeah, initializers should run.

I'm going to re-open this for the moment, but I'm sure there's something simple going on here.

@steveklabnik steveklabnik reopened this Jul 1, 2012
@steveklabnik
Copy link
Member

Okay, so, I added the definition to an initializer, and it Worked For Me:

$ cat > config/initializers/environment.rb
ENV["FEE_CALC"] = "10"
^D

steve at thoth in ~/tmp/rails_bug_6892 on master!
$ rake assets:precompile
/Users/steve/.rvm/rubies/ruby-1.8.7-p358/bin/ruby /Users/steve/.rvm/gems/ruby-1.8.7-p358@global/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets

steve at thoth in ~/tmp/rails_bug_6892 on master!

How are you defining the env var?

@dongennl
Copy link
Author

dongennl commented Jul 1, 2012

Very strange! I have in config/initializers/constants.rb
ENV['FEE_CALC'] = '0.9'

should work right?

@steveklabnik
Copy link
Member

Yeah, I can't imagine that it wouldn't. Hm.

Differences between your line and mine: quotes in key (shouldn't matter), float instead of decimal (shouldn't matter).

Since I can't reproduce this in a basic app, I'm gonna give it a close. If you can give me a sample app that demonstrates the issue, we'll open it back up, and I'll pay attention to this thread and try to give you a hand, but I don't think there's much more I can do. Something else is screwy with your environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants