Skip to content

Commit fdfec81

Browse files
committed
refactor(Sprockets) extract sprockets mount; use silence_deprecations for 3.7+
1 parent 4f58990 commit fdfec81

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

lib/react/jsx.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'react/jsx/template'
44
require 'react/jsx/jsx_transformer'
55
require 'react/jsx/babel_transformer'
6+
require 'react/jsx/sprockets_strategy'
67
require 'rails'
78

89
module React
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module React
2+
module JSX
3+
# Depending on the Sprockets version,
4+
# attach JSX transformation the the Sprockets environment.
5+
#
6+
# You can override it with `config.sprockets_strategy`
7+
# @example Specifying a Sprockets strategy
8+
# app.config.react.sprockets_strategy = :register_engine
9+
#
10+
# @example Opting out of any Sprockets strategy
11+
# app.config.react.sprockets_strategy = false
12+
#
13+
module SprocketsStrategy
14+
module_function
15+
16+
# @param [Sprockets::Environment] the environment to attach JSX to
17+
# @param [Symbol, Nil] A strategy name, or `nil` to detect a strategy
18+
def attach_with_strategy(sprockets_env, strategy_or_nil)
19+
strategy = strategy_or_nil || detect_strategy
20+
self.public_send(strategy, sprockets_env)
21+
end
22+
23+
# @return [Symbol] based on the environment, return a method name to call with the sprockets environment
24+
def detect_strategy
25+
sprockets_version = Gem::Version.new(Sprockets::VERSION)
26+
if sprockets_version >= Gem::Version.new("4.0.0")
27+
:register_processors
28+
elsif sprockets_version >= Gem::Version.new("3.0.0")
29+
:register_engine_with_mime_type
30+
else
31+
:register_engine
32+
end
33+
end
34+
35+
def register_engine(sprockets_env)
36+
sprockets_env.register_engine(".jsx", React::JSX::Template)
37+
end
38+
39+
def register_engine_with_mime_type(sprockets_env)
40+
sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript", silence_deprecation: true)
41+
end
42+
43+
def register_processors(sprockets_env)
44+
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
45+
sprockets_env.register_mime_type("application/jsx+coffee", extensions: [".jsx.coffee", ".js.jsx.coffee"])
46+
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
47+
sprockets_env.register_transformer("application/jsx+coffee", "application/jsx", Sprockets::CoffeeScriptProcessor)
48+
sprockets_env.register_preprocessor("application/jsx", Sprockets::DirectiveProcessor.new(comments: ["//", ["/*", "*/"]]))
49+
sprockets_env.register_preprocessor("application/jsx+coffee", Sprockets::DirectiveProcessor.new(comments: ["#", ["###", "###"]]))
50+
end
51+
end
52+
end
53+
end

lib/react/rails/railtie.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Railtie < ::Rails::Railtie
1010
config.react.jsx_transform_options = {}
1111
config.react.jsx_transformer_class = nil # defaults to BabelTransformer
1212
config.react.camelize_props = false # pass in an underscored hash but get a camelized hash
13+
config.react.sprockets_strategy = nil # how to attach JSX to the asset pipeline (or `false` for none)
1314

1415
# Server rendering:
1516
config.react.server_renderer_pool_size = 1 # increase if you're on JRuby
@@ -97,22 +98,13 @@ class Railtie < ::Rails::Railtie
9798
end
9899

99100
initializer "react_rails.setup_engine", :group => :all do |app|
100-
# Sprockets 3.x expects this in a different place
101-
sprockets_env = app.assets || defined?(Sprockets) && Sprockets
101+
if app.config.react.sprockets_strategy == false
102+
# pass, sprockets opt-out
103+
else
104+
# Sprockets 3.x expects this in a different place
105+
sprockets_env = app.assets || defined?(Sprockets) && Sprockets
102106

103-
if !sprockets_env.nil?
104-
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.7.0")
105-
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
106-
sprockets_env.register_mime_type("application/jsx+coffee", extensions: [".jsx.coffee", ".js.jsx.coffee"])
107-
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
108-
sprockets_env.register_transformer("application/jsx+coffee", "application/jsx", Sprockets::CoffeeScriptProcessor)
109-
sprockets_env.register_preprocessor("application/jsx", Sprockets::DirectiveProcessor.new(comments: ["//", ["/*", "*/"]]))
110-
sprockets_env.register_preprocessor("application/jsx+coffee", Sprockets::DirectiveProcessor.new(comments: ["#", ["###", "###"]]))
111-
elsif Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
112-
sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript")
113-
else
114-
sprockets_env.register_engine(".jsx", React::JSX::Template)
115-
end
107+
React::JSX::SprocketsStrategy.attach_with_strategy(sprockets_env, app.config.react.sprockets_strategy)
116108
end
117109
end
118110
end

0 commit comments

Comments
 (0)