在Rails引擎中使用Sass @import指令时,无法在插件中找到资产 [英] Sass @import directive when used in Rails engine can't find assets in plugins

查看:321
本文介绍了在Rails引擎中使用Sass @import指令时,无法在插件中找到资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在构建一个使用zurb-foundation样式表的Rails引擎,并保持同样的Sass :: Syntax错误。这似乎很奇怪,因为我在Rails应用程序中遵循了相同的过程,并且在第一次尝试时就使用了该过程。因此,我决定通过启动两个新的Rails项目(一个应用程序和一个引擎)来解决问题,并配置它们以最少的设置使用Foundation。

I was building a Rails engine that uses zurb-foundation's stylesheets, and kep hitting the same Sass::Syntax error. This seemed strange because I had followed the same procedure in a Rails application and it was worked on the first try. So I decided to zero in on the isse by starting two new rails projects--one application and one engine--and configure them to use foundation with a minimal amount of setup.

我从2个干净的Rails 3.2.9项目开始-一个应用程序和一个 engine --full 项目,并通过添加foundation_and_overrides.scss和<$手动为基础手动配置它们c $ c>要求-将其放入CSS清单中。

I started with 2 clean Rails 3.2.9 projects--one application and one engine --full and configured them both manually for foundations by adding foundation_and_overrides.scss and require-ing it in the CSS manifest.

然后我创建了一个准系统控制器,因此我需要一个页面载入。对于应用程序,加载该页面并查看页面源代码,可以看到基础CSS已正确编译并加载。

I then creating a bare-bones controller so I'd have a page to load. For the application, loading that page and looking at the page source, I can see that foundation CSS was compiled correctly and loaded.

当我为引擎尝试相同的操作时,出现此错误:

When I tried the same thing for the engine, I got this error:

Sass::SyntaxError in Main#index

Showing /<PLUGIN_PATH>/test/dummy/app/views/layouts/application.html.erb where line #5 raised:

File to import not found or unreadable: foundation/common/ratios.
Load path: /<PLUGIN_PATH>/test/dummy
  (in /<PLUGIN_PATH>/app/assets/stylesheets/example_plugin/foundation_and_overrides.scss)

(请注意,虚拟应用的CSS清单是 require -插入插件的CSS清单,是 require -ing foundation_and_overrides.scss。接线显然不是问题,因为错误是来自foundation_and_overrides.scss)

(Note that the dummy app's CSS manifest is require-ing the plugin's CSS manifest, which is require-ing foundation_and_overrides.scss. The wiring is clearly not the issue because the error is raised from foundation_and_overrides.scss)

我用几种Ruby-1.9.3版本(p125、194,p327,我认为)进行了尝试,结果相同。

I tried this with several builds of Ruby-1.9.3 (p125, 194, p327, I think) with the same results.

它仍然没有当我将此行添加到test / dummy / config / application.rb时可以正常工作:

It still didn't work when I added this line to test/dummy/config/application.rb:

config.assets.paths << "#{Foundation::Engine.root}/scss"

即使是Foundation / common / _ratios。

even though foundation/common/_ratios.scss exists at that path.

问题可能出在Sass,Sprockets,Compass,Foundation或Rails,所以我不知道从哪里去。

The issue may be with Sass, Sprockets, Compass, Foundation or Rails, so I don't know where to go from here.

我将其报告为基金会存在的问题,但我不太相信。

I reported it as an issue with Foundation, but I'm not so convinced of that.

以前有人看过吗?

推荐答案

我在这个Stackoverflow问题

问题是Rails引擎的虚拟应用程序和常规Rails应用程序的不同的config / application.rb文件。

The issue is that a Rails engine's dummy app and a regular Rails app have slightly different config/application.rb files.

在Rails应用中,您将在顶部看到以下代码:

In a Rails app, you'll see this code towards the top:

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

但是在引擎虚拟应用中,您会看到:

But in an engine dummy app, instead you see:

Bundler.require
require "<PLUGIN_NAME>"

在Rails应用程序中,第一个Bundler.require包括Gemfile中的所有组,但不包括:assets,除非Rails.env是:development或:test
第二个Bundler.require包括:default(即Gemfile中不在组中的任何东西)以及:assets和:production,:development或:test

In the Rails app, the first Bundler.require include all groups in the Gemfile but excludes :assets unless Rails.env is :development or :test The second Bundler.require includes :default--that is, anything in your Gemfile that's not in a group--as well as :assets and either :production, :development or :test

在虚拟应用程序中,这些都不是必需的,因为1)不适用于生产环境; 2)测试和开发通常具有相似的依赖要求。由于:assets组只是将依赖项应用于:test和:development而不是应用于:production的便捷方法,因此在虚拟应用程序中,它与:default相同。因此,在虚拟应用程序中,仅需:default即可。由于 Bundler.require Bundler.require(:default)(警告:前面的破坏者)相同,因此该虚拟对象应用仅使用 Bundler.require 。这意味着您Gemfile中位于组中的所有内容都将被忽略。

In a dummy app, none of this is necessary because 1) it's not meant to be used in production, and 2) test and development typically have similar dependency requirements. Since the :assets group is just a convenient way to apply dependencies to :test and :development but not to :production, in a dummy app it's the same as :default. So in a dummy app, nothing but :default is needed. Since Bundler.require is (Warning: Spoiler ahead) the same as Bundler.require(:default), the dummy app just uses Bundler.require. This means that anything in your Gemfile that's in a group will be ignored.

所以我遇到的问题是我正在从Rails应用程序Gemfile复制粘贴到一个Rails虚拟应用程序Gemfile,包括 group:资产做... 。我删除了 group 行,一切正常!

So the problem I had was that I was copy-and-pasting from a Rails app Gemfile into a Rails dummy app Gemfile, including group :assets do.... I removed the group line and everything worked!

当天晚些时候在11/29添加:

ADDED later in the day on 11/29:

为澄清起见,我看到的错误是因为不包括sass-rails(因为它在我的Gemfile中的:assets组中),所以不是解析我的资产作为Sass,它们被解析为CSS。由于 @import 对CSS的含义不同于对Sass的含义,因此解析CSS文件所使用的是不同的 @import 找不到文件。

To clarify, the error I was seeing is because sass-rails wasn't included (since it was in the :assets group in my Gemfile), so instead of my assets being parsed as Sass, they were parsed as CSS. Since @import means something different to CSS than to Sass, whatever was parsing the CSS file was using a different implementation of @import and couldn't find the file.

这篇关于在Rails引擎中使用Sass @import指令时,无法在插件中找到资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