DEV: improves speed of live reload css in core and plugins ()

This corrects live refresh of CSS when working on plugins (and core) in many cases live refresh was not working.
This commit is contained in:
Joffrey JAFFEUX 2019-10-10 07:10:23 +02:00 committed by Sam
parent 1be8c737e6
commit c5326682d6
2 changed files with 60 additions and 13 deletions
lib/stylesheet

@ -19,7 +19,15 @@ class Stylesheet::Manager
end
def self.clear_theme_cache!
cache.hash.keys.select { |k| k =~ /theme/ }.each { |k|cache.delete(k) }
cache.hash.keys.select { |k| k =~ /theme/ }.each { |k| cache.delete(k) }
end
def self.clear_core_cache!(targets)
cache.hash.keys.select { |k| k =~ /#{targets.join('|')}/ }.each { |k| cache.delete(k) }
end
def self.clear_plugin_cache!(plugin)
cache.hash.keys.select { |k| k =~ /#{plugin}/ }.each { |k| cache.delete(k) }
end
def self.stylesheet_data(target = :desktop, theme_ids = :missing)

@ -56,10 +56,37 @@ module Stylesheet
@paths.each do |watch|
Thread.new do
begin
plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file|
File.symlink?(file) ? File.readlink(file) : file
end.compact
listener = Listen.to("#{root}/#{watch}", listener_opts) do |modified, added, _|
paths = [modified, added].flatten
paths.compact!
paths.map! { |long| long[(root.length + 1)..-1] }
paths.map! do |long|
plugin_name = nil
plugins_paths.each do |plugin_path|
if long.include?(plugin_path)
plugin_name = File.basename(plugin_path)
break
end
end
target = nil
if !plugin_name
target_match = long.match(/admin|desktop|mobile/)
if target_match&.length
target = target_match[0]
end
end
{
basename: File.basename(long),
target: target,
plugin_name: plugin_name
}
end
process_change(paths)
end
rescue => e
@ -71,29 +98,41 @@ module Stylesheet
end
end
def core_assets_refresh(target)
targets = target ? [target] : ["desktop", "mobile", "admin"]
Stylesheet::Manager.clear_core_cache!(targets)
message = targets.map! do |name|
Stylesheet::Manager.stylesheet_data(name.to_sym, Stylesheet::Watcher.theme_id)
end.flatten!
MessageBus.publish '/file-change', message
end
def plugin_assets_refresh(plugin_name)
Stylesheet::Manager.clear_plugin_cache!(plugin_name)
message = Stylesheet::Manager.stylesheet_data(plugin_name.to_sym, Stylesheet::Watcher.theme_id)
MessageBus.publish '/file-change', message
end
def worker_loop
@queue.pop
path = @queue.pop
while @queue.length > 0
@queue.pop
end
Stylesheet::Manager.cache.clear
targets = ["desktop", "mobile", "admin"]
targets.append(*Discourse.find_plugin_css_assets(mobile_view: true, desktop_view: true))
message = targets.map do |name|
Stylesheet::Manager.stylesheet_data(name.to_sym, Stylesheet::Watcher.theme_id)
end.flatten
MessageBus.publish '/file-change', message
if path[:plugin_name]
plugin_assets_refresh(path[:plugin_name])
else
core_assets_refresh(path[:target])
end
end
def process_change(paths)
paths.each do |path|
if path =~ /\.(css|scss)$/
if path[:basename] =~ /\.(css|scss)$/
@queue.push path
end
end
end
end
end