From c5326682d6346c13b495ece9f3ee4e82f1b13a72 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 10 Oct 2019 07:10:23 +0200 Subject: [PATCH] DEV: improves speed of live reload css in core and plugins (#8161) This corrects live refresh of CSS when working on plugins (and core) in many cases live refresh was not working. --- lib/stylesheet/manager.rb | 10 ++++++- lib/stylesheet/watcher.rb | 63 +++++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 13 deletions(-) diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb index 1b9b8757b33..5f5365512ad 100644 --- a/lib/stylesheet/manager.rb +++ b/lib/stylesheet/manager.rb @@ -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) diff --git a/lib/stylesheet/watcher.rb b/lib/stylesheet/watcher.rb index 3ee968baebf..f59b53f6dc0 100644 --- a/lib/stylesheet/watcher.rb +++ b/lib/stylesheet/watcher.rb @@ -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