00:00:10.130
Thank you! So, I'm going to present to you how Sprockets works. But first, I have to introduce myself. My name is Rafael Mendonça França, and I'm from Brazil. You can find me on GitHub, and I'm a member of the Rails core team.
00:00:18.390
One of the things I like about the Rails community is that you can choose what you want to do within it. I often say that I'm the person who does the things that nobody wants to do. I maintain Rails, which means I usually release the changes in the blog posts, handle issues, and ensure we have great people working on Rails.
00:00:31.470
Rails is present in many different ways; it’s not just about the framework itself. There are various components like Action View, Action Cable, and Sprockets. So you can see that the Rails project is present almost everywhere in your application. We have processes running on the server-side and also client-side applications in the browser.
00:00:50.129
This talk aims to present something that is not widely known – how the Asset Pipeline of Rails works. I'm going to explain why we need the Asset Pipeline, the components responsible for it, and how it integrates into our applications. I will also take some time to demonstrate how to extend the Asset Pipeline.
00:02:03.360
So, why do we need an Asset Pipeline? Before its introduction, Rails had a different approach. We often faced the question of where to place our assets. There was no convention for handling client-side code in Rails applications. We had to put our assets in the public folder, resulting in an orgless structure, often ending up with numerous files we couldn't verify if they were being used.
00:02:32.880
Rails promotes convention over configuration, but at that time, we lacked proper organization for our client-side code. We also dealt with trade-offs between organization and performance due to limitations in browsers. For instance, should we create small, self-contained files, or make fewer asset requests? Should we focus on writing legible code, or should we minimize bytes sent to clients?
00:03:02.840
Additionally, there were emerging technologies like CoffeeScript and SASS that we weren’t using effectively in Rails applications. To resolve these challenges, the Asset Pipeline was created, and it now works with established conventions for our client-side code.
00:03:47.480
Currently, our assets live in the app/assets directory or other designated asset folders, and they are automatically compiled during development and need to be recompiled for production. We also have assets that are fingerprinted and generated with digest to help with cache busting.
00:04:30.670
Sprockets is comprised of several important components, including processors, compressors, and directives. These components work together in conjunction with environments and manifests within the pipeline. The processors are crucial; they are objects that take input, modify it, and return metadata along with the compiled asset.
00:05:04.360
For instance, there's a JavaScript processor that removes semicolons from the end of your JavaScript files to make them compatible, as they are not needed. The input has special keys, and it returns a hash containing both data and metadata. This metadata is integral when dealing with dependencies in your assets.
00:05:49.860
Sprockets comes with built-in processors, such as the CoffeeScript processor, which compiles CoffeeScript into JavaScript. Likewise, the SASS processor compiles SASS files into CSS. Another important processor is the bundler processor, which concatenates multiple files into a single file in your application.
00:06:46.600
We also have compressors that operate on the concatenated files to minimize size by removing unnecessary parts of the code. Additionally, Sprockets has directives that are special comments indicating your bundles and dependencies. For example, a typical application.js file generated by a Rails app might specify which files to require before executing.
00:07:23.510
One special directive we use is 'require_tree', which tells Sprockets to compile all files in a specified directory. This makes it easier to understand what's being included and compiled during asset generation. Another helpful directive is the 'link' directive, which allows us to reference all images in a directory for asset compilation.
00:08:10.670
The environment where the assets are loaded is critical; it's where requests for asset files are processed, and where the methods we’ve discussed take place. The manifest holds references for compiled assets, enabling the application to serve the right versions without recompiling each time.
00:09:50.440
These components work collectively to produce the right asset for our application. While there’s a lot more to Sprockets, I’d encourage you to read the documentation to uncover more details. The Asset Pipeline itself integrates Sprockets into the Rails application, connecting everything from the methods you use in your erb files to the configurations in the initializer.
00:10:37.360
As we move forward, the importance of keeping the community informed and educated about these components becomes vital. We also want to ensure that we are not only users of these powerful tools but that we actively contribute to their documentation and community knowledge.
00:11:00.000
In conclusion, Sprockets helps us efficiently manage our assets in Rails applications. It’s crucial to understand how it works to enable us to maximize the capabilities of the Asset Pipeline and ensure smooth development processes.
00:11:35.000
If you want to dig deeper and learn more about improving asset management in Rails, I encourage you to explore the Ruby on Rails community and the various resources available.