⬆️ ⬇️

Why not Assets Pipeline?

Introduction
The asset pipeline is technically no longer a core feature of Rails 4, it has been chosen to go into the sprockets-rails gem.
Rails Guides. The asset pipeline



This means that, starting with rails 4.2, the asset pipeline mechanism is no longer part of the rails core and may not be used in the application development process. This gem connects by default. Indeed, in simple applications (business card site, blog), this approach is fully justified and allows you not to worry about writing complex, frontend-dependent components. In the professional development of large sites, the role of the frontend increases markedly, as does the complexity of working with it. So let's put forward our own assumptions about why Rails developers no longer impose a sprockets usage scenario.



Modularity



A bit of theory. A modular approach to developing web applications using Javascript. AMD intends to design large JavaScript applications based on individual modules. The module has the following characteristics:

')

  1. Perform the task before him and no more
  2. Not to know that there are other modules, and even more about their functionality
  3. If the module generates an unexpected error, other modules must continue to do their job without errors.
  4. Modules may have dependencies on other modules to use their functionality. Dependency management is a simple and clear developer tool.
  5. Modules that are not used should not be loaded on the page.


Rails allows you to build complex nested dependency trees, but in practice this can be a nightmare. Usually, developers do not care about the separation and dependencies of modules and go in an easy way - simply add a link to the file at the end of the shared manifest (app / assets / javascripts / application.js, app / assets / javascripts / application.css), which loads all , regardless of whether resources are needed or not.



Multiple entry points



Once again, a separate subsystem should have its own entry point in the form of a plug-in. Rails allows you to do this, but it is so difficult and not elegant that few people use it. A typical dependency description in Rails looks like this:



//= require jquery //= require jquery_ujs //= require_tree ./subsystem1/* …    ,     


The last line describes the inclusion of all modules from the subsystem1 subsystem folder in the manifest and this notation is usually used in real life. Agree that it is difficult to keep such code up to date:



 //= require jquery //= require jquery_ujs //= require_tree ./subsystem1/module1 //= require_tree ./subsystem1/module2 //= require_tree ./subsystem1/module3_old //= require_tree ./subsystem1/module4 //= require_tree ./subsystem1/module5 … //= require_tree ./subsystem1/module_n 


The subsystem1 module files may not be interconnected or exist only for backward compatibility, but they will still be loaded onto the page. Typical situation. The contractor developed a portal and left the project, leaving a giant manifesto. It is already difficult to make out which module depends on which one. The project dictates its own wrong paradigm. Although, of course, there is a contractor in the world who once held in his head a scheme of interaction between modules ...



Very often, the manifest grows to such dimensions that it becomes impossible to track dependencies. Especially in the context of team development. Merging this file in a version control system is also very inconvenient for large files.



JS and CSS are also dependent on each other.



This means only that the java script module can be dependent on the css module. For example, the date picker contains styles that are loaded on the page only if the item is on the form. Unfortunately, rails does not support this functionality. It does not support bundling html templates.



Total



Because of all these features of using the assets pipeline, the simplest solution to managing dependencies is to include them in a single manifest file. Over time, it grows to a huge size, it becomes uncontrollable. As a result, all resources are loaded on the page. This is probably why the assets pipeline has moved from mandatory to recommended Rails development features.



Instead of conclusion



Dear community, this post was written for two reasons:



  1. Desire to join Habr
  2. A heated argument between a developer using the assets pipeline and a developer using requirejs in everyday life. I would like to hear your opinion on this issue in the comments


Thanks in advance!



Used Books



» Rails Guides. The asset pipeline

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



All Articles