Compressors

Assets compression (aka minification) is a process to shrink the file size of a file in order to reduce the time that a browser needs to download it. Usually, it’s applied to javascripts and stylesheets.

In order to set one of the following engines, we need to open apps/web/application.rb and write:

  1. # apps/web/application.rb
  2. module Web
  3. class Application < Hanami::Application
  4. configure do
  5. assets do
  6. javascript_compressor :builtin
  7. stylesheet_compressor :builtin
  8. # ...
  9. end
  10. end
  11. end
  12. end

If we want to skip compression, just comment one or both the lines above.

JavaScript

We support the following engines:

  • :builtin - It isn’t efficient like the other algorithms, but it’s a good starting point because it’s written in pure Ruby and it doesn’t require external dependencies, but it’s very limited in terms of functionalities.
  • :yui - It’s based on Yahoo! YUI Compressor. It requires yui-compressor gem and Java 1.4+
  • :uglifier - It’s based on UglifyJS2. It requires uglifier gem and Node.js
  • :closure - It’s based on Google Closure Compiler. It requires closure-compiler gem and Java

In order to use :yui, :uglifier and :closure compressors, you need to add the corresponding gem to Gemfile.

Stylesheet

We support the following engines:

  • :builtin - It isn’t efficient like the other algorithms, but it’s a good starting point because it’s written in pure Ruby and it doesn’t require external dependencies, but it’s very limited in terms of functionalities.
  • :yui - It’s based on Yahoo! YUI Compressor. It requires yui-compressor gem and Java 1.4+
  • :sass - It’s based on Sass. It requires sass gem

In order to use :yui, and :sass compressors, you need to add the corresponding gem to Gemfile.

Custom Compressors

We can use our own compressor for both JS and CSS. It MUST respond to #compress(filename) and return a String with the minification output.

  1. class MyCustomJavascriptCompressor
  2. def compress(filename)
  3. # ...
  4. end
  5. end

Then we can use it with our configuration:

  1. # apps/web/application.rb
  2. module Web
  3. class Application < Hanami::Application
  4. configure do
  5. assets do
  6. javascript_compressor MyCustomJavascriptCompressor.new
  7. stylesheet_compressor :builtin
  8. # ...
  9. end
  10. end
  11. end
  12. end