资产管道缓存CSS?

发布于 2024-12-07 20:05:43 字数 448 浏览 0 评论 0原文

我正在开发 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 技术交流群。

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

发布评论

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

评论(5

挽梦忆笙歌 2024-12-14 20:05:43

Sprockets depend_on 指令用于声明此类依赖关系。因此,在 css.scss.erb 文件的顶部,与其他指令(require 和朋友)一起放置如下内容:

//= depend_on "/path/to/colors.rb"

然后,当文件 /path/to/colors.rb 更改时,它将强制 CSS 也更新。

不幸的是,我从来没有让它与资产目录之一(javascripts/stylesheets/images)外部的文件的相对路径一起使用,因此Sprockets解析路径的方式可能会阻止这个,否则我会错过一些东西。这让您可以选择指定绝对路径,这几乎肯定无法在所有应用程序环境中工作,或者将常量文件放入资产目录中(例如,app/assets/stylesheets/colors.rb)。

作为参考,这里是来自 Sprockets (2.0.3) 源代码的 depend_on 指令的文档,位于 sprockets/directive_processor.rb

  # Allows you to state a dependency on a file without
  # including it.
  #
  # This is used for caching purposes. Any changes made to
  # the dependency file will invalidate the cache of the
  # source file.
  #
  # This is useful if you are using ERB and File.read to pull
  # in contents from another file.
  #
  #     //= depend_on "foo.png"
  #

如果有人知道如何指定到其他位置(如 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:

//= depend_on "/path/to/colors.rb"

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.rb

  # Allows you to state a dependency on a file without
  # including it.
  #
  # This is used for caching purposes. Any changes made to
  # the dependency file will invalidate the cache of the
  # source file.
  #
  # This is useful if you are using ERB and File.read to pull
  # in contents from another file.
  #
  #     //= depend_on "foo.png"
  #

If anyone does know a way to specify relative paths to other places like config/initializers or something, please let me know!

陈年往事 2024-12-14 20:05:43

除了大卫·费伯的回答。我也需要使用相对路径。

我想生成一个带有区域设置字典的 js 文件,如果区域设置文件发生更改,该文件将会更新:

//= depend_on "../../../config/locales/en.yml"
//= depend_on "../../../config/locales/ja.yml"

var locales = <%= locales.to_json %>;

事实证明,当前(Rails 3.2.3)相对路径仅在相对路径也在资产路径中时才有效!

所以丑陋的解决方案是在 config/application.rb 中添加路径:

config.assets.paths.unshift Rails.root.join("config", "locales").to_s

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:

//= depend_on "../../../config/locales/en.yml"
//= depend_on "../../../config/locales/ja.yml"

var locales = <%= locales.to_json %>;

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:

config.assets.paths.unshift Rails.root.join("config", "locales").to_s
空宴 2024-12-14 20:05:43

http://guides.rubyonrails.org/configuring.html

  • config.assets.compile 是一个布尔值,可用于在生产中打开实时 Sprockets 编译。

可能想尝试一下,但我不确定它是否实时编译,至少它应该禁用缓存。

http://guides.rubyonrails.org/configuring.html

  • config.assets.compile is a boolean that can be used to turn on live Sprockets compilation in production.

might want to try that, I'm not sure if its getting compiled real time though, at least it should disable the caching.

一个人的旅程 2024-12-14 20:05:43

也许可以尝试:

config.assets.digest = true

在您的开发配置文件中

maybe try:

config.assets.digest = true

in your development config file

绮筵 2024-12-14 20:05:43

我尝试这个,它

在 application.rb 中

config.autoload_paths += %W(#{config.root}/lib/assets_variables)
config.assets.paths << File.join(Rails.root, 'lib', 'assets_variables')

的 lib/assets_variables/color.rb 中

module Color
  def self.default
    'blue'
  end
end

的 app/assets/stylesheets/color.css.scss.erb 中工作

//= depend_on "color.rb"
$default_color: <%= Color::default %>;
.content {
  color: $default_color;
}

I try this, it work

in application.rb

config.autoload_paths += %W(#{config.root}/lib/assets_variables)
config.assets.paths << File.join(Rails.root, 'lib', 'assets_variables')

in lib/assets_variables/color.rb

module Color
  def self.default
    'blue'
  end
end

in app/assets/stylesheets/color.css.scss.erb

//= depend_on "color.rb"
$default_color: <%= Color::default %>;
.content {
  color: $default_color;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文