Module: React::JSX::SprocketsStrategy

Defined in:
lib/react/jsx/sprockets_strategy.rb

Overview

Depending on the Sprockets version, attach JSX transformation the the Sprockets environment.

You can override it with ‘config.sprockets_strategy`

Examples:

Specifying a Sprockets strategy

app.config.react.sprockets_strategy = :register_engine

Opting out of any Sprockets strategy

app.config.react.sprockets_strategy = false

Class Method Summary collapse

Class Method Details

.attach_with_strategy(sprockets_env, strategy_or_nil) ⇒ Object

Parameters:

  • the (Sprockets::Environment)

    environment to attach JSX to

  • A (Symbol, Nil)

    strategy name, or ‘nil` to detect a strategy



18
19
20
21
# File 'lib/react/jsx/sprockets_strategy.rb', line 18

def attach_with_strategy(sprockets_env, strategy_or_nil)
  strategy = strategy_or_nil || detect_strategy
  self.public_send(strategy, sprockets_env)
end

.detect_strategySymbol

Returns based on the environment, return a method name to call with the sprockets environment.

Returns:

  • (Symbol)

    based on the environment, return a method name to call with the sprockets environment



24
25
26
27
28
29
30
31
32
33
# File 'lib/react/jsx/sprockets_strategy.rb', line 24

def detect_strategy
  sprockets_version = Gem::Version.new(Sprockets::VERSION)
  if sprockets_version >= Gem::Version.new("4.x")
    :register_processors
  elsif sprockets_version >= Gem::Version.new("3.0.0")
    :register_engine_with_mime_type
  else
    :register_engine
  end
end

.register_engine(sprockets_env) ⇒ Object



35
36
37
# File 'lib/react/jsx/sprockets_strategy.rb', line 35

def register_engine(sprockets_env)
  sprockets_env.register_engine(".jsx", React::JSX::Template)
end

.register_engine_with_mime_type(sprockets_env) ⇒ Object



39
40
41
# File 'lib/react/jsx/sprockets_strategy.rb', line 39

def register_engine_with_mime_type(sprockets_env)
  sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript", silence_deprecation: true)
end

.register_processors(sprockets_env) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/react/jsx/sprockets_strategy.rb', line 43

def register_processors(sprockets_env)
  sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
  sprockets_env.register_mime_type("application/jsx+coffee", extensions: [".jsx.coffee", ".js.jsx.coffee"])
  sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
  sprockets_env.register_transformer("application/jsx+coffee", "application/jsx", Sprockets::CoffeeScriptProcessor)
  sprockets_env.register_preprocessor("application/jsx", Sprockets::DirectiveProcessor.new(comments: ["//", ["/*", "*/"]]))
  sprockets_env.register_preprocessor("application/jsx+coffee", Sprockets::DirectiveProcessor.new(comments: ["#", ["###", "###"]]))
end