Class: ViewComponentScopedCss::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component_scoped_css/compiler.rb

Overview

compiling scss files to provide final css

Instance Method Summary collapse

Constructor Details

#initialize(path:, identifier:) ⇒ Compiler

Returns a new instance of Compiler.



8
9
10
11
12
13
# File 'lib/view_component_scoped_css/compiler.rb', line 8

def initialize(path:, identifier:)
  @identifier = identifier
  @css = Sass.compile(path).css
rescue Sass::CompileError => e
  raise "scss[#{path}] #{e.message}"
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/view_component_scoped_css/compiler.rb', line 15

def call
  @css.lines.map do |line|
    case line
    when /:module\((.*?)\)/
      css_class_name = [".viewcomponent", @identifier, ::Regexp.last_match(1)].compact.join('\:\:')
      line.gsub(Regexp.last_match[0], css_class_name)
    else
      line
    end
  end.join
end