A platform for community discussion. Free, open, simple.
DEV: Allow stylesheet entrypoints to use `@use` (#31905)
David Taylorb1924c352487
diff --git a/app/models/theme.rb b/app/models/theme.rbindex a6a7df1b977..03a6b6d95ed 100644--- a/app/models/theme.rb+++ b/app/models/theme.rb@@ -836,11 +836,27 @@ class Theme < ActiveRecord::Baseenddef with_scss_load_paths- return yield([]) if self.extra_scss_fields.empty?-ThemeStore::ZipExporter.new(self)- .with_export_dir(extra_scss_only: true) { |dir| yield ["#{dir}/stylesheets"] }+ .with_export_dir(scss_only: true) do |dir|+ FileUtils.mkdir_p("#{dir}/_entry_loadpath/theme-entrypoint")++ entrypoints = {+ "common/common.scss" => "common.scss",+ "common/embedded.scss" => "embedded.scss",+ "common/color_definitions.scss" => "color_definitions.scss",+ "desktop/desktop.scss" => "desktop.scss",+ "mobile/mobile.scss" => "mobile.scss",+ }++ entrypoints.each do |source, destination|+ source_path = "#{dir}/#{source}"+ destination_path = "#{dir}/_entry_loadpath/theme-entrypoint/#{destination}"+ FileUtils.mv(source_path, destination_path) if File.exist?(source_path)+ end++ yield ["#{dir}/_entry_loadpath", "#{dir}/stylesheets"]+ endenddef scss_variablesdiff --git a/app/models/theme_field.rb b/app/models/theme_field.rbindex 0659b447b90..acd63339a67 100644--- a/app/models/theme_field.rb+++ b/app/models/theme_field.rb@@ -480,12 +480,28 @@ class ThemeField < ActiveRecord::Base# requests.end+ def scss_entrypoint_name+ if name == "scss"+ self.target_name+ elsif target_name == "common" && name == "color_definitions"+ "color_definitions"+ elsif target_name == "common" && name == "embedded_scss"+ "embedded"+ else+ raise "Unknown entrypoint for #{target_name}/#{name}"+ end+ end+def compile_scss(prepended_scss = nil)prepended_scss ||= Stylesheet::Importer.new({}).prepended_scssself.theme.with_scss_load_paths do |load_paths|Stylesheet::Compiler.compile(- "#{prepended_scss} #{self.theme.scss_variables} #{self.value}",+ <<~SCSS,+ #{prepended_scss}+ #{self.theme.scss_variables}+ @import \"theme-entrypoint/#{scss_entrypoint_name}\";+ SCSS"#{Theme.targets[self.target_id]}.scss",theme: self.theme,load_paths: load_paths,@@ -501,7 +517,7 @@ class ThemeField < ActiveRecord::Base# We don't want to raise a blocking error here# admin theme editor or discourse_theme CLI will show it nonethelessRails.logger.error "SCSS compilation error: #{e.message}"- ["", nil]+ ["/* SCSS compilation error: #{e.message} */", nil]endcssenddiff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex f1669754382..6d83c58774c 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -785,7 +785,7 @@ class Plugin::Instancejs = "(function(){#{js}})();" if js.present?result = []- result << [css, "css"] if css.present?+ result << [css, "scss"] if css.present?result << [js, "js"] if js.present?result.map do |asset, extension|diff --git a/lib/stylesheet/compiler.rb b/lib/stylesheet/compiler.rbindex 4623525441b..8b494b2d7c3 100644--- a/lib/stylesheet/compiler.rb+++ b/lib/stylesheet/compiler.rb@@ -9,22 +9,25 @@ module Stylesheetdef self.compile_asset(asset, options = {})importer = Importer.new(options)file = importer.prepended_scss+ filename = "_#{asset}_entrypoint.scss"if Importer::THEME_TARGETS.include?(asset.to_s)filename = "theme_#{options[:theme_id]}.scss"file += options[:theme_variables].to_sfile += importer.theme_import(asset)- elsif plugin_assets = Importer.plugin_assets[asset.to_s]- filename = "#{asset}.scss"+ elsif plugin_asset_info = Importer.plugin_assets[asset.to_s]options[:load_paths] = [] if options[:load_paths].nil?++ plugin_assets = plugin_asset_info[:stylesheets]+ plugin_path = plugin_asset_info[:plugin_path]+ options[:load_paths] << plugin_path+plugin_assets.each do |src|- file += File.read srcoptions[:load_paths] << File.expand_path(File.dirname(src))+ file += "@import \"#{src}\";\n"end- else- filename = "#{asset}.scss"- path = "#{ASSET_ROOT}/#{filename}"- file += File.read path+ else # Core asset+ file += "@import \"#{asset}\";\n"case asset.to_swhen "embed", "publish"diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rbindex 601bfbe12d8..07410faf608 100644--- a/lib/stylesheet/importer.rb+++ b/lib/stylesheet/importer.rb@@ -27,9 +27,12 @@ module Stylesheetend)- plugin_assets[asset_name] = stylesheets[- plugin_directory_name- ] if plugin_directory_name.present?+ if plugin_directory_name.present?+ plugin_assets[asset_name] = {+ plugin_path: plugin.path,+ stylesheets: stylesheets[plugin_directory_name],+ }+ endendendend@@ -112,21 +115,11 @@ module Stylesheetresolved_ids = Theme.transform_ids(theme_id)if resolved_ids- theme = Theme.find_by_id(theme_id)-- contents << "\n\n// Theme SCSS variables\n\n"- contents << theme&.scss_variables.to_s.split(";").join(";\n") + ";\n\n"- contents << "\n\n"Theme.list_baked_fields(resolved_ids, :common, :color_definitions).each do |field|contents << "\n\n// Color definitions from #{field.theme.name}\n\n"-- if field.theme_id == theme.id- contents << field.value- else- contents << field.compiled_css(prepended_scss)- end+ contents << field.compiled_css(prepended_scss)contents << "\n\n"endend@@ -198,16 +191,7 @@ module Stylesheetfields = theme.list_baked_fields(target, attr)fields.map do |field|- value = field.value- if value.present?- contents << <<~SCSS- // Theme: #{field.theme.name}- // Target: #{field.target_name} #{field.name}- // Last Edited: #{field.updated_at}- SCSS-- contents << value- end+ contents << "@import \"theme-entrypoint/#{field.scss_entrypoint_name}\";\n"endcontentsenddiff --git a/lib/stylesheet/manager/builder.rb b/lib/stylesheet/manager/builder.rbindex f81be1cf1a0..ce778b68187 100644--- a/lib/stylesheet/manager/builder.rb+++ b/lib/stylesheet/manager/builder.rb@@ -52,7 +52,7 @@ class Stylesheet::Manager::Builderrescue SassC::SyntaxError, SassC::NotRenderedError, DiscourseJsProcessor::TranspileError => eif Stylesheet::Importer::THEME_TARGETS.include?(@target.to_s)# no special errors for theme, handled in theme editor- ["", nil]+ ["/* SCSS compilation error: #{e.message} */", nil]elsif @target.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET && Rails.env.production?# log error but do not crash for errors in color definitions SCSSRails.logger.error "SCSS compilation error: #{e.message}"diff --git a/lib/theme_store/zip_exporter.rb b/lib/theme_store/zip_exporter.rbindex f74c4b2cd7c..e9826dffdb6 100644--- a/lib/theme_store/zip_exporter.rb+++ b/lib/theme_store/zip_exporter.rb@@ -35,12 +35,12 @@ class ThemeStore::ZipExporterprivate- def export_to_folder(extra_scss_only: false)+ def export_to_folder(scss_only: false)destination_folder = File.join(@temp_folder, @export_name)FileUtils.mkdir_p(destination_folder)@theme.theme_fields.each do |field|- next if extra_scss_only && !field.extra_scss_field?+ next if scss_only && !(field.extra_scss_field? || field.basic_scss_field?)next unless path = field.file_path# Belt and braces approach here. All the user input should already be@@ -64,7 +64,7 @@ class ThemeStore::ZipExporterFile.write(path, content)end- if !extra_scss_only+ if !scss_onlyFile.write(File.join(destination_folder, "about.json"),JSON.pretty_generate(@theme.generate_metadata_hash),diff --git a/spec/lib/stylesheet/compiler_spec.rb b/spec/lib/stylesheet/compiler_spec.rbindex 1ddc16c31dc..aeb517404f9 100644--- a/spec/lib/stylesheet/compiler_spec.rb+++ b/spec/lib/stylesheet/compiler_spec.rb@@ -42,16 +42,18 @@ RSpec.describe Stylesheet::Compiler dotype_id: ThemeField.types[:scss],)end- before { stylesheet_theme_field.save! }it "theme stylesheet should be able to access theme asset variables" do- css, _map =- Stylesheet::Compiler.compile_asset(- "desktop_theme",- theme_id: theme.id,- theme_variables: theme.scss_variables,- )- expect(css).to include(upload.url)+ theme.reload.with_scss_load_paths do |load_paths|+ css, _map =+ Stylesheet::Compiler.compile_asset(+ "desktop_theme",+ theme_id: theme.id,+ theme_variables: theme.scss_variables,+ load_paths: load_paths,+ )+ expect(css).to include(upload.url)+ endendcontext "with a plugin" dodiff --git a/spec/lib/stylesheet/importer_spec.rb b/spec/lib/stylesheet/importer_spec.rbindex 53a0818c7b6..c4dcf1c451f 100644--- a/spec/lib/stylesheet/importer_spec.rb+++ b/spec/lib/stylesheet/importer_spec.rb@@ -48,14 +48,15 @@ RSpec.describe Stylesheet::Importer doenddescribe "#import_color_definitions" do- let(:scss) { ":root{--custom-color: green}" }+ let(:input_scss) { ':root{--custom-color: green;--core-color: #{$primary}}' }+ let(:output_scss) { ":root{--custom-color: green;--core-color: #222}" }let(:scss_child) do"$navy: #000080; :root{--custom-color: red; --custom-color-rgb: \#{hexToRGB($navy)}}"endlet(:theme) doFabricate(:theme).tap do |t|- t.set_field(target: :common, name: "color_definitions", value: scss)+ t.set_field(target: :common, name: "color_definitions", value: input_scss)t.save!endend@@ -69,7 +70,7 @@ RSpec.describe Stylesheet::Importer doit "should include color definitions in the theme" dostyles = Stylesheet::Importer.new({ theme_id: theme.id }).import_color_definitions- expect(styles).to include(scss)+ expect(styles).to include(output_scss)endit "should include color definitions from components" do@@ -85,7 +86,7 @@ RSpec.describe Stylesheet::Importer doit "should include default theme color definitions" doSiteSetting.default_theme_id = theme.idstyles = Stylesheet::Importer.new({}).import_color_definitions- expect(styles).to include(scss)+ expect(styles).to include(output_scss)endenddiff --git a/spec/lib/stylesheet/manager_spec.rb b/spec/lib/stylesheet/manager_spec.rbindex d17f0db7ce7..38b21bb1584 100644--- a/spec/lib/stylesheet/manager_spec.rb+++ b/spec/lib/stylesheet/manager_spec.rb@@ -869,12 +869,8 @@ RSpec.describe Stylesheet::Manager domanager: manager,)- expect { stylesheet.compile }.to raise_error(Discourse::ScssError)-- Rails.env.stubs(:production?).returns(true)- expect(stylesheet.compile).to include(- "/* SCSS compilation error: Error: Undefined variable",- )+ expect(stylesheet.compile).to include("--primary:") # core vals preserved+ expect(File.read(stylesheet.source_map_fullpath)).to include("/* SCSS compilation error:")endit "child theme SCSS includes the default theme's color scheme variables" dodiff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rbindex 3083eb698d1..196298ec065 100644--- a/spec/models/theme_field_spec.rb+++ b/spec/models/theme_field_spec.rb@@ -36,19 +36,19 @@ RSpec.describe ThemeField doendit "adds an error when optimized image links are included" do- theme_field = ThemeField.create!(theme_id: 1, target_id: 0, name: "body_tag", value: <<~HTML)+ theme_field = theme.set_field(target: :common, name: :body_tag, value: <<~HTML)<img src="http://mysite.invalid/uploads/default/optimized/1X/6d749a141f513f88f167e750e528515002043da1_2_1282x1000.png"/>HTML- theme_field.ensure_baked!- expect(theme_field.error).to include(I18n.t("themes.errors.optimized_link"))+ theme.save!+ expect(theme_field.reload.error).to include(I18n.t("themes.errors.optimized_link"))- theme_field = ThemeField.create!(theme_id: 1, target_id: 0, name: "scss", value: <<~SCSS)+ theme_field = theme.set_field(target: :common, name: :scss, value: <<~SCSS)body {background: url(http://mysite.invalid/uploads/default/optimized/1X/6d749a141f513f88f167e750e528515002043da1_2_1282x1000.png);}SCSS- theme_field.ensure_baked!- expect(theme_field.error).to include(I18n.t("themes.errors.optimized_link"))+ theme.save!+ expect(theme_field.reload.error).to include(I18n.t("themes.errors.optimized_link"))theme_field.update(value: <<~SCSS)body {@@ -158,19 +158,17 @@ HTMLit "correctly generates errors for transpiled css" docss = "body {"- field = ThemeField.create!(theme_id: 1, target_id: 0, name: "scss", value: css)- field.ensure_baked!- expect(field.error).not_to eq(nil)+ field = theme.set_field(target: :common, name: :scss, value: css)+ theme.save!+ expect(field.reload.error).to include('Error: expected "}"')- field.value = "@import 'missingfile';"- field.save!- field.ensure_baked!- expect(field.error).to include("Error: Can't find stylesheet to import.")+ theme.set_field(target: :common, name: :scss, value: "@import 'missingfile';")+ theme.save!+ expect(field.reload.error).to include("Error: Can't find stylesheet to import.")- field.value = "body {color: blue};"- field.save!- field.ensure_baked!- expect(field.error).to eq(nil)+ theme.set_field(target: :common, name: :scss, value: "body {color: blue};")+ theme.save!+ expect(field.reload.error).to eq(nil)endit "allows importing scss files" do