从资产管道中排除JS文件

从资产管道中排除JS文件

本文介绍了Rails 3.1,从资产管道中排除JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经有100万个问题,但我无法解决.

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

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

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 .

我的配置/环境/production.rb:

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

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

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)

我不希望它被预编译.我要手动加载. (实际上,可以预编译除所创建的主要application.js以外的文件,但是我不知道该怎么做.)

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.)

有什么解决方案?

谢谢!

推荐答案

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

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

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

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

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

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

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

然后,您可以像传统上那样调用文件(在这种情况下,使用nifty_layout):

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"

这篇关于Rails 3.1,从资产管道中排除JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:27