2017-04-12 22:52:52 +08:00
|
|
|
require_dependency 'stylesheet/common'
|
|
|
|
require_dependency 'stylesheet/importer'
|
|
|
|
require_dependency 'stylesheet/functions'
|
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
|
|
|
|
class Compiler
|
|
|
|
|
|
|
|
def self.error_as_css(error, label)
|
|
|
|
error = error.message
|
|
|
|
error.gsub!("\n", '\A ')
|
|
|
|
error.gsub!("'", '\27 ')
|
|
|
|
|
2017-08-16 23:47:50 +08:00
|
|
|
"#main { display: none; }
|
|
|
|
body { white-space: pre; }
|
|
|
|
body:before { font-family: monospace; content: '#{error}' }"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
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]
|
|
|
|
filename = "theme.scss"
|
2017-04-13 01:37:27 +08:00
|
|
|
file = "@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 = {})
|
|
|
|
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
|