discourse/lib/stylesheet/compiler.rb
Penar Musaraj c937afc75e
FEATURE: automatic dark mode (#10341)
A first step to adding automatic dark mode color scheme switching. Adds a new SCSS file at `color_definitions.scss` that serves to output all SCSS color variables as CSS custom properties. And replaces all SCSS color variables with the new CSS custom properties throughout the stylesheets. 

This is an alpha feature at this point, can only be enabled via console using the `default_dark_mode_color_scheme_id` site setting.
2020-08-03 22:57:10 -04:00

57 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require 'stylesheet/common'
require 'stylesheet/importer'
require 'stylesheet/functions'
module Stylesheet
class Compiler
def self.compile_asset(asset, options = {})
if Importer.special_imports[asset.to_s]
filename = "theme_#{options[:theme_id]}.scss"
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 = "#{Stylesheet::Common::ASSET_ROOT}/#{filename}"
file = File.read path
end
compile(file, filename, options)
end
def self.compile(stylesheet, filename, options = {})
source_map_file = options[:source_map_file] || "#{filename.sub(".scss", "")}.css.map"
engine = SassC::Engine.new(stylesheet,
importer: Importer,
filename: filename,
style: :compressed,
source_map_file: source_map_file,
source_map_contents: true,
theme_id: options[:theme_id],
theme: options[:theme],
theme_field: options[:theme_field],
color_scheme_id: options[:color_scheme_id],
load_paths: [Stylesheet::Common::ASSET_ROOT])
result = engine.render
if options[:rtl]
require 'r2'
[R2.r2(result), nil]
else
source_map = engine.source_map
source_map.force_encoding("UTF-8")
[result, source_map]
end
end
end
end