FIX: Child themes being precompiled multiple times.

This commit is contained in:
Alan Guo Xiang Tan 2021-06-30 16:06:13 +08:00
parent 5c43f9a3a3
commit 6986b36985
2 changed files with 26 additions and 25 deletions

View File

@ -46,54 +46,49 @@ class Stylesheet::Manager
targets += Discourse.find_plugin_css_assets(include_disabled: true, mobile_view: true, desktop_view: true) targets += Discourse.find_plugin_css_assets(include_disabled: true, mobile_view: true, desktop_view: true)
targets.each do |target| targets.each do |target|
STDERR.puts "precompile target: #{target}" $stderr.puts "precompile target: #{target}"
Stylesheet::Manager::Builder.new(target: target, manager: nil).compile(force: true) Stylesheet::Manager::Builder.new(target: target, manager: nil).compile(force: true)
end end
end end
def self.precompile_theme_css def self.precompile_theme_css
themes = Theme.where('user_selectable OR id = ?', SiteSetting.default_theme_id).pluck(:id, :name, :color_scheme_id) 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.where(user_selectable: true).to_a
color_schemes << ColorScheme.find_by(id: SiteSetting.default_dark_mode_color_scheme_id) color_schemes << ColorScheme.find_by(id: SiteSetting.default_dark_mode_color_scheme_id)
color_schemes << ColorScheme.base
color_schemes = color_schemes.compact.uniq color_schemes = color_schemes.compact.uniq
targets = [:desktop_theme, :mobile_theme] targets = [:desktop_theme, :mobile_theme]
compiled = Set.new
themes.each do |id, name, color_scheme_id| themes.each do |theme_id, color_scheme_id|
theme_id = id || SiteSetting.default_theme_id
manager = self.new(theme_id: theme_id) manager = self.new(theme_id: theme_id)
targets.each do |target| 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| manager.load_themes(manager.theme_ids).each do |theme|
next if compiled.include?("#{target}_#{theme.id}")
builder = Stylesheet::Manager::Builder.new( builder = Stylesheet::Manager::Builder.new(
target: target, theme: theme, manager: manager target: target, theme: theme, manager: manager
) )
STDERR.puts "precompile target: #{target} #{builder.theme.name}" $stderr.puts "precompile target: #{target} #{theme.name}"
next if theme.component && !scss_checker.has_scss(theme.id) next if theme.component && !scss_checker.has_scss(theme.id)
builder.compile(force: true) builder.compile(force: true)
end compiled << "#{target}_#{theme.id}"
else
STDERR.puts "precompile target: #{target} #{name}"
Stylesheet::Manager::Builder.new(
target: target, theme: manager.get_theme(theme_id), manager: manager
).compile(force: true)
end end
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_color_scheme, *color_schemes].uniq.each do |scheme| [theme_color_scheme, *color_schemes].compact.uniq.each do |scheme|
STDERR.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{name} (#{scheme.name})" $stderr.puts "precompile target: #{COLOR_SCHEME_STYLESHEET} #{name} (#{scheme.name})"
Stylesheet::Manager::Builder.new( Stylesheet::Manager::Builder.new(
target: COLOR_SCHEME_STYLESHEET, target: COLOR_SCHEME_STYLESHEET,

View File

@ -654,6 +654,7 @@ describe Stylesheet::Manager do
t.save! t.save!
user_theme.add_relative_theme!(:child, t) user_theme.add_relative_theme!(:child, t)
default_theme.add_relative_theme!(:child, t)
end end
default_theme.set_default! default_theme.set_default!
@ -668,7 +669,12 @@ describe Stylesheet::Manager do
StylesheetCache.destroy_all StylesheetCache.destroy_all
# only themes # only themes
output = capture_output(:stderr) do
Stylesheet::Manager.precompile_theme_css Stylesheet::Manager.precompile_theme_css
end
# Ensure we force compile each theme only once
expect(output.scan(/#{child_theme_with_css.name}/).length).to eq(2)
results = StylesheetCache.pluck(:target) results = StylesheetCache.pluck(:target)
expect(results.size).to eq(12) # (2 themes * 2 targets) + (one child theme * 2 targets) + 6 color schemes (2 custom, 4 base schemes) expect(results.size).to eq(12) # (2 themes * 2 targets) + (one child theme * 2 targets) + 6 color schemes (2 custom, 4 base schemes)