What sprockets mean in rails

徘徊边缘 提交于 2019-12-03 10:45:16

问题


So, I just realised that I have absolutely no Idea what Sprockets are in rails.

when using either bootstrap, or materialize, it is requested to include the sprocket (bootstrap-sprocket or materialize-sprocket) in the application.js file.

Everything I search talks about the asset pipeline and confuses me the more.

According to ruby-guides,

The asset pipeline is technically no longer a core feature of Rails 4, it has been extracted out of the framework into the sprockets-rails gem.

and when I did bundle show sprockets-rails, I got:

/Users/Sunday/workspace/resilience/vendor/bundle/ruby/2.1.0/gems/sprockets-rails-2.3.1

which shows that I have the gem, at least.

But my confusion and my question is that what is the importance of sprockets, as opposed to sprockets-rails that made other gems like bootstrap and materialize and probably some others to have sprockets, especially in their javascript files?

Thanks.


回答1:


Sprockets is a Ruby library for compiling and serving web assets. Sprockets allows to organize an application’s JavaScript files into smaller more manageable chunks that can be distributed over a number of directories and files. It provides structure and practices on how to include assets in our projects.

Using directives at the start of each JavaScript file, Sprockets can determine which files a JavaScript file depends on. When it comes to deploying your application, Sprockets then uses these directives to turn your multiple JavaScript files into a single file for better performance.




回答2:


/app/assets/javascripts/application.js
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

application.js file is known as a manifest and it’s managed internally by Sprockets. When a request comes in for this file Sprockets looks at the manifest and compiles together every file that is mentioned in it and includes their contents before any code in this file. Sprockets will search the loadpath for this file and, in this case, load it from the jquery-rails engine’s vendor/asset/javascripts directory.



来源:https://stackoverflow.com/questions/31828795/what-sprockets-mean-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!