Module: CSSModules::Rewrite

Defined in:
lib/css_modules/rewrite.rb

Defined Under Namespace

Classes: ModuleVisitor

Constant Summary collapse

RE_MODULE =

Module Scopes :module(login) { .button red; }

/^\:module\((?<module_name>.*?)\)\s+(?<declarations>.*)/

Class Method Summary collapse

Class Method Details

.modulize_selector(module_name, selector) ⇒ String

Combine ‘module_name` and `selector`, but don’t prepend a ‘.` or `#` because this value will be inserted into the HTML page as `class=` or `id=`

Parameters:

  • module_name (String)

    A CSS module name

  • selector (String)

    A would-be DOM selector (without the leading ‘.` or `#`)

Returns:

  • (String)

    An opaque selector for this module-selector pair



30
31
32
33
# File 'lib/css_modules/rewrite.rb', line 30

def modulize_selector(module_name, selector)
  tran = CSSModules.env == :production ? Transform::ProductionTransform : Transform::DevelopmentTransform
  tran.transform(module_name, selector)
end

.rewrite_css(css_module_code) ⇒ Object

Take css module code as input, and rewrite it as browser-friendly CSS code. Apply opaque transformations so that selectors can only be accessed programatically, not by class name literals.



15
16
17
18
19
20
21
22
23
# File 'lib/css_modules/rewrite.rb', line 15

def rewrite_css(css_module_code)
  # Parse incoming CSS into an AST
  css_root = Sass::SCSS::CssParser.new(css_module_code, "(CSSModules)", 1).parse

  Sass::Tree::Visitors::SetOptions.visit(css_root, {})
  ModuleVisitor.visit(css_root)

  css_root.render
end