简体   繁体   中英

Ruby Rails precompiling assets vs. using the asset pipeline

I'm wondering when to use Rails.application.config.assets.precompile to include Javascript plugins, and when to use the asset pipeline. From what I understand, the asset pipeline minifies all files listed in it, and it makes this minified file available to the entire application. However, you can use Rails.application.config.assets.precompile in config/initializers/assets.rb to allow a plugin to be used only on specific pages. Is this understanding correct?

For instance, if I have the lines below in my config/initializers/assets.rb file:

Rails.application.config.assets.precompile += %w( plugins/footable/angular-footable.js )
Rails.application.config.assets.precompile += %w( plugins/footable/footable.all.min.js )
Rails.application.config.assets.precompile += %w( plugins/footable/footable.core.css )

Then I can use the Foo Table plugin in my app/views/users/index.html.erb file:

<%= javascript_include_tag 'plugins/footable/angular-footable' %>
<%= javascript_include_tag 'plugins/footable/footable.all.min' %>
<%= stylesheet_link_tag 'plugins/footable/footable.core' %>

Is this a proper usage?

The reason I ask is because I want to use the Foo Table plugin (among others) only on the app/views/users/index.html.erb page. I thought about including the plugin in the asset pipeline, but I didn't want to bog down the rest of my application with Javascript code that was only needed by one page.

I found an answer to my own question on this website: https://launchschool.com/blog/rails-asset-pipeline-best-practices . Basically, the answer, is yes, my understanding is correct.

Here are the relevant pieces of the article: 在此处输入图片说明

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM