📜 ⬆️ ⬇️

Compressing JavaScript and CSS on Rails with asset_packager

It often happens that making a big project eventually leaves a bunch of separate javascript files like libraries, plug-ins for them and others. Also, periodically you can meet several CSS, which are mainly made for convenience of perception. Also, in an amicable way, the whole thing is supplied with a number of comments, so that at least somehow sort it out.
Due to the fact that the Rails development and production versions are separated, using the asset_packager plugin you can adjust the automatic compression process of all js and all css files into 2 files with all comments cut out.
Those. it turns out that in the development version you will have normally supported scripts and styles, and in production everything will be integrated to speed up the download (ps browsers generally do not support parallel loading of js and css files).


How to use


  1. Install the plugin:
    script / plugin install sbecker.net/shared/plugins/asset_packager
    or
    script / plugin install git: //github.com/sbecker/asset_packager.git
  2. Run the “rake asset: packager: create_yml” to generate the config / config / asset_packages.yml for the first time. It describes which files and in which order to merge (syntax - the simplest yaml config)
  3. Run rake asset: packager: build_all to generate compressed files. Every time you rewrite the config, you need to run the task again.


How does it work


For example, put a call to Java in your layout:
<% = javascript_include_merged 'prototype', 'effects', 'controls', 'dragdrop', 'application', 'foo', 'bar'%>
In the development version you will receive:







And in production:
')

Similarly with styles:
Calling <% = stylesheet_link_merged 'screen', 'header'%> will do in development:
/>
/>

And in production:
/>

Good luck to use!

Source: https://habr.com/ru/post/28884/


All Articles