Rails 在更改时触发特定资产的重建
Rails trigger rebuild of specific asset on change
由于 an issue in the angular-rails-templates gem, I've adopted one of the recommended workarounds in my repo here and here.
基本前提有效 - 当我修改任何 JS 模板时,它会触发删除缓存文件夹。
没有发生的是 app/assets/javascripts/templates.js.erb
处动态生成的 Angular 模板未在同一进程中重新编译。我仍然需要重新启动 rails 服务器才能看到更改。
如何在同一过程中重新编译该文件?
生成 Angular 模板缓存 (仅适用于 rails 服务器启动)
(function(){
'use strict';
angular.module('templates', []).run(['$templateCache', function($templateCache) {
<%
environment.context_class.instance_eval { include ActionView::Helpers::JavaScriptHelper }
app_root = File.expand_path('../../', __FILE__)
templates = File.join(app_root, %w{templates ** *.html.haml})
Dir.glob(templates).each do |f|
key = f.gsub(%r(^#{app_root}/templates/), '')
key.gsub!('.haml', '')
html = Tilt::HamlTemplate.new(f).render
template = html.squish
%>
$templateCache.put("<%= key %>", "<%= escape_javascript(template) %>");
<% end %>
}]);
}());
侦听任何 JS 模板的更改
require 'fileutils'
if Rails.env.development?
cache_path = Rails.root.join('tmp/cache/assets/development')
FileUtils.rm_rf(cache_path)
listener = Listen.to(Rails.root.join('app/assets/templates')) do |modified, added, removed|
puts 'CLEARING NG TEMPLATE CACHE'
# clearing cache
FileUtils.rm_rf(cache_path)
end
listener.start
end
Rails 默认缓存 FileStore 使用额外的内存存储来保存文件 I/O。从那里 Rails 即使没有缓存文件或目录,也会继续为您提供旧内容。
要完全清除缓存,您应该在同一服务器进程中调用以下语句。
Rails.cache.clear
因此您的侦听器代码应如下所示:
if Rails.env.development?
listener = Listen.to(Rails.root.join('app/assets/templates')) do |modified, added, removed|
puts 'CLEARING NG TEMPLATE CACHE'
Rails.cache.clear
end
listener.start
end
如果您经常更改这些模板,那么默认缓存可能会有很多 create/delete 文件循环。为避免这种情况,您可以尝试将其切换为纯内存缓存。将此行添加到您的 config/environments/development.rb
:
config.cache_store = :memory_store
由于 an issue in the angular-rails-templates gem, I've adopted one of the recommended workarounds in my repo here and here.
基本前提有效 - 当我修改任何 JS 模板时,它会触发删除缓存文件夹。
没有发生的是 app/assets/javascripts/templates.js.erb
处动态生成的 Angular 模板未在同一进程中重新编译。我仍然需要重新启动 rails 服务器才能看到更改。
如何在同一过程中重新编译该文件?
生成 Angular 模板缓存 (仅适用于 rails 服务器启动)
(function(){
'use strict';
angular.module('templates', []).run(['$templateCache', function($templateCache) {
<%
environment.context_class.instance_eval { include ActionView::Helpers::JavaScriptHelper }
app_root = File.expand_path('../../', __FILE__)
templates = File.join(app_root, %w{templates ** *.html.haml})
Dir.glob(templates).each do |f|
key = f.gsub(%r(^#{app_root}/templates/), '')
key.gsub!('.haml', '')
html = Tilt::HamlTemplate.new(f).render
template = html.squish
%>
$templateCache.put("<%= key %>", "<%= escape_javascript(template) %>");
<% end %>
}]);
}());
侦听任何 JS 模板的更改
require 'fileutils'
if Rails.env.development?
cache_path = Rails.root.join('tmp/cache/assets/development')
FileUtils.rm_rf(cache_path)
listener = Listen.to(Rails.root.join('app/assets/templates')) do |modified, added, removed|
puts 'CLEARING NG TEMPLATE CACHE'
# clearing cache
FileUtils.rm_rf(cache_path)
end
listener.start
end
Rails 默认缓存 FileStore 使用额外的内存存储来保存文件 I/O。从那里 Rails 即使没有缓存文件或目录,也会继续为您提供旧内容。
要完全清除缓存,您应该在同一服务器进程中调用以下语句。
Rails.cache.clear
因此您的侦听器代码应如下所示:
if Rails.env.development?
listener = Listen.to(Rails.root.join('app/assets/templates')) do |modified, added, removed|
puts 'CLEARING NG TEMPLATE CACHE'
Rails.cache.clear
end
listener.start
end
如果您经常更改这些模板,那么默认缓存可能会有很多 create/delete 文件循环。为避免这种情况,您可以尝试将其切换为纯内存缓存。将此行添加到您的 config/environments/development.rb
:
config.cache_store = :memory_store