Rails 3.1 资产管道和手动订购的 Javascript 需要

发布于 2024-11-10 11:41:28 字数 560 浏览 5 评论 0原文

我正在尝试将现有应用程序转换为新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

燕归巢 2024-11-17 11:41:28

您有两种可能的结构:第一种和第二种。
通过以下两个示例,您将在 /assets/externals.js 公开一个包。
您可以 javascript_include_tag 此包,但也可以在 application.js 文件中需要它。

第一个

vendor/
├── assets
│   ├── javascripts
│   │   ├── externals.js
│   │   ├── modernizr-1.7.js
│   │   └── underscore-1.1.6.js
│   └── stylesheets
└── plugins

文件 externals.js 包含:

//= require ./underscore-1.1.6.js
//= require ./modernizr-1.7.js

第二个

vendor/
├── assets
│   ├── javascripts
│   │   └── externals
│   │       ├── index.js
│   │       ├── modernizr-1.7.js
│   │       └── underscore-1.1.6.js
│   └── stylesheets
└── plugins

文件 index.js 包含:

//= require ./underscore-1.1.6.js
//= require ./modernizr-1.7.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 your application.js file.

The first one

vendor/
├── assets
│   ├── javascripts
│   │   ├── externals.js
│   │   ├── modernizr-1.7.js
│   │   └── underscore-1.1.6.js
│   └── stylesheets
└── plugins

The file externals.js contains :

//= require ./underscore-1.1.6.js
//= require ./modernizr-1.7.js

The second one

vendor/
├── assets
│   ├── javascripts
│   │   └── externals
│   │       ├── index.js
│   │       ├── modernizr-1.7.js
│   │       └── underscore-1.1.6.js
│   └── stylesheets
└── plugins

The file index.js contains :

//= require ./underscore-1.1.6.js
//= require ./modernizr-1.7.js
錯遇了你 2024-11-17 11:41:28

您可以按特定顺序要求每个文件,然后添加:

//= require_self

而不是:

//= require_tree .

You can require each file in particular order and then add:

//= require_self

instead of:

//= require_tree .
醉城メ夜风 2024-11-17 11:41:28

我的回答适用于Rails 3.1rc4,我不知道它与其他版本是否功能相同。

实际上,您可以将所有 require 语句放在 app/assets/javascripts/application.js 中,无论 .js 文件是否位于 app/assets/javascripts/ 或供应商/assets/javascripts/ 中,

如下所示:

// this is in app/assets/javascripts/application.js

//= require modernizr-2.0
//= require jquery
//= require jquery_ujs
//= require jqueryui-1.8.12
//= require jquery.easing-1.3
//= require jquery.noisy
//= require jquery.jslide-1.0
//= require respond
//= require smoke
//= require_tree

我在这里包含 require_tree 因为我还有其他我的个人控制器的 javascript 文件(pages.js.coffee、users.js.coffee)和用于站点范围内容的通用文件(site.js.coffee)

同时,这是文件结构。

app/
├── assets
│   ├── javascripts
│   │   ├── application.js
│   │   ├── pages.js.coffee
│   │   ├── users.js.coffee
│   │   └── site.js.coffee
│   └── stylesheets
└── plugins

vendor/
├── assets
│   ├── javascripts
│   │   ├── jquery.easing-1.3.js
│   │   ├── jquery.jslide-1.0.js
│   │   ├── jquery.noisy.js
│   │   ├── jqueryui-1.8.12.js
│   │   ├── modernizr-2.0.js
│   │   ├── respond.js
│   │   └── smoke.js
│   └── stylesheets
└── plugins

这使我能够控制供应商库的加载顺序(这通常很重要),而不必担心我的内部 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:

// this is in app/assets/javascripts/application.js

//= require modernizr-2.0
//= require jquery
//= require jquery_ujs
//= require jqueryui-1.8.12
//= require jquery.easing-1.3
//= require jquery.noisy
//= require jquery.jslide-1.0
//= require respond
//= require smoke
//= require_tree

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.

app/
├── assets
│   ├── javascripts
│   │   ├── application.js
│   │   ├── pages.js.coffee
│   │   ├── users.js.coffee
│   │   └── site.js.coffee
│   └── stylesheets
└── plugins

vendor/
├── assets
│   ├── javascripts
│   │   ├── jquery.easing-1.3.js
│   │   ├── jquery.jslide-1.0.js
│   │   ├── jquery.noisy.js
│   │   ├── jqueryui-1.8.12.js
│   │   ├── modernizr-2.0.js
│   │   ├── respond.js
│   │   └── smoke.js
│   └── stylesheets
└── plugins

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.

埋葬我深情 2024-11-17 11:41:28

我相信您可以将 library.js 放入您的 vendor/assets/javascripts 中,然后只需

//= require library.js

从您的 application.js 中获取,不是吗?

I believe you can put a library.js in your in vendor/assets/javascripts and then simply

//= require library.js

from your application.js, no?

始于初秋 2024-11-17 11:41:28

require_tree 完全按照您的指示执行。如果你给它

//= require_tree .

,它会加载调用 require_tree 的当前目录中的文件。如果你提供了它

//=require_tree ../../../vendor/assets/javascripts

,那么你将在供应商下获得 javascript。

我不喜欢 ../../.. 符号,因此我创建了一个名为vendor/assets/javascripts/vendor_application.js 的文件,其中包含:

//= require_tree .

在供应商目录下加载 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

//= require_tree .

it loads the files in the current directory where require_tree is called. If you give it

//=require_tree ../../../vendor/assets/javascripts

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:

//= require_tree .

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文