2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-01 23:01:53 -05:00
|
|
|
require 'stylesheet/importer'
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
|
|
|
|
class Compiler
|
2021-03-12 10:17:42 -06:00
|
|
|
ASSET_ROOT = "#{Rails.root}/app/assets/stylesheets" unless defined? ASSET_ROOT
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.compile_asset(asset, options = {})
|
2021-02-19 10:22:24 -06:00
|
|
|
importer = Importer.new(options)
|
|
|
|
file = importer.prepended_scss
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-02-02 12:09:41 -06:00
|
|
|
if Importer::THEME_TARGETS.include?(asset.to_s)
|
|
|
|
filename = "theme_#{options[:theme_id]}.scss"
|
|
|
|
file += options[:theme_variables].to_s
|
2021-02-19 10:22:24 -06:00
|
|
|
file += importer.theme_import(asset)
|
2021-03-12 10:17:42 -06:00
|
|
|
elsif plugin_assets = Importer.plugin_assets[asset.to_s]
|
|
|
|
filename = "#{asset.to_s}.scss"
|
|
|
|
options[:load_paths] = [] if options[:load_paths].nil?
|
|
|
|
plugin_assets.each do |src|
|
|
|
|
file += File.read src
|
|
|
|
options[:load_paths] << File.expand_path(File.dirname(src))
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
else
|
|
|
|
filename = "#{asset}.scss"
|
2021-03-12 10:17:42 -06:00
|
|
|
path = "#{ASSET_ROOT}/#{filename}"
|
2021-01-05 13:05:34 -06:00
|
|
|
file += File.read path
|
2020-08-06 08:46:17 -05:00
|
|
|
|
2021-03-10 10:05:56 -06:00
|
|
|
case asset.to_s
|
|
|
|
when "embed", "publish"
|
|
|
|
file += importer.font
|
|
|
|
when "wizard"
|
|
|
|
file += importer.wizard_fonts
|
2021-03-12 10:17:42 -06:00
|
|
|
when Stylesheet::Manager::COLOR_SCHEME_STYLESHEET
|
2021-02-19 10:22:24 -06:00
|
|
|
file += importer.import_color_definitions
|
|
|
|
file += importer.import_wcag_overrides
|
2021-07-06 12:11:10 -05:00
|
|
|
file += importer.category_backgrounds
|
|
|
|
file += importer.font
|
2020-08-06 08:46:17 -05:00
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
compile(file, filename, options)
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.compile(stylesheet, filename, options = {})
|
2018-12-03 21:48:13 -06:00
|
|
|
source_map_file = options[:source_map_file] || "#{filename.sub(".scss", "")}.css.map"
|
2017-04-19 15:46:28 -05:00
|
|
|
|
2021-03-12 10:17:42 -06:00
|
|
|
load_paths = [ASSET_ROOT]
|
2021-02-02 12:09:41 -06:00
|
|
|
load_paths += options[:load_paths] if options[:load_paths]
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
engine = SassC::Engine.new(stylesheet,
|
|
|
|
filename: filename,
|
|
|
|
style: :compressed,
|
|
|
|
source_map_file: source_map_file,
|
|
|
|
source_map_contents: true,
|
|
|
|
theme_id: options[:theme_id],
|
2017-05-09 16:20:28 -05:00
|
|
|
theme: options[:theme],
|
2017-04-19 15:46:28 -05:00
|
|
|
theme_field: options[:theme_field],
|
2020-08-03 21:57:10 -05:00
|
|
|
color_scheme_id: options[:color_scheme_id],
|
2021-02-02 12:09:41 -06:00
|
|
|
load_paths: load_paths)
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
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
|