Skip to content
Permalink
Browse files

FIX: Refactor to prevent themes affecting core stylesheets (#7029)

If a theme setting contained invalid SCSS, it would cause an error 500 on the site, with no way to recover. This commit stops loading theme settings in the core stylesheets, and instead only loads the color scheme variables. This change also makes `common/foundation/variables.scss` available to themes without an explicit import.
  • Loading branch information...
davidtaylorhq committed Feb 19, 2019
1 parent 9ade04b commit 7878e5007aa96f21a23320cfdc2cda5c5ad081d6
@@ -64,7 +64,7 @@ $line-height-large: 1.4; // Normal or small text
// These files don't actually exist. They're injected by Stylesheet::Compiler.
// --------------------------------------------------

@import "theme_variables";
@import "theme_colors";
@import "plugins_variables";
@import "common/foundation/math";

@@ -261,7 +261,7 @@ def ensure_baked!
def ensure_scss_compiles!
if ThemeField.scss_fields.include?(self.name)
begin
Stylesheet::Compiler.compile("@import \"theme_variables\"; @import \"theme_field\";",
Stylesheet::Compiler.compile("@import \"common/foundation/variables\"; @import \"theme_variables\"; @import \"theme_field\";",
"theme.scss",
theme_field: self.value.dup,
theme: self.theme
@@ -10,7 +10,7 @@ def self.compile_asset(asset, options = {})

if Importer.special_imports[asset.to_s]
filename = "theme.scss"
file = "@import \"theme_variables\"; @import \"#{asset}\";"
file = "@import \"common/foundation/variables\"; @import \"theme_variables\"; @import \"#{asset}\";"
else
filename = "#{asset}.scss"
path = "#{ASSET_ROOT}/#{filename}"
@@ -33,12 +33,19 @@ def self.register_import(name, &blk)
import_files(DiscoursePluginRegistry.sass_variables)
end

register_import "theme_variables" do
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
contents

Import.new("theme_colors.scss", source: contents)
end

register_import "theme_variables" do
contents = ""

theme&.all_theme_variables&.each do |field|
if field.type_id == ThemeField.types[:theme_upload_var]
@@ -290,7 +290,7 @@ def transpile(html)
theme.reload
expect(theme.theme_fields.find_by(name: :scss).error).to eq(nil)

scss, _map = Stylesheet::Compiler.compile('@import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
scss, _map = Stylesheet::Compiler.compile('@import "common/foundation/variables"; @import "theme_variables"; @import "desktop_theme"; ', "theme.scss", theme_id: theme.id)
expect(scss).to include(upload.url)
end
end

1 comment on commit 7878e50

@discoursebot

This comment has been minimized.

Copy link

commented on 7878e50 Feb 19, 2019

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/how-to-use-discourse-core-variables-in-your-theme/77551/10

Please sign in to comment.
You can’t perform that action at this time.