Class: Sass::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/compiler.rb,
lib/sass/compiler/host.rb,
lib/sass/compiler/varint.rb,
lib/sass/compiler/connection.rb,
lib/sass/compiler/dispatcher.rb,
lib/sass/compiler/host/protofier.rb,
lib/sass/compiler/host/structifier.rb,
lib/sass/compiler/host/logger_registry.rb,
lib/sass/compiler/host/value_protofier.rb,
lib/sass/compiler/resilient_dispatcher.rb,
lib/sass/compiler/host/function_registry.rb,
lib/sass/compiler/host/importer_registry.rb

Overview

The Compiler for using dart-sass. Each instance creates its own communication Dispatcher with a dedicated compiler process.

Examples:

sass = Sass::Compiler.new
result = sass.compile_string('h1 { font-size: 40px; }')
result = sass.compile('style.scss')
sass.close

Instance Method Summary collapse

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



29
30
31
# File 'lib/sass/compiler.rb', line 29

def initialize
  @dispatcher = ResilientDispatcher.new(Dispatcher)
end

Instance Method Details

#closeObject



180
181
182
# File 'lib/sass/compiler.rb', line 180

def close
  @dispatcher.close
end

#closed?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/sass/compiler.rb', line 184

def closed?
  @dispatcher.closed?
end

#compile(path, load_paths: [], charset: true, source_map: false, source_map_include_sources: false, style: :expanded, functions: {}, importers: [], alert_ascii: false, alert_color: nil, logger: nil, quiet_deps: false, verbose: false) ⇒ CompileResult

Compiles the Sass file at path to CSS.

Parameters:

  • path (String)
  • load_paths (Array<String>) (defaults to: [])

    Paths in which to look for stylesheets loaded by rules like @use and @import.

  • charset (Boolean) (defaults to: true)

    By default, if the CSS document contains non-ASCII characters, Sass adds a @charset declaration (in expanded output mode) or a byte-order mark (in compressed mode) to indicate its encoding to browsers or other consumers. If charset is false, these annotations are omitted.

  • source_map (Boolean) (defaults to: false)

    Whether or not Sass should generate a source map.

  • source_map_include_sources (Boolean) (defaults to: false)

    Whether Sass should include the sources in the generated source map.

  • style (String, Symbol) (defaults to: :expanded)

    The OutputStyle of the compiled CSS.

  • functions (Hash<String, Proc>) (defaults to: {})

    Additional built-in Sass functions that are available in all stylesheets.

  • importers (Array<Object>) (defaults to: [])

    Custom importers that control how Sass resolves loads from rules like @use and @import.

  • alert_ascii (Boolean) (defaults to: false)

    If this is true, the compiler will exclusively use ASCII characters in its error and warning messages. Otherwise, it may use non-ASCII Unicode characters as well.

  • alert_color (Boolean) (defaults to: nil)

    If this is true, the compiler will use ANSI color escape codes in its error and warning messages. If it’s false, it won’t use these. If it’s nil, the compiler will determine whether or not to use colors depending on whether the user is using an interactive terminal.

  • logger (Object) (defaults to: nil)

    An object to use to handle warnings and/or debug messages from Sass.

  • quiet_deps (Boolean) (defaults to: false)

    If this option is set to true, Sass won’t print warnings that are caused by dependencies. A “dependency” is defined as any file that’s loaded through load_paths or importer. Stylesheets that are imported relative to the entrypoint are not considered dependencies.

  • verbose (Boolean) (defaults to: false)

    By default, Dart Sass will print only five instances of the same deprecation warning per compilation to avoid deluging users in console noise. If you set verbose to true, it will instead print every deprecation warning it encounters.

Returns:

Raises:

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sass/compiler.rb', line 61

def compile(path,
            load_paths: [],

            charset: true,
            source_map: false,
            source_map_include_sources: false,
            style: :expanded,

            functions: {},
            importers: [],

            alert_ascii: false,
            alert_color: nil,
            logger: nil,
            quiet_deps: false,
            verbose: false)
  raise ArgumentError, 'path must be set' if path.nil?

  Host.new(@dispatcher).compile_request(
    path:,
    source: nil,
    importer: nil,
    load_paths:,
    syntax: nil,
    url: nil,
    charset:,
    source_map:,
    source_map_include_sources:,
    style:,
    functions:,
    importers:,
    alert_color:,
    alert_ascii:,
    logger:,
    quiet_deps:,
    verbose:
  )
