2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
require_dependency 'stylesheet/common'
|
|
|
|
require_dependency 'stylesheet/importer'
|
|
|
|
require_dependency 'stylesheet/functions'
|
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
|
|
|
|
class Compiler
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.compile_asset(asset, options = {})
|
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-02-19 23:55:59 +08:00
|
|
|
file = "@import \"common/foundation/variables\"; @import \"theme_variables\"; @import \"#{asset}\";"
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
|
|
|
filename = "#{asset}.scss"
|
|
|
|
path = "#{ASSET_ROOT}/#{filename}"
|
|
|
|
file = File.read path
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
compile(file, filename, options)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
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],
|
2017-04-12 22:52:52 +08:00
|
|
|
load_paths: [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
|