资产管道缓存CSS?
我正在开发 Rails 3.1 应用程序。我创建了一个 application.css.scss.erb
文件。 .erb 位于最后,因为我想从配置文件加载一个变量作为 css 中的颜色变量:
$highlight1: #<%= COLOR.highlight1 %>;
$highlight2: #<%= COLOR.highlight2 %>;
一切正常,但我遇到的问题是,每当我更改 COLOR.highlight1 中的值时,它都不会直到我进入我的 css 文件并更改某些内容(我通常添加一些空格并保存)后才会反映更改。那是我看到变化的时候。显然,rails 正在查看文件是否已更改,以便更新更改。
有没有什么办法至少在开发过程中可以将其关闭,并且我可以看到更改而不必修改 css 文件?
也欢迎对我的技术提出任何批评/意见
I am working on a Rails 3.1 app. I have created an application.css.scss.erb
file. The .erb is in the end because I want to load a variable from the config file as the color variable in the css:
$highlight1: #<%= COLOR.highlight1 %>;
$highlight2: #<%= COLOR.highlight2 %>;
Everything works fine, but the problem I am having is that whenever I change a value inside COLOR.highlight1, it doesn't reflect the change until I go in to my css file and change something (i usually add some spaces and save it). Thats when I see the change. Clearly rails is looking to see if the file was changed in order to update the change.
Is there any way that at least during development, this can be turned off and I can see the changes without having to also modify the css file?
Any critique/opinions on my technique are also welcome
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Sprockets
depend_on
指令用于声明此类依赖关系。因此,在 css.scss.erb 文件的顶部,与其他指令(require 和朋友)一起放置如下内容:然后,当文件
/path/to/colors.rb
更改时,它将强制 CSS 也更新。不幸的是,我从来没有让它与资产目录之一(javascripts/stylesheets/images)外部的文件的相对路径一起使用,因此Sprockets解析路径的方式可能会阻止这个,否则我会错过一些东西。这让您可以选择指定绝对路径,这几乎肯定无法在所有应用程序环境中工作,或者将常量文件放入资产目录中(例如,app/assets/stylesheets/colors.rb)。
作为参考,这里是来自 Sprockets (2.0.3) 源代码的
depend_on
指令的文档,位于 sprockets/directive_processor.rb如果有人知道如何指定到其他位置(如 config/initializers)的相对路径或者什么,请告诉我!
The Sprockets
depend_on
directive is used to declare these kinds of dependencies. So at the top of your css.scss.erb file, with the other directives (require and friends), put something like:Then when the file
/path/to/colors.rb
changes, it will force the css to update too.Unfortunately, I have never gotten this to work with a relative path to a file outside of one of the asset directories (javascripts/stylesheets/images) so there may be something in the way Sprockets resolves paths that prevents this, or else I'm missing something. That leaves you with the options of specifying an absolute path, which will almost certainly not work in across all your app environments, or putting the constants file into your asset directories (app/assets/stylesheets/colors.rb, for example).
For reference, here's the doc for the
depend_on
directive from the Sprockets (2.0.3) source, in sprockets/directive_processor.rbIf anyone does know a way to specify relative paths to other places like config/initializers or something, please let me know!
除了大卫·费伯的回答。我也需要使用相对路径。
我想生成一个带有区域设置字典的 js 文件,如果区域设置文件发生更改,该文件将会更新:
事实证明,当前(Rails 3.2.3)相对路径仅在相对路径也在资产路径中时才有效!
所以丑陋的解决方案是在 config/application.rb 中添加路径:
In addition to David Faber's answer. I needed to use relative paths too.
I wanted to generate a js file with the locale dictionary, which would update if the locale files were changed:
Turns out that currently (Rails 3.2.3) relative paths only work if the relative path is also in the assets path!
So the ugly solution is to add the path in
config/application.rb
:http://guides.rubyonrails.org/configuring.html
可能想尝试一下,但我不确定它是否实时编译,至少它应该禁用缓存。
http://guides.rubyonrails.org/configuring.html
might want to try that, I'm not sure if its getting compiled real time though, at least it should disable the caching.
也许可以尝试:
在您的开发配置文件中
maybe try:
in your development config file
我尝试这个,它
在 application.rb 中
的 lib/assets_variables/color.rb 中
的 app/assets/stylesheets/color.css.scss.erb 中工作
I try this, it work
in application.rb
in lib/assets_variables/color.rb
in app/assets/stylesheets/color.css.scss.erb