module CSSModules::Rewrite

Constants

RE_MODULE

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

Public Instance Methods

modulize_selector(module_name, selector) click to toggle source

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=` @param module_name [String] A CSS module name @param selector [String] A would-be DOM selector (without the leading `.` or `#`) @return [String] An opaque selector for this module-selector pair

# 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) click to toggle source

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.

# 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