discourse/lib/stylesheet/compiler.rb
Rafael dos Santos Silva 6e522e4aad
DEV: Move to Sass compilation to dart-sass (#19910)
This PR is a major change to Sass compilation in Discourse.

The new version of sass-ruby moves to dart-sass putting we back on the supported version of Sass. It does so while keeping compatibility with the existing method signatures, so minimal change is needed in Discourse for this change.

This moves us

From:
  - sassc 2.0.1 (Feb 2019)
  - libsass 3.5.2 (May 2018)

To:
  - dart-sass 1.58

This update applies the following breaking changes:

> 
> These breaking changes are coming soon or have recently been released:
> 
>  [Functions are stricter about which units they allow](https://sass-lang.com/documentation/breaking-changes/function-units) beginning in Dart Sass 1.32.0.
> 
>  [Selectors with invalid combinators are invalid](https://sass-lang.com/documentation/breaking-changes/bogus-combinators) beginning in Dart Sass 1.54.0.
> 
>  [/ is changing from a division operation to a list separator](https://sass-lang.com/documentation/breaking-changes/slash-div) beginning in Dart Sass 1.33.0.
> 
>  [Parsing the special syntax of @-moz-document will be invalid](https://sass-lang.com/documentation/breaking-changes/moz-document) beginning in Dart Sass 1.7.2.
> 
>  [Compound selectors could not be extended](https://sass-lang.com/documentation/breaking-changes/extend-compound) in Dart Sass 1.0.0 and Ruby Sass 4.0.0.


SCSS files have been migrated automatically using `sass-migrator division app/assets/stylesheets/**/*.scss`
2023-02-07 12:24:57 -03:00

80 lines
2.3 KiB
Ruby

# frozen_string_literal: true
require "stylesheet/importer"
module Stylesheet
class Compiler
ASSET_ROOT = "#{Rails.root}/app/assets/stylesheets" unless defined?(ASSET_ROOT)
def self.compile_asset(asset, options = {})
importer = Importer.new(options)
file = importer.prepended_scss
if Importer::THEME_TARGETS.include?(asset.to_s)
filename = "theme_#{options[:theme_id]}.scss"
file += options[:theme_variables].to_s
file += importer.theme_import(asset)
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
else
filename = "#{asset}.scss"
path = "#{ASSET_ROOT}/#{filename}"
file += File.read path
case asset.to_s
when "embed", "publish"
file += importer.font
when "wizard"
file += importer.wizard_fonts
when Stylesheet::Manager::COLOR_SCHEME_STYLESHEET
file += importer.import_color_definitions
file += importer.import_wcag_overrides
file += importer.category_backgrounds
file += importer.font
end
end
compile(file, filename, options)
end
def self.compile(stylesheet, filename, options = {})
source_map_file = options[:source_map_file] || "#{filename.sub(".scss", "")}.css.map"
load_paths = [ASSET_ROOT]
load_paths += options[:load_paths] if options[:load_paths]
engine =
SassC::Engine.new(
stylesheet,
filename: filename,
style: :compressed,
source_map_file: source_map_file,
source_map_contents: true,
theme_id: options[:theme_id],
theme: options[:theme],
theme_field: options[:theme_field],
color_scheme_id: options[:color_scheme_id],
load_paths: load_paths,
validate_source_map_path: false,
)
result = engine.render
if options[:rtl]
require "rtlcss_wrapper"
[RtlcssWrapper.flip_css(result), nil]
else
source_map = engine.source_map
source_map.force_encoding("UTF-8")
[result, source_map]
end
end
end
end