Moving forward with the Rails asset pipeline
From Yeti dev Guillermo Iguaran:
One of the big changes with Rails 3.1 was the inclusion of the asset pipeline. With all the productivity benefits it brought, there’ve been some headaches. Here are some notes on how it’s improving and changing in Rails 4.0.
Extraction to plugin
In Rails 4.0, the asset pipeline has been extracted to a separate plugin, named sprockets-rails. This extraction has helped to the plugin to grow independently from Rails Core, and follow the Rails moves towards modularity. I’ve been leading the development of the new plugin since the initial extraction (which I also performed).
Since the sprockets-rails gem was extracted, we were able to start to support the latest versions of sprockets and we are working on keeping sprockets-rails updated with the latest features available in sprockets.
We’ve agreed to sync sprockets and sprockets-rails version numbering, and now sprockets-rails 2.x will be compatible with sprockets 2.x. The next major version of sprockets will be released the next year and will bring exciting features like source maps support. Sprockets-rails 3.0 will be compatible with sprockets 3.0 and hopefully will be released before the release of Rails 4.1.
The precompiling has been improved dramatically by Josh Peek. Now sprockets has built-in support for precompiling and generation of a manifest log (that now is saved as json instead of yaml). In sprockets-rails we are dropping the StacticCompiler class used for precompiling in favor of the methods provided by Manifest class of sprockets for pre-compilation.
Additionally we are removing support for “double-compiling” (digested and undigested assets simultaneously); now the value set
config.assets.digest in production environment is respected during the precompiling process. That change will improve the performance of precompiling dramatically, and we will encourage people to keep their undigested assets in the public folder.
We are doing some tweaks to some of the configuration variables used for asset pipeline:
config.assets.enabledwas deprecated. Assets pipeline is enabled by default and to disable it you should disable the loading of the sprockets-rails framework like any other framework in Rails. For reference check this gist.
config.assets.manifesthas been removed; this option was rarely used and often misused. By default the assets manifest is saved in the same place as assets, in future versions of sprockets it’ll probably will be moved to another folder.
config.assets.cachehas changed its default value: now the default cache store (placed in tmp/cache/assets) isn’t shared between different environments. In Rails 3.x if you test in the browser, then run integration tests, then test in the browser browser, it blows away the cache at every change. In Rails 4.0 this won’t a problem anymore, since the cache used during integration tests running will be different than the cache used while you test on browsers.
config.assets.loggerhas been removed. Now sprockets is logging more silently, using debug level by default. If you still want to use a different logger set it with
Asset Tags Helpers
Josh Peek has refactored the code of all the assets tags helpers. Now the helpers are independent from ActionView helpers. This will make to the code easier to maintain and now it’s easier for users to track and debug errors related to assets tags helpers.
The performance for assets compiling has been improved drastically. Precompiling in most of the projects is requiring a couple of seconds instead of several seconds or minutes. For example, in a single application with multiple js and css assets:
% time rake assets:precompile
8.06s user 0.85s system 95% cpu 9.339 total
% time rake assets:precompile
1.42s user 0.32s system 99% cpu 1.752 total
Nathan Broadbent is doing further work to optimize the precompiling even more, in how it detects changes in assets. There is an open pull request to merge his changes in sprockets and sprockets-rails. You can test his work right now with the turbo-sprockets-rails3 gem.
More updates on the good stuff on the way in Rails 4.0 coming soon.