end

#compile_string(source, importer: nil, load_paths: [], syntax: :scss, url: nil, charset: true, source_map: false, source_map_include_sources: false, style: :expanded, functions: {}, importers: [], alert_ascii: false, alert_color: nil, logger: nil, quiet_deps: false, verbose: false) ⇒ CompileResult

Compiles a stylesheet whose contents is source to CSS.

Parameters:

  • source (String)
  • importer (Object) (defaults to: nil)

    The importer to use to handle loads that are relative to the entrypoint stylesheet.

  • load_paths (Array<String>) (defaults to: [])

    Paths in which to look for stylesheets loaded by rules like @use and @import.

  • syntax (String, Symbol) (defaults to: :scss)

    The Syntax to use to parse the entrypoint stylesheet.

  • url (String) (defaults to: nil)

    The canonical URL of the entrypoint stylesheet. If this is passed along with importer, it’s used to resolve relative loads in the entrypoint stylesheet.

  • charset (Boolean) (defaults to: true)

    By default, if the CSS document contains non-ASCII characters, Sass adds a @charset declaration (in expanded output mode) or a byte-order mark (in compressed mode) to indicate its encoding to browsers or other consumers. If charset is false, these annotations are omitted.

  • source_map (Boolean) (defaults to: false)

    Whether or not Sass should generate a source map.

  • source_map_include_sources (Boolean) (defaults to: false)

    Whether Sass should include the sources in the generated source map.

  • style (String, Symbol) (defaults to: :expanded)

    The OutputStyle of the compiled CSS.

  • functions (Hash<String, Proc>) (defaults to: {})

    Additional built-in Sass functions that are available in all stylesheets.

  • importers (Array<Object>) (defaults to: [])

    Custom importers that control how Sass resolves loads from rules like @use and @import.

  • alert_ascii (Boolean) (defaults to: false)

    If this is true, the compiler will exclusively use ASCII characters in its error and warning messages. Otherwise, it may use non-ASCII Unicode characters as well.

  • alert_color (Boolean) (defaults to: nil)

    If this is true, the compiler will use ANSI color escape codes in its error and warning messages. If it’s false, it won’t use these. If it’s nil, the compiler will determine whether or not to use colors depending on whether the user is using an interactive terminal.

  • logger (Object) (defaults to: nil)

    An object to use to handle warnings and/or debug messages from Sass.

  • quiet_deps (Boolean) (defaults to: false)

    If this option is set to true, Sass won’t print warnings that are caused by dependencies. A “dependency” is defined as any file that’s loaded through load_paths or importer. Stylesheets that are imported relative to the entrypoint are not considered dependencies.

  • verbose (Boolean) (defaults to: false)

    By default, Dart Sass will print only five instances of the same deprecation warning per compilation to avoid deluging users in console noise. If you set verbose to true, it will instead print every deprecation warning it encounters.

Returns:

Raises:

See Also:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sass/compiler.rb', line 132

def compile_string(source,
                   importer: nil,
                   load_paths: [],
                   syntax: :scss,
                   url: nil,

                   charset: true,
                   source_map: false,
                   source_map_include_sources: false,
                   style: :expanded,

                   functions: {},
                   importers: [],

                   alert_ascii: false,
                   alert_color: nil,
                   logger: nil,
                   quiet_deps: false,
                   verbose: false)
  raise ArgumentError, 'source must be set' if source.nil?

  Host.new(@dispatcher).compile_request(
    path: nil,
    source:,
    importer:,
    load_paths:,
    syntax:,
    url:,
    charset:,
    source_map:,
    source_map_include_sources:,
    style:,
    functions:,
    importers:,
    alert_color:,
    alert_ascii:,
    logger:,
    quiet_deps:,
    verbose:
  )
end

#infoString

Returns Information about the Sass implementation.

Returns:

  • (String)

    Information about the Sass implementation.

See Also:



176
177
178
# File 'lib/sass/compiler.rb', line 176

def info
  @info ||= Host.new(@dispatcher).version_request
end