Rails 3.1,从资产管道中排除 JS 文件

发布于 2024-12-22 10:44:13 字数 1329 浏览 1 评论 0原文

我知道已经有一百万个问题与此相关,但我无法理解。

我想将大部分 JS 文件包含在资产管道中,但我想有条件地加载一些文件(或仅在某些页面上)。这些都是大而复杂的文件,95% 的用户永远不会使用它们,所以我不想为每个用户加载它们。一组 JS 文件用于日历,放置在:

app/assets/javascripts/calendar

因此我的清单设置为仅包含顶级目录(并排除日历子目录):

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory .

我的 config/environments/production.rb:

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs.
config.assets.digest = true

# This following config is left over from previous Rails app,
# so not sure if it's now unnecessary ...
# Disable Rails's static asset server
# In production, Apache or nginx will already do this
config.serve_static_assets = false

在视图中,我正在使用Ryan Bates 的 nifty_layout 手动包含日历文件:

javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"

我已经尝试在开发和生产中进行预编译 - 文档不清楚我应该在哪里执行此操作,但它看起来像生产。

当我运行该页面时,我得到这样的信息:

ActionView::Template::Error (calendar/date.js isn't precompiled)

我不想预编译它。我想要手动加载。 (实际上,在创建的主 application.js 之外的文件中预编译是可以的,但我不知道该怎么做。)

解决方案是什么?

谢谢!

I know there are a million questions already on this, but I can't get this.

I want to include most of my JS files in the asset pipeline, but I have a few I want to load conditionally (or only on certain pages). These are big, complicated files and will never, ever be used by 95% of the users, so I'd rather not have them loaded for every user. One set of JS files is for a calendar, placed in:

app/assets/javascripts/calendar

So my manifest is set up to include only the top directory (and exclude the calendar subdirectory):

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_directory .

My config/environments/production.rb:

# Compress JavaScripts and CSS
config.assets.compress = true

# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false

# Generate digests for assets URLs.
config.assets.digest = true

# This following config is left over from previous Rails app,
# so not sure if it's now unnecessary ...
# Disable Rails's static asset server
# In production, Apache or nginx will already do this
config.serve_static_assets = false

In the view, I'm using Ryan Bates' nifty_layout to manually include the calendar files:

javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"

I've tried precompiling in both development and production -- the docs aren't clear where I'm supposed to do this, but it looks like production.

And when I run the page, I get this:

ActionView::Template::Error (calendar/date.js isn't precompiled)

I don't want it precompiled. I want it loaded manually. (Actually, it would be OK to precompile in a file other than the main application.js that is created, but I don't know how to do that.)

What's the solution?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

像极了他 2024-12-29 10:44:13

好吧,我没有意识到它是如何工作的,但我认为我已经弄清楚了。

将要手动加载的文件添加到 config/environments/development.rb 中,如下所示:

config.assets.precompile += %w( calendar/*.js jquery_calendar/*.css )

我认为这只是将它们折叠到 application.js 和 application.css 中,但显然不是 - 它将它们编译为单独的文件。

然后,您可以按照传统方式调用这些文件(在本例中,使用 nifty_layout):

javascript "calendar/date.js", "calendar/jquery.weekcalendar.js", "calendar/custom.js"

OK, I didn't realize this was how it works, but I think I figured it out.

Add the files to be manually loaded to config/environments/production.rb like so:

config.assets.precompile += %w( calendar/*.js jquery_calendar/*.css )

I thought this just folded them into the application.js and application.css, but apparently not -- it compiles them as individual files.

Then, you can call the files as you traditionally would (in this case, using nifty_layout):

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