Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Rails compile CoffeeScript?

When does Rails compile Coffeescript resources to JavaScript? Does it happen on-demand or at startup?

like image 974
alexs333 Avatar asked Nov 17 '11 06:11

alexs333


People also ask

How do I use CoffeeScript in HTML?

If you are looking to implement coffee script in html, take a look at this. You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file.


1 Answers

CoffeeScript compiles to JavaScript on demand when a request is made to a view that includes a CoffeeScript resource.

However, you can use the following to have CoffeeScript compile to JavaScript ahead of time:

rake assets:precompile

See this link for more information on the Rails asset pipeline.

From the link:

The file extensions used on an asset determine what preprocessing is applied. When a controller or a scaffold is generated with the default Rails gemset, a CoffeeScript file and a SCSS file are generated in place of a regular JavaScript and CSS file. The example used before was a controller called “projects”, which generated an app/assets/javascripts/projects.js.coffee and an app/assets/stylesheets/projects.css.scss file.

When these files are requested, they are processed by the processors provided by the coffee-script and sass-rails gems and then sent back to the browser as JavaScript and CSS respectively.

...

Assets are compiled and cached on the first request after the server is started. Sprockets sets a must-revalidate Cache-Control HTTP header to reduce request overhead on subsequent requests — on these the browser gets a 304 (Not Modified) response.

If any of the files in the manifest have changed between requests, the server responds with a new compiled file.

...

Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk.

Compiled assets are written to the location specified in config.assets.prefix. The default setting will use the public/assets directory.

You must use this task either during deployment or locally if you do not have write access to your production filesystem.

The rake task is:

bundle exec rake assets:precompile

UPDATE: For those interested in precompiling assets for development, set the RAILS_ENV variable to development first (from here):

RAILS_ENV=development bundle exec rake assets:precompile
like image 140
Matt Hulse Avatar answered Sep 24 '22 20:09

Matt Hulse