DEV: Compile core and plugin stylesheets independently of themes (#13638)

Take 2 of https://github.com/discourse/discourse/pull/13466. 

Fixes a few issues with the original PR: 

- color definition stylesheet target now includes the theme id, to avoid themes set to use the default color scheme loading the same stylesheet 
- changes the internal cache key for color definition stylesheet to reset the pre-existing cache
This commit is contained in:
Penar Musaraj
2021-07-06 13:11:10 -04:00
committed by GitHub
parent da03a3f5d6
commit 95b5794331
10 changed files with 167 additions and 169 deletions

View File

@ -38,58 +38,58 @@ class Stylesheet::Manager
def self.color_scheme_cache_key(color_scheme, theme_id = nil)
color_scheme_name = Slug.for(color_scheme.name) + color_scheme&.id.to_s
theme_string = theme_id ? "_theme#{theme_id}" : ""
"#{COLOR_SCHEME_STYLESHEET}_#{color_scheme_name}#{theme_string}_#{Discourse.current_hostname}"
"#{COLOR_SCHEME_STYLESHEET}_#{color_scheme_name}_#{theme_string}_#{Discourse.current_hostname}"
end
def self.precompile_css
targets = [:desktop, :mobile, :desktop_rtl, :mobile_rtl, :admin, :wizard]
targets += Discourse.find_plugin_css_assets(include_disabled: true, mobile_view: true, desktop_view: true)
targets.each do |target|
$stderr.puts "precompile target: #{target}"
Stylesheet::Manager::Builder.new(target: target, manager: nil).compile(force: true)
end
end
def self.precompile_theme_css
themes = Theme.where('user_selectable OR id = ?', SiteSetting.default_theme_id).pluck(:id, :color_scheme_id)
themes << nil
color_schemes = ColorScheme.where(user_selectable: true).to_a
color_schemes << ColorScheme.find_by(id: SiteSetting.default_dark_mode_color_scheme_id)
color_schemes << ColorScheme.base
color_schemes = color_schemes.compact.uniq
targets = [:desktop, :mobile, :desktop_rtl, :mobile_rtl, :desktop_theme, :mobile_theme, :admin, :wizard]
targets += Discourse.find_plugin_css_assets(include_disabled: true, mobile_view: true, desktop_view: true)
targets = [:desktop_theme, :mobile_theme]
compiled = Set.new
themes.each do |id, color_scheme_id|
theme_id = id || SiteSetting.default_theme_id
themes.each do |theme_id, color_scheme_id|
manager = self.new(theme_id: theme_id)
targets.each do |target|
if target =~ THEME_REGEX
next if theme_id == -1
next if theme_id == -1
scss_checker = ScssChecker.new(target, manager.theme_ids)
scss_checker = ScssChecker.new(target, manager.theme_ids)
manager.load_themes(manager.theme_ids).each do |theme|
next if compiled.include?("#{target}_#{theme.id}")
manager.load_themes(manager.theme_ids).each do |theme|
next if compiled.include?("#{target}_#{theme.id}")
builder = Stylesheet::Manager::Builder.new(
target: target, theme: theme, manager: manager
)
next if theme.component && !scss_checker.has_scss(theme.id)
$stderr.puts "precompile target: #{target} #{theme.name}"
builder.compile(force: true)
compiled << "#{target}_#{theme.id}"
end
else
theme = manager.get_theme(theme_id)
$stderr.puts "precompile target: #{target} #{theme&.name}"
Stylesheet::Manager::Builder.new(
builder = Stylesheet::Manager::Builder.new(
target: target, theme: theme, manager: manager
).compile(force: true)
)
next if theme.component && !scss_checker.has_scss(theme.id)
$stderr.puts "precompile target: #{target} #{theme.name}"
builder.compile(force: true)
compiled << "#{target}_#{theme.id}"
end
end
theme_color_scheme = ColorScheme.find_by_id(color_scheme_id) || ColorScheme.base
theme_color_scheme = ColorScheme.find_by_id(color_scheme_id)
theme = manager.get_theme(theme_id)
[theme_color_scheme, *color_schemes].uniq.each do |scheme|
$stderr.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} (#{scheme.name}) #{theme&.name}"
[theme_color_scheme, *color_schemes].compact.uniq.each do |scheme|
$stderr.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{theme.name} (#{scheme.name})"
Stylesheet::Manager::Builder.new(
target: COLOR_SCHEME_STYLESHEET,
@ -200,15 +200,19 @@ class Stylesheet::Manager
def stylesheet_details(target = :desktop, media = 'all')
target = target.to_sym
current_hostname = Discourse.current_hostname
is_theme_target = !!(target.to_s =~ THEME_REGEX)
array_cache_key = is_theme_target ?
"array_themes_#{@theme_ids.join(",")}_#{target}_#{current_hostname}" :
"array_#{target}_#{current_hostname}"
array_cache_key = "array_themes_#{@theme_ids.join(",")}_#{target}_#{current_hostname}"
stylesheets = cache[array_cache_key]
return stylesheets if stylesheets.present?
@@lock.synchronize do
stylesheets = []
stale_theme_ids = []
theme_ids = target.to_s =~ THEME_REGEX ? @theme_ids : [@theme_id]
theme_ids = is_theme_target ? @theme_ids : [nil]
theme_ids.each do |theme_id|
cache_key = "path_#{target}_#{theme_id}_#{current_hostname}"
@ -226,25 +230,36 @@ class Stylesheet::Manager
scss_checker = ScssChecker.new(target, stale_theme_ids)
themes = @theme_id.blank? ? [nil] : load_themes(stale_theme_ids)
if is_theme_target
themes = load_themes(stale_theme_ids)
themes.each do |theme|
theme_id = theme&.id
data = { target: target, theme_id: theme_id }
builder = Builder.new(target: target, theme: theme, manager: self)
is_theme = builder.is_theme?
has_theme = builder.theme.present?
themes.each do |theme|
theme_id = theme&.id
data = { target: target, theme_id: theme_id }
builder = Builder.new(target: target, theme: theme, manager: self)
is_theme = builder.is_theme?
has_theme = builder.theme.present?
if is_theme && !has_theme
next
else
next if is_theme && builder.theme&.component && !scss_checker.has_scss(theme_id)
builder.compile unless File.exists?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
cache.defer_set("path_#{target}_#{theme_id}_#{current_hostname}", href)
if is_theme && !has_theme
next
else
next if is_theme && builder.theme&.component && !scss_checker.has_scss(theme_id)
builder.compile unless File.exists?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
cache.defer_set("path_#{target}_#{theme_id}_#{current_hostname}", href)
end
data[:new_href] = href
stylesheets << data
end
else
builder = Builder.new(target: target, manager: self)
builder.compile unless File.exists?(builder.stylesheet_fullpath)
href = builder.stylesheet_path(current_hostname)
data[:new_href] = href
cache.defer_set("path_#{target}__#{current_hostname}", href)
data = { target: target, new_href: href }
stylesheets << data
end