Class: Proscenium::CssModule::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/proscenium/css_module/transformer.rb

Constant Summary collapse

FILE_EXT =
'.module.css'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path) ⇒ Transformer

Returns a new instance of Transformer.



11
12
13
14
15
16
# File 'lib/proscenium/css_module/transformer.rb', line 11

def initialize(source_path)
  return unless (@source_path = source_path)

  @source_path = Pathname.new(@source_path) unless @source_path.is_a?(Pathname)
  @source_path = @source_path.sub_ext(FILE_EXT) unless @source_path.to_s.end_with?(FILE_EXT)
end

Class Method Details

.class_names(path, *names) ⇒ Object



7
8
9
# File 'lib/proscenium/css_module/transformer.rb', line 7

def self.class_names(path, *names)
  new(path).class_names(*names)
end

Instance Method Details

#class_name!(name, original_name, path: @source_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/proscenium/css_module/transformer.rb', line 55

def class_name!(name, original_name, path: @source_path)
  unless path
    raise Proscenium::CssModule::TransformError.new(original_name, 'CSS module path not given')
  end

  digest = Importer.import(path.to_s)

  transformed_name = name.to_s
  if transformed_name.start_with?('_')
    "_#{transformed_name[1..]}_#{digest}"
  else
    "#{transformed_name}_#{digest}"
  end
end

#class_names(*names, require_prefix: true) {|transformed_name, side_load_path| ... } ⇒ Array<String>

Transform each of the given class ‘names` to their respective CSS module name, which consist of the name, and suffixed with the digest of the resolved source path.

Any name beginning with ‘@’ will be transformed to a CSS module name. If ‘require_prefix` is false, then all names will be transformed to a CSS module name regardless of whether or not they begin with ’@‘.

class_names :@my_module_name, :my_class_name

Note that the generated digest is based on the resolved (URL) path, not the original path.

You can also provide a path specifier and class name. The path will be the URL path to a stylesheet. The class name will be the name of the class to transform.

class_names "/lib/button@default"
class_names "mypackage/button@large"
class_names "@scoped/package/button@small"

When a block is given, it is yielded once per input name with ‘(transformed_name, side_load_path)`. `side_load_path` is the exact string passed to `Importer.import` for that name, or `nil` for names that did not trigger a side-load (plain class names with `require_prefix: true`). The return value is unchanged — callers that don’t need the path can keep ignoring the block.

Parameters:

  • names (String, Symbol, Array<String,Symbol>)
  • require_prefix: (Boolean) (defaults to: true)

    whether or not to require the ‘@` prefix.

Yield Parameters:

  • transformed_name (String)
  • side_load_path (String, nil)

Returns:

  • (Array<String>)

    the transformed CSS module names.



47
48
49
50
51
52
53
# File 'lib/proscenium/css_module/transformer.rb', line 47

def class_names(*names, require_prefix: true)
  names.map do |name|
    transformed, path = transform_class_name(name, require_prefix: require_prefix)
    yield(transformed, path) if block_given?
    transformed
  end
end