Class: LightningCSS::Options

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

Overview

The options a call was given, on their way to the native library.

Lightning CSS reads them as JSON, so this is where a Ruby hash becomes one, and where an option nobody knows is refused. Refusing early is the point: an option the native side does not read would otherwise be accepted and quietly do nothing.

LightningCSS::Options.new(minify: true).to_json  #=> "{\"minify\":true}"

Constant Summary collapse

KNOWN =

Returns:

  • (Array[Symbol])
[
  :filename,
  :minify,
  :error_recovery,
  :targets,
  :css_modules,
  :scope
].freeze
STYLE_ATTRIBUTE =

Signature:

  • Array[Symbol]

Returns:

  • (Array[Symbol])
[
  :filename,
  :minify,
  :error_recovery,
  :targets
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed: KNOWN, subject: "a transform", **options) ⇒ Options

: (?allowed: Array, ?subject: String, **untyped) -> void

Parameters:

  • allowed: (Array[Symbol]) (defaults to: KNOWN)
  • subject: (String) (defaults to: "a transform")
  • (Object)


37
38
39
40
41
42
43
44
45
# File 'lib/lightningcss/options.rb', line 37

def initialize(allowed: KNOWN, subject: "a transform", **options)
  given = options.transform_keys(&:to_sym)

  validate!(given.keys, allowed, subject)

  @to_h = normalize(given).freeze

  freeze
end

Instance Attribute Details

#to_hHash[Symbol, untyped] (readonly)

Signature:

  • Array[Symbol]

Returns:

  • (Hash[Symbol, untyped])


29
30
31
# File 'lib/lightningcss/options.rb', line 29

def to_h
  @to_h
end

Class Method Details

.serialize(options, allowed: KNOWN, subject: "a transform") ⇒ String

: (Hash[Symbol, untyped], ?allowed: Array, ?subject: String) -> String

Parameters:

  • (Hash[Symbol, untyped])
  • allowed: (Array[Symbol]) (defaults to: KNOWN)
  • subject: (String) (defaults to: "a transform")

Returns:

  • (String)


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

def self.serialize(options, allowed: KNOWN, subject: "a transform")
  new(allowed: allowed, subject: subject, **options).to_json
end

Instance Method Details

#inspectString

: () -> String

Returns:

  • (String)


53
54
55
# File 'lib/lightningcss/options.rb', line 53

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

#normalize(options) ⇒ Hash[Symbol, untyped]

: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]

Parameters:

  • (Hash[Symbol, untyped])

Returns:

  • (Hash[Symbol, untyped])


73
74
75
76
77
78
79
80
# File 'lib/lightningcss/options.rb', line 73

def normalize(options)
  normalized = options.dup

  normalized[:css_modules] = {} if normalized[:css_modules] == true
  normalized.delete(:css_modules) if normalized[:css_modules] == false

  normalized.compact
end

#to_json(state = nil) ⇒ String

: (?untyped) -> String

Parameters:

  • (Object)

Returns:

  • (String)


48
49
50
# File 'lib/lightningcss/options.rb', line 48

def to_json(state = nil)
  JSON.generate(to_h, state)
end

#validate!(names, allowed, subject) ⇒ void

This method returns an undefined value.

: (Array, Array, String) -> void

Parameters:

  • (Array[Symbol])
  • (Array[Symbol])
  • (String)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lightningcss/options.rb', line 60

def validate!(names, allowed, subject)
  unknown = names - KNOWN

  raise OptionError, "Unknown option#{"s" if unknown.length > 1}: #{unknown.join(", ")}" if unknown.any?

  unsupported = names - allowed

  return if unsupported.empty?

  raise OptionError, "#{unsupported.join(", ")} #{unsupported.one? ? "is not an option" : "are not options"} for #{subject}"
end