Rails 3.1 资产管道和手动订购的 Javascript 需要
我正在尝试将现有应用程序转换为新的 3.1 资产管道布局,并希望包含许多必须按特定顺序排列的供应商文件(underscore.js 和backbone 是一对)。因此,我不能只使用 = require_tree .
来提取我的供应商文件(无需使用前缀重命名每个文件。恶心)。
以下内容位于我的 app/assets/javascripts/application.js
文件中:
//= require modernizr-1.7 //= require jquery-1.6.1 //= require underscore-1.1.5 //= require backbone-0.3.3 //= require_tree .
我尝试了带/不带扩展名、带/不带 require_tree 和带/不带相对路径的每种组合,但没有任何效果。我的所有供应商文件都位于 /vendor/assets/javascripts/
中。
我觉得我很愚蠢,因为这似乎是一个如此明显的用例(包括按顺序排列的特定文件在 JS 中很常见,不是吗?)我一定在做一些白痴的事情?
I am trying to convert an existing app to the new 3.1 asset pipeline layout, and want to include a lot of vendor files that have to be in a specific order, (underscore.js and backbone being one pair). As such, I can't just use a = require_tree .
to pull in my vendor files, (without renaming each file with a prefix. Yuck).
The following is within my app/assets/javascripts/application.js
file:
//= require modernizr-1.7 //= require jquery-1.6.1 //= require underscore-1.1.5 //= require backbone-0.3.3 //= require_tree .
I have tried every combination of with/out extensions, with/out the require_tree and with/out the relative paths, and nothing works. All of my vendor files are in /vendor/assets/javascripts/
.
I feel like I am being stupid because this seems like such an obvious use case, (including specific files by name in an order is common with JS, no?) that I must be doing something idiotic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您有两种可能的结构:第一种和第二种。
通过以下两个示例,您将在
/assets/externals.js
公开一个包。您可以
javascript_include_tag
此包,但也可以在application.js
文件中需要它。第一个
文件
externals.js
包含:第二个
文件
index.js
包含:You have two possible structure : the first one and the second one.
With both following examples, you expose a package at
/assets/externals.js
.You can
javascript_include_tag
this package, but you can also require it in yourapplication.js
file.The first one
The file
externals.js
contains :The second one
The file
index.js
contains :您可以按特定顺序要求每个文件,然后添加:
而不是:
You can require each file in particular order and then add:
instead of:
我的回答适用于Rails 3.1rc4,我不知道它与其他版本是否功能相同。
实际上,您可以将所有 require 语句放在 app/assets/javascripts/application.js 中,无论 .js 文件是否位于 app/assets/javascripts/ 或供应商/assets/javascripts/ 中,
如下所示:
我在这里包含 require_tree 因为我还有其他我的个人控制器的 javascript 文件(pages.js.coffee、users.js.coffee)和用于站点范围内容的通用文件(site.js.coffee)
同时,这是文件结构。
这使我能够控制供应商库的加载顺序(这通常很重要),而不必担心我的内部 javascript,其中顺序通常不太重要。
更重要的是,我将所有 require 语句控制在一个经常使用的文件中,我发现这样既安全又干净。
My answer applies to Rails 3.1rc4, I don't know whether it functions the same with other versions.
You can actually put all require statements in app/assets/javascripts/application.js whether or not the .js files are in app/assets/javascripts/ or vendor/assets/javascripts/
Like so:
I included require_tree here because I have other javascript files for my individual controllers (pages.js.coffee, users.js.coffee) and a general one for site-wide stuff (site.js.coffee)
Meanwhile here's the file structure.
This allows me to control the load order of vendor libraries (which matters a lot, usually) and not worry about my internal javascript, where order generally matters less.
More importantly, I control all require statements within one often used file, I find that both safer and cleaner.
我相信您可以将
library.js
放入您的vendor/assets/javascripts
中,然后只需从您的
application.js
中获取,不是吗?I believe you can put a
library.js
in your invendor/assets/javascripts
and then simplyfrom your
application.js
, no?require_tree 完全按照您的指示执行。如果你给它
,它会加载调用 require_tree 的当前目录中的文件。如果你提供了它
,那么你将在供应商下获得 javascript。
我不喜欢 ../../.. 符号,因此我创建了一个名为vendor/assets/javascripts/vendor_application.js 的文件,其中包含:
在供应商目录下加载 javascript。
请注意,require 确实会搜索 3 个管道位置(app、lib、vendor)来查找需要的文件。 require_tree 是字面意思,这可能就是它应该的方式。
Railscast对此非常有帮助: http://railscasts.com/episodes/279 -了解资产管道
require_tree does exactly what you tell it. If you give it
it loads the files in the current directory where require_tree is called. If you give it
then you'll get the javascript under vendor.
I did not like the ../../.. notation, so I created a file called vendor/assets/javascripts/vendor_application.js which contains:
That loads the javascript under the vendor directory.
Note, require does search the 3 pipeline locations (app, lib, vendor) for the file to require. require_tree is literal, which is probably the way it should be.
The railscast on this is very helpful: http://railscasts.com/episodes/279-understanding-the-asset-pipeline