Module: Sasso

Defined in:
lib/sasso.rb,
lib/sasso.rb,
lib/sasso/version.rb

Defined Under Namespace

Classes: CompileError, CompileResult, Error

Constant Summary collapse

STYLES =
%i[expanded compressed].freeze
SYNTAXES =
%i[scss sass css].freeze
VERSION =

The gem version floats INDEPENDENTLY of the core ‘sasso` crate version; the native extension pins the crate exactly (ext/sasso/Cargo.toml: sasso = “=…”).

"0.2.3"

Class Method Summary collapse

Class Method Details

.compile(path, **opts) ⇒ Object

Compile the file at ‘path`. Syntax is inferred from the extension unless overridden; `url:` defaults to `path` so diagnostics get the dart-exact block.

The entry file’s own directory is searched FIRST for relative @use/@forward/ siblings), ahead of any caller-supplied ‘load_paths:`.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sasso.rb', line 70

def compile(path, **opts)
  src = File.read(path)
  inferred =
    case File.extname(path)
    when ".sass" then :sass
    when ".css"  then :css
    else :scss
    end
  given = Array(opts.delete(:load_paths)).map(&:to_s)
  load_paths = [File.dirname(path.to_s), *given]
  compile_string(src, syntax: inferred, url: path.to_s, load_paths: load_paths, **opts)
end

.compile_string(source, style: :expanded, syntax: :scss, indented: false, load_paths: [], url: nil, alert_ascii: false, source_map: false, source_map_include_sources: false) ⇒ Object

Compile a SCSS/Sass source String to a CSS String.

style:       :expanded (default) | :compressed
syntax:      :scss (default) | :sass | :css
indented:    true => shorthand for syntax: :sass
load_paths:  dirs searched for @use/@forward/@import (built-in importer)
url:         filename shown in diagnostics; ENABLES the dart-exact error block
alert_ascii: true => ASCII-only diagnostics (maps to the compiler's no-unicode)

Raises Sasso::CompileError on a compile failure; ArgumentError on bad options.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sasso.rb', line 49

def compile_string(source, style: :expanded, syntax: :scss, indented: false,
                   load_paths: [], url: nil, alert_ascii: false,
                   source_map: false, source_map_include_sources: false)
  syntax = :sass if indented
  validate!(style, STYLES, :style)
  validate!(syntax, SYNTAXES, :syntax)
  paths = Array(load_paths).map(&:to_s)
  src = String(source)
  return Sasso::Native._compile(src, style.to_s, syntax.to_s, paths, url && url.to_s, !alert_ascii) unless source_map

  css, map_json = Sasso::Native._compile_with_map(src, style.to_s, syntax.to_s, paths,
                                                  url && url.to_s, !alert_ascii, source_map_include_sources)
  CompileResult.new(css, JSON.parse(map_json))
end