从 SCSS/Rails 3.2 w/sass-rails 导入 CSS 文件
我在供应商/资产/样式表中有一个 CSS 文件,我想在我的 application.css.scss 文件(或类似文件)中链接到它。我希望这只会渲染 @import
加上资产路径:
@import asset-path('lionbars.css', stylesheet)
但没有这样的运气:我得到
Invalid CSS after "@import ": expected uri, was "asset-path('lio..."
要么我语法错误,要么是因为 @import
不接受动态字符串,但我不确定。
所以我看到了一些选择。我可以:
- 找出适当的语法,如果这只是语法问题。
- 将我的文件重命名为 application.css.scss.erb 并将其他现有资源路径引用替换为
<%= asset_path('random/asset.png') %>
。这让我的语法荧光笔非常混乱,而且可能效率不高。否则,这有效。 - 将我的 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:
- Figure out the appropriate syntax, if it's just a syntax issue.
- 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. - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 ofasset-path
.Alternitavely, you could use
add_import_path
in your config.rb. See Compass Configuration Reference正确的解决方案:
@import 'lionbars'
。 Sass-rails 自定义@import
指令来自动使用资产管道。The correct solution:
@import 'lionbars'
. Sass-rails customizes the@import
directive to automatically use the asset pipeline.sass-rails 网站非常清楚:
...但是我在开发和生产中尝试了直接的 css 文件,并且效果很好:
所以除非有人有更好的主意,否则这就是我要做的。当 Sprockets 尝试编译它时,我确实必须将 IE8 特定注释移到我的布局中,但我仍然发现这比其他选择更容易。如果您有比这更好的解决方案,我很高兴听到!我对使用 sass-rails 页面上明确反对的解决方案并不感到兴奋。
The sass-rails website is quite clear:
...but I tried it in both dev and production for straight-up css files and it worked just fine:
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.