Skip to content

A platform for community discussion. Free, open, simple.

DEV: Compile and serve rtl versions of theme CSS (#32881)

David Taylorb4990ccb7116
diff --git a/app/views/common/_discourse_stylesheet.html.erb b/app/views/common/_discourse_stylesheet.html.erb
index 8f251f6071c..9c200c83d5d 100644
--- a/app/views/common/_discourse_stylesheet.html.erb
+++ b/app/views/common/_discourse_stylesheet.html.erb
@@ -37,11 +37,11 @@
<% end %>
<%- if theme_id.present? %>
- <%= discourse_stylesheet_link_tag(:common_theme) %>
+ <%= discourse_stylesheet_link_tag(:common_theme, supports_rtl: true) %>
<% if SiteSetting.viewport_based_mobile_mode %>
- <%= discourse_stylesheet_link_tag(:mobile_theme, media: "(max-width: 39.99999rem)") %>
- <%= discourse_stylesheet_link_tag(:desktop_theme, media: "(min-width: 40rem)") %>
+ <%= discourse_stylesheet_link_tag(:mobile_theme, media: "(max-width: 39.99999rem)", supports_rtl: true) %>
+ <%= discourse_stylesheet_link_tag(:desktop_theme, media: "(min-width: 40rem)", supports_rtl: true) %>
<% else %>
- <%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_theme : :desktop_theme) %>
+ <%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_theme : :desktop_theme, supports_rtl: true) %>
<% end %>
<%- end %>
diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index 2437c49c599..88d269ce24c 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -17,7 +17,7 @@ class Stylesheet::Manager
private_constant :CACHE_PATH
MANIFEST_DIR = "#{Rails.root}/tmp/cache/assets/#{Rails.env}"
- THEME_REGEX = /_theme\z/
+ THEME_REGEX = /_theme(_rtl)?\z/
COLOR_SCHEME_STYLESHEET = "color_definitions"
@@lock = Mutex.new
@@ -87,6 +87,7 @@ class Stylesheet::Manager
color_schemes = color_schemes.compact.uniq
targets = %i[common_theme desktop_theme mobile_theme]
+ targets += targets.map { |t| :"#{t}_rtl" }
compiled = Set.new
themes.each do |theme_id, color_scheme_id|
@@ -95,7 +96,7 @@ class Stylesheet::Manager
targets.each do |target|
next if theme_id == -1
- scss_checker = ScssChecker.new(target, manager.theme_ids)
+ scss_checker = ScssChecker.new(target.to_s.delete_suffix("_rtl"), manager.theme_ids)
manager
.load_themes(manager.theme_ids)
diff --git a/lib/stylesheet/manager/builder.rb b/lib/stylesheet/manager/builder.rb
index 54aba89dff6..b1515dbc70c 100644
--- a/lib/stylesheet/manager/builder.rb
+++ b/lib/stylesheet/manager/builder.rb
@@ -51,7 +51,7 @@ class Stylesheet::Manager::Builder
strict_deprecations: %i[desktop mobile admin wizard].include?(@target),
)
rescue SassC::SyntaxError, SassC::NotRenderedError, DiscourseJsProcessor::TranspileError => e
- if Stylesheet::Importer::THEME_TARGETS.include?(@target.to_s)
+ if Stylesheet::Manager::THEME_REGEX.match?(@target.to_s)
# no special errors for theme, handled in theme editor
["/* SCSS compilation error: #{e.message} */", nil]
elsif @target.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET && Rails.env.production?
@@ -177,8 +177,9 @@ class Stylesheet::Manager::Builder
end
def scss_digest
- if %i[common_theme mobile_theme desktop_theme].include?(@target)
- resolve_baked_field(@target.to_s.sub("_theme", ""), :scss)
+ base_target = @target.to_s.delete_suffix("_rtl").to_sym
+ if %i[common_theme mobile_theme desktop_theme].include?(base_target)
+ resolve_baked_field(base_target.to_s.delete_suffix("_theme"), :scss)
elsif @target == :embedded_theme
resolve_baked_field(:common, :embedded_scss)
else
diff --git a/spec/lib/stylesheet/manager_spec.rb b/spec/lib/stylesheet/manager_spec.rb
index bf0fd11fad5..7c55dbebcb9 100644
--- a/spec/lib/stylesheet/manager_spec.rb
+++ b/spec/lib/stylesheet/manager_spec.rb
@@ -914,7 +914,16 @@ RSpec.describe Stylesheet::Manager do
%w[common desktop mobile admin wizard common_rtl desktop_rtl mobile_rtl admin_rtl wizard_rtl]
end
- let(:theme_targets) { %i[common_theme desktop_theme mobile_theme] }
+ let(:theme_targets) do
+ %i[
+ common_theme
+ desktop_theme
+ mobile_theme
+ common_theme_rtl
+ desktop_theme_rtl
+ mobile_theme_rtl
+ ]
+ end
before do
STDERR.stubs(:write)
@@ -959,8 +968,8 @@ RSpec.describe Stylesheet::Manager do
output = capture_output(:stderr) { Stylesheet::Manager.precompile_theme_css }
# Ensure we force compile each theme only once
- expect(output.scan(/#{child_theme_with_css.name}/).length).to eq(1)
- expect(StylesheetCache.count).to eq(33) # (1 theme with css) + 32 color schemes (2 themes * 8 color schemes (7 defaults + 1 theme scheme) * 2 (light and dark mode per scheme))
+ expect(output.scan(/#{child_theme_with_css.name}/).length).to eq(2) # ltr/rtl
+ expect(StylesheetCache.count).to eq(34) # (1 theme with rtl/ltr) + 32 color schemes (2 themes * 8 color schemes (7 defaults + 1 theme scheme) * 2 (light and dark mode per scheme))
end
it "generates precompiled CSS - core and themes" do
@@ -968,9 +977,9 @@ RSpec.describe Stylesheet::Manager do
Stylesheet::Manager.precompile_theme_css
results = StylesheetCache.pluck(:target)
- expect(results.size).to eq(43) # 10 core targets + 1 theme + 32 color schemes (light and dark mode per scheme)
+ expect(results.size).to eq(44) # 10 core targets + 2 theme (ltr/rtl) + 32 color schemes (light and dark mode per scheme)
- expect(results.count { |target| target =~ /^common_theme_/ }).to eq(1)
+ expect(results.count { |target| target =~ /^common_theme_/ }).to eq(2) # ltr/rtl
end
it "correctly generates precompiled CSS - core and themes and no default theme" do
@@ -980,7 +989,7 @@ RSpec.describe Stylesheet::Manager do
Stylesheet::Manager.precompile_theme_css
results = StylesheetCache.pluck(:target)
- expect(results.size).to eq(43) # 10 core targets + 1 theme + 32 color schemes (light and dark mode per scheme)
+ expect(results.size).to eq(44) # 10 core targets + theme rtl/ltr + 32 color schemes (light and dark mode per scheme)
expect(results).to include("color_definitions_#{scheme1.name}_#{scheme1.id}_#{user_theme.id}")
expect(results).to include(