Class: LightningCSS::Transformer

Inherits:
Object
  • Object
show all
Defined in:
lib/lightningcss/transformer.rb,
sig/lightningcss/transformer.rbs

Overview

A set of options to transform many stylesheets with.

transformer = LightningCSS::Transformer.new(minify: true, targets: { chrome: 100 })

transformer.transform(".a { color: red }").code
transformer.transform(".b { color: red }", scope: "[data-scope-abc]").code

Options given to a call are merged over the ones it was built with, so the ones that belong to the project are written once and the ones that belong to a single stylesheet travel with it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Transformer

: (?filename: String?, ?minify: bool, ?error_recovery: bool, ?targets: browsers?, ?css_modules: css_modules?, ?scope: String?) -> void

Parameters:

  • filename: (String, nil)
  • minify: (Boolean)
  • error_recovery: (Boolean)
  • targets: (browsers, nil)
  • css_modules: (css_modules, nil)
  • scope: (String, nil)


18
19
20
21
22
# File 'lib/lightningcss/transformer.rb', line 18

def initialize(**options)
  @options = options.transform_keys(&:to_sym).freeze

  freeze
end

Instance Attribute Details

#optionsHash[Symbol, untyped] (readonly)

Signature:

  • Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


15
16
17
# File 'lib/lightningcss/transformer.rb', line 15

def options
  @options
end

Instance Method Details

#bundle(path, **overrides) ⇒ LightningCSS::Result

: (String, ?filename: String?, ?minify: bool, ?error_recovery: bool, ?targets: browsers?, ?css_modules: css_modules?, ?scope: String?) -> LightningCSS::Result

Parameters:

  • (String)
  • filename: (String, nil)
  • minify: (Boolean)
  • error_recovery: (Boolean)
  • targets: (browsers, nil)
  • css_modules: (css_modules, nil)
  • scope: (String, nil)

Returns:



32
33
34
# File 'lib/lightningcss/transformer.rb', line 32

def bundle(path, **overrides)
  LightningCSS.bundle(path, **options, **overrides)
end

#inspectString

: () -> String

Returns:

  • (String)


52
53
54
# File 'lib/lightningcss/transformer.rb', line 52

def inspect
  "#<#{self.class.name} #{options.inspect}>"
end

#minify(code, **overrides) ⇒ String

: (String, ?filename: String?, ?error_recovery: bool, ?targets: browsers?, ?css_modules: css_modules?, ?scope: String?) -> String

Parameters:

  • (String)
  • filename: (String, nil)
  • error_recovery: (Boolean)
  • targets: (browsers, nil)
  • css_modules: (css_modules, nil)
  • scope: (String, nil)

Returns:

  • (String)


42
43
44
# File 'lib/lightningcss/transformer.rb', line 42

def minify(code, **overrides)
  transform(code, **overrides, minify: true).code
end

#transform(code, **overrides) ⇒ LightningCSS::Result Also known as: call

: (String, ?filename: String?, ?minify: bool, ?error_recovery: bool, ?targets: browsers?, ?css_modules: css_modules?, ?scope: String?) -> LightningCSS::Result

Parameters:

  • (String)
  • filename: (String, nil)
  • minify: (Boolean)
  • error_recovery: (Boolean)
  • targets: (browsers, nil)
  • css_modules: (css_modules, nil)
  • scope: (String, nil)

Returns:



25
26
27
# File 'lib/lightningcss/transformer.rb', line 25

def transform(code, **overrides)
  LightningCSS.transform(code, **options, **overrides)
end

#transform_style_attribute(code, **overrides) ⇒ LightningCSS::Result

: (String, ?filename: String?, ?minify: bool, ?error_recovery: bool, ?targets: browsers?) -> LightningCSS::Result

Parameters:

  • (String)
  • filename: (String, nil)
  • minify: (Boolean)
  • error_recovery: (Boolean)
  • targets: (browsers, nil)

Returns:



37
38
39
# File 'lib/lightningcss/transformer.rb', line 37

def transform_style_attribute(code, **overrides)
  LightningCSS.transform_style_attribute(code, **options, **overrides)
end

#with(**overrides) ⇒ LightningCSS::Transformer

: (?filename: String?, ?minify: bool, ?error_recovery: bool, ?targets: browsers?, ?css_modules: css_modules?, ?scope: String?) -> LightningCSS::Transformer

Parameters:

  • filename: (String, nil)
  • minify: (Boolean)
  • error_recovery: (Boolean)
  • targets: (browsers, nil)
  • css_modules: (css_modules, nil)
  • scope: (String, nil)

Returns:



47
48
49
# File 'lib/lightningcss/transformer.rb', line 47

def with(**overrides)
  self.class.new(**options, **overrides)
end