Rails 资产使用说明
Rails assets usage clarification
我是 rails 的新手,我对资产的加载方式有些困惑。我可以让事情正常进行,但我想了解幕后真正发生的事情。
我一直在阅读文档,但有些地方我不完全理解。
清单文件
首先让我感到困惑的是清单文件的用法。
例如,如果在我的 app/assets/javascripts/application.js 文件中,我有:
//= require_tree .
Rails 文档说:
tells Sprockets to recursively include all JavaScript files in the
specified directory into the output
我不清楚是哪个目录? app/assets/javascripts/?
这是否意味着如果我在 app/assets/javascripts/ 中添加一个文件,它将被加载和提供?
如果我添加一个 gem 需要添加一个文件 example.js 我需要添加到清单文件:
//= require example.js
但是,如果 //= require_tree
已经在 app/assets/javascripts/ 中加载和提供文件,这就是我放置 example.js 的位置,为什么还需要这样做?好的,如果以后我添加更多要求,这允许我指定顺序。但除此之外?
HTML 文件
然后脚本要包含在application.html.erb
<%= javascript_include_tag ('application'), 'data-turbolinks-track' => true %>
我知道这会加载上面提到的 application.js 文件,因此会加载其中的各种 //= require
。
有时需要将特定文件编写为脚本:
<script src="js/example.js"></script>
这种情况不会 <script src="js/example.js"></script>
做与 //= require example.js
完全相同的事情吗?
PUBLIC VS 应用资产
我知道如果我将我的 example.js 文件放在 public/assets 文件夹中,它不会被编译,而是单独提供。我为什么要这么做?万一文件在连接和编译时未正确提供并且仅在分开提供时才有效,这样做是否合理?换句话说,如果我在 app/assest 上包含一个 .js 文件并且它在加载或破坏东西时遇到问题,是否值得尝试将其从那里删除并将其移动到 public/assets 或者只是这样做没有意义?
应用资产与供应商资产
在哪种情况下我应该将文件添加到供应商资产而不是应用程序资产?将它添加到一个地方或另一个地方有什么区别?
在我的 vendor/assets/javascripts 中,我只有一个空的 .keep 文件。所以 app/assets 上有清单文件之类的东西。那么这个文件夹中的文件是如何被引用的呢?
For directives that take a path argument, you may specify either a
logical path or a relative path. Relative paths begin with ./ and
reference files relative to the location of the current file.
因此 //= require_tree .
告诉 sprockets 加载 app/assets/javascripts/
中的任何文件并将它们连接到 application.js.
Is this scenario wouldn't be <script src="js/example.js"></script>
doing the exact same thing of //= require example.js?
没有。 Rails 在开发中将资产作为单独的文件提供,以便在发生错误时获得有意义的行号和文件引用。
在生产中,它连接并缩小了对性能很重要的资产。
Sprockets 不会检查您的视图/布局中的脚本标签。所以前者会导致两个请求。
I understand that if I place my example.js file on public/assets
folder it won't be compiled but served separately. Why would I do
that?
public 目录位于服务器网站根目录下。由于那里的文件在没有太多干预的情况下提供,因此它是错误页面之类的东西的好地方,或者您需要具有静态名称但没有缓存破坏指纹的资产的地方。
On which scenario should I add a file to vendor assets instead of app
assets? What is the difference between adding it to a place or
another?
/vendor/assets
是放置非您创建的或不属于应用程序的资产的地方。两者都添加到链轮负载路径中,因此结果相同。这只是代码组织的问题。
我是 rails 的新手,我对资产的加载方式有些困惑。我可以让事情正常进行,但我想了解幕后真正发生的事情。
我一直在阅读文档,但有些地方我不完全理解。
清单文件
首先让我感到困惑的是清单文件的用法。
例如,如果在我的 app/assets/javascripts/application.js 文件中,我有:
//= require_tree .
Rails 文档说:
tells Sprockets to recursively include all JavaScript files in the specified directory into the output
我不清楚是哪个目录? app/assets/javascripts/?
这是否意味着如果我在 app/assets/javascripts/ 中添加一个文件,它将被加载和提供?
如果我添加一个 gem 需要添加一个文件 example.js 我需要添加到清单文件:
//= require example.js
但是,如果 //= require_tree
已经在 app/assets/javascripts/ 中加载和提供文件,这就是我放置 example.js 的位置,为什么还需要这样做?好的,如果以后我添加更多要求,这允许我指定顺序。但除此之外?
HTML 文件
然后脚本要包含在application.html.erb
<%= javascript_include_tag ('application'), 'data-turbolinks-track' => true %>
我知道这会加载上面提到的 application.js 文件,因此会加载其中的各种 //= require
。
有时需要将特定文件编写为脚本:
<script src="js/example.js"></script>
这种情况不会 <script src="js/example.js"></script>
做与 //= require example.js
完全相同的事情吗?
PUBLIC VS 应用资产
我知道如果我将我的 example.js 文件放在 public/assets 文件夹中,它不会被编译,而是单独提供。我为什么要这么做?万一文件在连接和编译时未正确提供并且仅在分开提供时才有效,这样做是否合理?换句话说,如果我在 app/assest 上包含一个 .js 文件并且它在加载或破坏东西时遇到问题,是否值得尝试将其从那里删除并将其移动到 public/assets 或者只是这样做没有意义?
应用资产与供应商资产
在哪种情况下我应该将文件添加到供应商资产而不是应用程序资产?将它添加到一个地方或另一个地方有什么区别? 在我的 vendor/assets/javascripts 中,我只有一个空的 .keep 文件。所以 app/assets 上有清单文件之类的东西。那么这个文件夹中的文件是如何被引用的呢?
For directives that take a path argument, you may specify either a logical path or a relative path. Relative paths begin with ./ and reference files relative to the location of the current file.
因此 //= require_tree .
告诉 sprockets 加载 app/assets/javascripts/
中的任何文件并将它们连接到 application.js.
Is this scenario wouldn't be
<script src="js/example.js"></script>
doing the exact same thing of //= require example.js?
没有。 Rails 在开发中将资产作为单独的文件提供,以便在发生错误时获得有意义的行号和文件引用。
在生产中,它连接并缩小了对性能很重要的资产。
Sprockets 不会检查您的视图/布局中的脚本标签。所以前者会导致两个请求。
I understand that if I place my example.js file on public/assets folder it won't be compiled but served separately. Why would I do that?
public 目录位于服务器网站根目录下。由于那里的文件在没有太多干预的情况下提供,因此它是错误页面之类的东西的好地方,或者您需要具有静态名称但没有缓存破坏指纹的资产的地方。
On which scenario should I add a file to vendor assets instead of app assets? What is the difference between adding it to a place or another?
/vendor/assets
是放置非您创建的或不属于应用程序的资产的地方。两者都添加到链轮负载路径中,因此结果相同。这只是代码组织的问题。