2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-02 12:01:53 +08:00
|
|
|
require 'stylesheet/common'
|
|
|
|
require 'stylesheet/importer'
|
|
|
|
require 'stylesheet/functions'
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
|
|
|
|
class Compiler
|
|
|
|
|
|
|
|
def self.compile_asset(asset, options = {})
|
2021-01-06 03:05:34 +08:00
|
|
|
file = "@import \"common/foundation/variables\"; @import \"common/foundation/mixins\";"
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
if Importer.special_imports[asset.to_s]
|
2019-04-12 18:36:08 +08:00
|
|
|
filename = "theme_#{options[:theme_id]}.scss"
|
2019-09-17 00:06:34 +08:00
|
|
|
file += " @import \"theme_variables\";" if Importer::THEME_TARGETS.include?(asset.to_s)
|
|
|
|
file += " @import \"#{asset}\";"
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
|
|
|
filename = "#{asset}.scss"
|
2019-10-02 12:01:53 +08:00
|
|
|
path = "#{Stylesheet::Common::ASSET_ROOT}/#{filename}"
|
2021-01-06 03:05:34 +08:00
|
|
|
file += File.read path
|
2020-08-06 21:46:17 +08:00
|
|
|
|
|
|
|
if asset.to_s == Stylesheet::Manager::COLOR_SCHEME_STYLESHEET
|
2020-08-19 01:02:13 +08:00
|
|
|
file += Stylesheet::Importer.import_color_definitions(options[:theme_id])
|
2020-10-16 02:05:48 +08:00
|
|
|
file += Stylesheet::Importer.import_wcag_overrides(options[:color_scheme_id])
|
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
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
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],
|
2017-05-10 05:20:28 +08:00
|
|
|
theme: options[:theme],
|
2017-04-20 04:46:28 +08:00
|
|
|
theme_field: options[:theme_field],
|
2020-08-04 10:57:10 +08:00
|
|
|
color_scheme_id: options[:color_scheme_id],
|
2019-10-02 12:01:53 +08:00
|
|
|
load_paths: [Stylesheet::Common::ASSET_ROOT])
|
2017-04-12 22:52:52 +08: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
|