FIX: Do not include theme variables in plugin SCSS, and fix register_css

FIX: Do not include theme variables in plugin SCSS, and fix register_css

diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb
index e45bae2..8d8a0c6 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -343,7 +343,7 @@ class ThemeField < ActiveRecord::Base
   end
 
   def compile_scss
-    Stylesheet::Compiler.compile("@import \"common/foundation/variables\"; @import \"theme_variables\"; @import \"theme_field\";",
+    Stylesheet::Compiler.compile("@import \"common/foundation/variables\"; @import \"common/foundation/mixins\"; @import \"theme_variables\"; @import \"theme_field\";",
       "theme.scss",
       theme_field: self.value.dup,
       theme: self.theme
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 4a3211d..28a7c34 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -266,7 +266,7 @@ class Plugin::Instance
     automatic_assets.each do |path, contents|
       write_asset(path, contents)
       paths << path
-      assets << [path]
+      assets << [path, nil, directory_name]
     end
 
     delete_extra_automatic_assets(paths)
diff --git a/lib/stylesheet/compiler.rb b/lib/stylesheet/compiler.rb
index 1dea378..f1519ac 100644
--- a/lib/stylesheet/compiler.rb
+++ b/lib/stylesheet/compiler.rb
@@ -12,7 +12,9 @@ module Stylesheet
 
       if Importer.special_imports[asset.to_s]
         filename = "theme_#{options[:theme_id]}.scss"
-        file = "@import \"common/foundation/variables\"; @import \"common/foundation/mixins\"; @import \"theme_variables\"; @import \"#{asset}\";"
+        file = "@import \"common/foundation/variables\"; @import \"common/foundation/mixins\";"
+        file += " @import \"theme_variables\";" if Importer::THEME_TARGETS.include?(asset.to_s)
+        file += " @import \"#{asset}\";"
       else
         filename = "#{asset}.scss"
         path = "#{ASSET_ROOT}/#{filename}"
diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rb
index d06b4db..5cf12e0 100644
--- a/lib/stylesheet/importer.rb
+++ b/lib/stylesheet/importer.rb
@@ -7,6 +7,8 @@ module Stylesheet
   class Importer < SassC::Importer
     include GlobalPath
 
+    THEME_TARGETS = %w{embedded_theme mobile_theme desktop_theme}
+
     def self.special_imports
       @special_imports ||= {}
     end
@@ -15,88 +17,95 @@ module Stylesheet
       special_imports[name] = blk
     end
 
-    register_import "theme_field" do
-      Import.new("#{theme_dir(@theme_id)}/theme_field.scss", source: @theme_field)
-    end
+    # Contained in function so that it can be called repeatedly from test mode
+    def self.register_imports!
+      @special_imports = {}
+
+      register_import "theme_field" do
+        Import.new("#{theme_dir(@theme_id)}/theme_field.scss", source: @theme_field)
+      end
 
-    Discourse.plugins.each do |plugin|
-      plugin_directory_name = plugin.directory_name
+      Discourse.plugins.each do |plugin|
+        plugin_directory_name = plugin.directory_name
 
-      ["", "mobile", "desktop"].each do |type|
-        asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name
-        stylesheets = type.present? ? DiscoursePluginRegistry.send("#{type}_stylesheets") : DiscoursePluginRegistry.stylesheets
+        ["", "mobile", "desktop"].each do |type|
+          asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name
+          stylesheets = type.present? ? DiscoursePluginRegistry.send("#{type}_stylesheets") : DiscoursePluginRegistry.stylesheets
 
-        if stylesheets[plugin_directory_name].present?
-          register_import asset_name do
-            import_files(stylesheets[plugin_directory_name])
+          if stylesheets[plugin_directory_name].present?
+            register_import asset_name do
+              import_files(stylesheets[plugin_directory_name])
+            end
           end
         end
       end
-    end
-
-    register_import "plugins_variables" do
-      import_files(DiscoursePluginRegistry.sass_variables)
-    end
 
-    register_import "theme_colors" do
-      contents = +""
-      colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors
-      colors.each do |n, hex|
-        contents << "$#{n}: ##{hex} !default;\n"
+      register_import "plugins_variables" do
+        import_files(DiscoursePluginRegistry.sass_variables)
       end
 
-      Import.new("theme_colors.scss", source: contents)
-    end
+      register_import "theme_colors" do
+        contents = +""
+        colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors
+        colors.each do |n, hex|
+          contents << "$#{n}: ##{hex} !default;\n"
+        end
 
-    register_import "theme_variables" do
-      contents = +""
+        Import.new("theme_colors.scss", source: contents)
+      end
 
-      theme&.all_theme_variables&.each do |field|
-        if field.type_id == ThemeField.types[:theme_upload_var]
-          if upload = field.upload
-            url = upload_cdn_path(upload.url)
-            contents << "$#{field.name}: unquote(\"#{url}\");\n"
+      register_import "theme_variables" do
+        contents = +""
+
+        theme&.all_theme_variables&.each do |field|
+          if field.type_id == ThemeField.types[:theme_upload_var]
+            if upload = field.upload
+              url = upload_cdn_path(upload.url)
+              contents << "$#{field.name}: unquote(\"#{url}\");\n"
+            end
+          else
+            contents << to_scss_variable(field.name, field.value)
           end
-        else
-          contents << to_scss_variable(field.name, field.value)
         end
-      end
 
-      theme&.included_settings&.each do |name, value|
-        next if name == "theme_uploads"
-        contents << to_scss_variable(name, value)
+        theme&.included_settings&.each do |name, value|
+          next if name == "theme_uploads"
+          contents << to_scss_variable(name, value)
+        end
+
+        Import.new("theme_variable.scss", source: contents)
       end
 
-      Import.new("theme_variable.scss", source: contents)
-    end
+      register_import "category_backgrounds" do
+        contents = +""
+        Category.where('uploaded_background_id IS NOT NULL').each do |c|
+          contents << category_css(c) if c.uploaded_background&.url.present?
+        end
 
-    register_import "category_backgrounds" do
-      contents = +""
-      Category.where('uploaded_background_id IS NOT NULL').each do |c|
-        contents << category_css(c) if c.uploaded_background&.url.present?
+        Import.new("category_background.scss", source: contents)
       end
 
-      Import.new("category_background.scss", source: contents)
-    end
-
-    register_import "embedded_theme" do
-      next unless @theme_id
+      register_import "embedded_theme" do
+        next unless @theme_id
 
-      theme_import(:common, :embedded_scss)
-    end
+        theme_import(:common, :embedded_scss)
+      end
 
-    register_import "mobile_theme" do
-      next unless @theme_id
+      register_import "mobile_theme" do
+        next unless @theme_id
 
-      theme_import(:mobile, :scss)
-    end
+        theme_import(:mobile, :scss)
+      end
 
-    register_import "desktop_theme" do
-      next unless @theme_id
+      register_import "desktop_theme" do
+        next unless @theme_id
 
-      theme_import(:desktop, :scss)
+        theme_import(:desktop, :scss)
+      end
     end
 
+    register_imports!
+
     def initialize(options)
       @theme = options[:theme]
       @theme_id = options[:theme_id]
diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index 2d9a275..266c7f2 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -168,7 +168,7 @@ class Stylesheet::Manager
          source_map_file: source_map_filename
       )
     rescue SassC::SyntaxError => e
-      if %w{embedded_theme mobile_theme desktop_theme}.include?(@target.to_s)
+      if Importer::THEME_TARGETS.include?(@target.to_s)

[... diff too long, it was truncated ...]

GitHub sha: 081c36a4

1 Like