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