简体   繁体   English

导轨引擎和资产

[英]rails engines and assets

I have a rails engine that has a widget in it. 我有一个带有小部件的Rails引擎。 I'm rendering the widget using a layout: false property in render and to style the widget, I'm recycling CSS on my main app. 我正在使用layout: false渲染小部件layout: false render中的layout: false属性并为小部件设置样式,我在主应用程序中回收CSS。

Now here's the tricky part, I only needed 3 stylesheets for my widget and 2 of them are in my main app, those are bootstrap.css and _ss_font.css. 现在这是棘手的部分,我的小部件只需要3个样式表,其中2个在我的主应用程序中,分别是bootstrap.css和_ss_font.css。 On development, it works fine but when I push it to production, I'm having an error on bootstrap.css, it seems that it's dependent on another file on my vendor assets. 在开发中,它工作正常,但是当我将其投入生产时,bootstrap.css出现错误,似乎它依赖于供应商资产上的另一个文件。 How do I include those assets? 如何包括这些资产?

Note: If I use =stylesheet_link_tag "application" my problem is partially resolved but I have to handle conflicting class names and it will include all assets on my application. 注意:如果我使用=stylesheet_link_tag "application"我的问题已部分解决,但我必须处理冲突的类名,并且它将包括我的应用程序中的所有资产。 I just want to load the necessary stylesheets to style my widget in the engine. 我只想加载必要的样式表以在引擎中设置我的小部件的样式。

EDIT 1 编辑1

As suggested I'm including a more descriptive explanation on this: 如建议的那样,我对此进行了更具描述性的解释:

So this is the error I'm having. 所以这是我的错误。

ActionView::Template::Error (File to import not found or unreadable: ss-variables. Load path: /data/serviceseeking_au_staging/releases/20140604114806 (in /data/serviceseeking_au_staging/releases/20140604114806/vendor/assets/stylesheets/bootstrap.scss)): ActionView :: Template :: Error(找不到要导入的文件或不可读的文件:ss变量。加载路径:/ data / serviceseeking_au_staging / releases / 20140604114806(在/ data / serviceseeking_au_staging / releases / 20140604114806 / vendor / assets / stylesheets / bootstrap中。 SCSS)):

 2: !!! 5 3: %html 4: %head 5: = stylesheet_link_tag "bootstrap", "_ss_font", "feedback/reviews-widget" 6: 7: %body 8: .reviews-container lib/action_logger.rb:31:in `call' 

This view is inside the engine. 该视图在引擎内部。 As you can see, this is my ideal goal, to only include 3 stylesheets. 如您所见,这是我的理想目标,只包含3个样式表。 There's not much to see on the reviews-widget , it's just sass without any external file dependency so I will not show it. reviews-widget没什么可看的,它只是sass,没有任何外部文件依赖性,因此我将不显示它。 So looking at the error message, where is this "ss-variable"? 因此,查看错误消息,此“ ss变量”在哪里? Well, let me take you guys on a field trip first before we arrive there. 好吧,在我们到达那里之前,让我先带你们进行实地考察。

I first traced where bootstrap.scss is and found it sitting on the main app under the vendor/assets/stylesheets folder. 我首先跟踪了bootstrap.scss所在的位置,发现它位于vendor/assets/stylesheets文件夹下的主应用程序中。 It's contents are as follows: 其内容如下:

 @import "bootstrap/variables";
 @import "bootstrap/mixins";

 @import "bootstrap/scaffolding";

 @import "bootstrap/grid"; 
 @import "bootstrap/layouts";

 etcetera...

So by tracing boostrap.scss and opening it, I saw that it's importing a partial named _variables.scss, so on with the tour and here's _variable.scss 因此,通过跟踪boostrap.scss并打开它,我看到它正在导入一个名为_variables.scss的局部文件,以此类推, _variable.scss_variable.scss

@import "ss-variables";

// Grays
// -------------------------
$black:                 #000 !default;
$grayDarker:            #222 !default;

( Note : I saw another boostrap.scss inside the /vendor/assets/stylesheets/bootstrap where _variables.ss is located) Aha! 注意 :我在/vendor/assets/stylesheets/bootstrap内看到了另一个_variables.ss所在的boostrap.scss)啊哈! There's that little freak, "ss-variable"!! 有点怪胎,“ SS变量”! We're getting close on this wild partial chase. 我们正在接近这个疯狂的局部追逐。 So I searched where ss-variables.scss is and found it on my lib/assets/stylesheets/ in the main app and it contains more scss variables. 因此,我搜索了ss-variables.scss所在的位置,并在主应用程序的lib/assets/stylesheets/中找到了它,它包含更多的scss变量。

So tracing them, I formulated this hypothesis that when trying to include bootstrap using stylesheet_link_tag on my engine, the engine doesn't see the dependent files that lies on the main app. 因此,为了追踪它们,我提出了这个假设,即当尝试在引擎上使用stylesheet_link_tag包含bootstrap时,引擎看不到主应用程序上的从属文件。

Bonus: I have a remarkable feeling that after this, the _ss_font css will be next. 奖励:我有一种非凡的感觉,在此之后, _ss_font css将是下一个。 But that's for another story. 但这是另一个故事。 Thanks. 谢谢。

From the looks of it, your problem might be caused by the asset_fingerprinting feature of the asset pipeline - basically appends all the production assets with an MD5 hash 从外观asset_fingerprinting ,您的问题可能是由资产管道的asset_fingerprinting功能引起的-基本是在所有production资产后附加MD5哈希

-- -

Asset Fingerprinting 资产指纹

Essentially, if you're using the likes of Heroku (which requires your assets to be precompiled for them to be served), you will basically get a series of files in your public/assets folder like this: 本质上,如果您使用的是Heroku (需要对其进行预编译才能提供这些文件),则基本上可以在public/assets文件夹中获得一系列文件,如下所示:

|-public
|--assets
|---images
|---stylesheets
|---javascripts

The files contained in this folder, when precompiled, all have the fingerprinted filename. 预编译时,此文件夹中包含的文件均具有fingerprinted文件名。 This means if you wanted to call one of these, you will need to use one of the ERB / SCSS preprocessors to access them: 这意味着,如果您要调用其中之一,则需要使用ERB / SCSS预处理器之一来访问它们:

#app/assets/stylesheets/application.css.scss
.style {
   background: asset_url("background.png");
}

This will reference the file, regardless of whether it's been precompiled (& fingerprinted ) or not. 这将引用该文件,而不管它是否已预编译(和fingerprinted )。 Although not exactly your issue, it is an important factor in what you're asking 尽管这不是您的问题,但这您要问的一个重要因素

-- -

Fix 固定

I would suggest your issue is likely that you're referencing "static" CSS files, which really need to be dynamically referenced. 我建议您的问题很可能是您引用的“静态” CSS文件,实际上确实需要动态引用。

How do I include those assets? 如何包括这些资产?

You'll be best using @import like this: 这样最好使用@import

#gem/app/assets/gem/widget.css.scss
@import "bootstrap.css" /* will probably need a relative path for this */
@import "other.css" /* again, will need a relative path */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM