Skip to content

Asset precompilation woes#53

Merged
RISCfuture merged 1 commit intoSquareSquash:masterfrom
bjeanes:precompile-all-assets-in-all-envs
Mar 4, 2013
Merged

Asset precompilation woes#53
RISCfuture merged 1 commit intoSquareSquash:masterfrom
bjeanes:precompile-all-assets-in-all-envs

Conversation

@bjeanes
Copy link
Contributor

@bjeanes bjeanes commented Feb 22, 2013

Currently config.assets.initialize_on_precompile is set to true (the default). This means, if you want to precompile assets prior to a deployment (e.g. creating a war file, deploying to Heroku, or just doing the work once before sending out to many servers), you need to have access to the production DB.

Usually, to counter this, one can set RAILS_ENV=development before running the precompilation rake task. However, in the case of Squash, "flot/excanvas.js" is added to config.assets.precompile only in config/environments/production.rb resulting in a ActionView::Template::Error (flot/excanvas.js isn't precompiled) error at runtime in production.

So, there are two ways out of this that I see. I'm happy to issue a pull request for either direction, I just wanted some input first in case there is a preferred route:

  1. Move config.assets.precompile << 'flot/excanvas.js from production.rb to application.rb so it is effective in all compilation environments (i.e. allowing precompilation with RAILS_ENV=development)
  2. Set config.assets.initialize_on_precompile = false so that a DB connection is not formed. This has other issues if any of the assets access certain classes or constants at compile time (e.g. ERB assets).

For option 2, one error that already pops up is uninitialized constant Ping due to a lot of assumed requires not happening. Things like this would need to be resolved.

@bjeanes
Copy link
Contributor Author

bjeanes commented Feb 21, 2013

Seems like option 2 would be out:

rake aborted!
uninitialized constant Squash::Configuration
  (in /Users/bjeanes/Code/squash/web/lib/assets/javascripts/configure_squash_client.js.erb)
/Users/bjeanes/Code/squash/web/lib/assets/javascripts/configure_squash_client.js.erb:15:in `evaluate_source'

@RISCfuture
Copy link
Contributor

Unless I'm mistaken, option 2 restricts the flexibility of people's deploy situations. Some people will want to precompile their assets locally, and others will want to do it on the target. And no matter what environments initialize_on_precompile is set on, we're always going to want to add excanvas to that list. So it makes sense to put that in application.rb to me.

@bjeanes
Copy link
Contributor Author

bjeanes commented Feb 22, 2013

Makes sense... that's what I did locally too, so I'm glad you agree. I'll send a PR shortly.

@bjeanes
Copy link
Contributor Author

bjeanes commented Feb 22, 2013

@RISCfuture I just added a commit that solves the issue for me locally and allows me to run rake assets:precompile:all RAILS_ENV=development, bundle it up in a war file and run. There are few other .war related things I'll be PR-ing shortly, too.

@gamafranco
Copy link
Contributor

Note, this is also breaking deploys on heroku. For heroku, we need to either compile localy or set config.assets.initialize_on_precompile = false.

Plus IMHO, for deploy, it shouldn't be mandatory to setup and run the app on development.

@bjeanes
Copy link
Contributor Author

bjeanes commented Feb 23, 2013

@gamafranco actually, for Heroku I believe you can also follow https://devcenter.heroku.com/articles/labs-user-env-compile to have it "just work"

@gamafranco
Copy link
Contributor

Hi bjeanes,

Thanks for the tip, I don't see how that might help. Heroku dones't allow database connections at asset compile time, so config.assets.initialize_on_precompile needs to be false. And that raises the problems you identified above.

Thanks.

@bjeanes
Copy link
Contributor Author

bjeanes commented Feb 24, 2013

@gamafranco it will help because that's the point of that lab feature. It will provide the database details at "compilation" time such that your rake task will know which database to connect to. Search that page for "assets:precompile"

@bjeanes
Copy link
Contributor Author

bjeanes commented Mar 4, 2013

@RISCfuture bump. Commit attached that solves problem using method 1.

RISCfuture added a commit that referenced this pull request Mar 4, 2013
@RISCfuture RISCfuture merged commit 739996e into SquareSquash:master Mar 4, 2013
@bjeanes bjeanes deleted the precompile-all-assets-in-all-envs branch March 6, 2013 21:16
mexisme pushed a commit to fluxfederation/SquareSquash-web that referenced this pull request Apr 22, 2015
mexisme pushed a commit to fluxfederation/SquareSquash-web that referenced this pull request Apr 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants