从 SCSS/Rails 3.2 w/sass-rails 导入 CSS 文件

发布于 2025-01-05 07:42:14 字数 935 浏览 1 评论 0原文

我在供应商/资产/样式表中有一个 CSS 文件,我想在我的 application.css.scss 文件(或类似文件)中链接到它。我希望这只会渲染 @import 加上资产路径:

@import asset-path('lionbars.css', stylesheet)

但没有这样的运气:我得到

Invalid CSS after "@import ": expected uri, was "asset-path('lio..."

要么我语法错误,要么是因为 @import 不接受动态字符串,但我不确定。

所以我看到了一些选择。我可以:

  1. 找出适当的语法,如果这只是语法问题。
  2. 将我的文件重命名为 application.css.scss.erb 并将其他现有资源路径引用替换为 <%= asset_path('random/asset.png') %>。这让我的语法荧光笔非常混乱,而且可能效率不高。否则,这有效。
  3. 将我的 css 文件重命名为 _lionbars.css.scss 并调用 @import "lionbars"。我不想这样做有两个原因,一是一般来说我不想接触供应商的代码,二是其中有一些 IE8 特定的东西() 不能放入 scss 文件中,而且我不想必须分解该文件(而且我仍然需要找到一个IE8 代码的解决方案以及如何包含它)。

所以,我希望答案是#1,但我不确定。有什么想法吗?

I've got a CSS file in vendor/assets/stylesheets, and I'd like to link to it in my application.css.scss file (or thereabouts). I was hoping that this would just render @import plus the asset path:

@import asset-path('lionbars.css', stylesheet)

But no such luck: I get

Invalid CSS after "@import ": expected uri, was "asset-path('lio..."

Either I've got the syntax wrong, or it's because @import doesn't accept dynamic strings, but I'm not sure.

So I see a few options. I can either:

  1. Figure out the appropriate syntax, if it's just a syntax issue.
  2. Rename my file to application.css.scss.erb and replace other existing asset-path references to <%= asset_path('random/asset.png') %>. This makes my syntax highlighter very confused, and probably isn't all that efficient. Otherwise, this works.
  3. Rename my css file to _lionbars.css.scss and call @import "lionbars". Two reasons I don't want to do this, is #1 generally speaking I don't want to touch vendored code, and #2 there's some IE8 specific stuff in there (<!--[if IE 8]>blahblah<![endif]-->) that can't go in an scss file, and I don't want to have to break the file up (and I'd still have to find a solution for the IE8 code and how to include that).

So, I'm hoping that the answer is #1, but I'm not sure. Any ideas?

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

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

发布评论

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

评论(3

内心荒芜 2025-01-12 07:42:14

sass-rails 具有资产助手功能,例如 asset-path。否则,Sass 和 Compass 没有任何资产路径的概念。

或者,您可以在 config.rb 中使用 add_import_path查看指南针配置参考

sass-rails features asset helpers like asset-path. Otherwise, Sass and Compass don't have any concept of asset-path.

Alternitavely, you could use add_import_path in your config.rb. See Compass Configuration Reference

≈。彩虹 2025-01-12 07:42:14

正确的解决方案:@import 'lionbars'。 Sass-rails 自定义 @import 指令来自动使用资产管道。

The correct solution: @import 'lionbars'. Sass-rails customizes the @import directive to automatically use the asset pipeline.

淡淡的优雅 2025-01-12 07:42:14

sass-rails 网站非常清楚:

Sprockets 提供了一些放置在注释中的指令,称为 require、require_tree 和 require_self。 不要在您的 SASS/SCSS 文件中使用它们

...但是我在开发和生产中尝试了直接的 css 文件,并且效果很好:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * ...
 *
 *= require_self
 *= require_tree .
 *= require lionbars
*/

所以除非有人有更好的主意,否则这就是我要做的。当 Sprockets 尝试编译它时,我确实必须将 IE8 特定注释移到我的布局中,但我仍然发现这比其他选择更容易。如果您有比这更好的解决方案,我很高兴听到!我对使用 sass-rails 页面上明确反对的解决方案并不感到兴奋。

The sass-rails website is quite clear:

Sprockets provides some directives that are placed inside of comments called require, require_tree, and require_self. DO NOT USE THEM IN YOUR SASS/SCSS FILES.

...but I tried it in both dev and production for straight-up css files and it worked just fine:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * ...
 *
 *= require_self
 *= require_tree .
 *= require lionbars
*/

So unless anybody has a better idea, this is what I'm going to do. I did have to move the IE8 specific comment into my layout as Sprockets threw up trying to compile it, but I still find this easier than the alternatives. If you've got a better solution than this though, I'd be glad to hear it! I'm not thrilled about using a solution that's explicitly frowned upon on the sass-rails page.

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