A platform for community discussion. Free, open, simple.
FIX: Improve SCSS handling in components (#11963)
Penar Musaraj12ffba771ccd
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rbindex 38701933ea8..540387a5e36 100644--- a/app/models/theme_field.rb+++ b/app/models/theme_field.rb@@ -354,6 +354,18 @@ class ThemeField < ActiveRecord::Base)end+ def compiled_css+ css, _source_map = begin+ compile_scss+ rescue SassC::SyntaxError => e+ # We don't want to raise a blocking error here+ # admin theme editor or discourse_theme CLI will show it nonetheless+ Rails.logger.error "SCSS compilation error: #{e.message}"+ ["", nil]+ end+ css+ end+def ensure_scss_compiles!result = ["failed"]begindiff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rbindex 20989916522..6f241f77d55 100644--- a/lib/stylesheet/importer.rb+++ b/lib/stylesheet/importer.rb@@ -141,9 +141,15 @@ module Stylesheetif resolved_idstheme = Theme.find_by_id(theme_id)contents << theme&.scss_variables.to_s- Theme.list_baked_fields(resolved_ids, :common, :color_definitions).each do |row|- contents << "// Color definitions from #{theme.name}\n\n"- contents << row.value+ Theme.list_baked_fields(resolved_ids, :common, :color_definitions).each do |field|+ contents << "// Color definitions from #{field.theme.name}\n\n"++ if field.theme_id == theme.id+ contents << field.value+ else+ contents << field.compiled_css+ end+ contents << "\n\n"endendcontents@@ -198,13 +204,7 @@ module Stylesheetif field.theme_id == theme.idcontents << valueelse- css, source_map = begin- field.compile_scss- rescue SassC::SyntaxError => e- raise Discourse::ScssError, e.message- end-- contents << css+ contents << field.compiled_cssendenddiff --git a/spec/components/stylesheet/importer_spec.rb b/spec/components/stylesheet/importer_spec.rbindex 36ee1181358..acb66072c27 100644--- a/spec/components/stylesheet/importer_spec.rb+++ b/spec/components/stylesheet/importer_spec.rb@@ -64,8 +64,8 @@ describe Stylesheet::Importer doendcontext "#import_color_definitions" do- let(:scss) { ":root { --custom-color: green}" }- let(:scss_child) { ":root { --custom-color: red}" }+ let(:scss) { ":root{--custom-color: green}" }+ let(:scss_child) { ":root{--custom-color: red}" }let(:theme) doFabricate(:theme).tap do |t|@@ -74,7 +74,7 @@ describe Stylesheet::Importer doendend- let(:child) { Fabricate(:theme, component: true).tap { |t|+ let(:child) { Fabricate(:theme, component: true, name: "Child Theme").tap { |t|t.set_field(target: :common, name: "color_definitions", value: scss_child)t.save!}}@@ -90,6 +90,7 @@ describe Stylesheet::Importer dostyles = Stylesheet::Importer.import_color_definitions(theme.id)expect(styles).to include(scss_child)+ expect(styles).to include("Color definitions from Child Theme")endit "should include default theme color definitions" dodiff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rbindex eca11336a00..3a65d8ab265 100644--- a/spec/models/theme_spec.rb+++ b/spec/models/theme_spec.rb@@ -807,6 +807,13 @@ HTMLexpect(parent_css).to include("body{background:green}")end+ it "does not fail if child theme has SCSS errors" do+ child_theme.set_field(target: :common, name: :scss, value: 'p {color: $missing_var;}')+ child_theme.save!++ parent_css, _parent_map = compiler+ expect(parent_css).to include("sourceMappingURL")+ endenddescribe "scss_variables" do