Skip to content

Commit bb7cb49

Browse files
guzartrmosolgo
authored andcommitted
Register with sprockets when sprockets is available
1 parent 20c8e9d commit bb7cb49

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

lib/react/rails/railtie.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ class Railtie < ::Rails::Railtie
5959
addons: app.config.react.addons,
6060
})
6161

62-
sprockets_env = app.assets || app.config.assets # sprockets-rails 3.x attaches this at a different config
63-
sprockets_env.version = [sprockets_env.version, "react-#{asset_variant.react_build}",].compact.join('-')
62+
sprockets_env = app.assets || app.config.try(:assets) # sprockets-rails 3.x attaches this at a different config
63+
if !sprockets_env.nil?
64+
sprockets_env.version = [sprockets_env.version, "react-#{asset_variant.react_build}",].compact.join('-')
65+
end
6466

6567
end
6668

@@ -70,8 +72,10 @@ class Railtie < ::Rails::Railtie
7072
addons: app.config.react.addons,
7173
})
7274

73-
app.config.assets.paths << asset_variant.react_directory
74-
app.config.assets.paths << asset_variant.jsx_directory
75+
if app.config.respond_to?(:assets)
76+
app.config.assets.paths << asset_variant.react_directory
77+
app.config.assets.paths << asset_variant.jsx_directory
78+
end
7579
end
7680

7781
config.after_initialize do |app|
@@ -93,14 +97,18 @@ class Railtie < ::Rails::Railtie
9397
end
9498

9599
initializer "react_rails.setup_engine", :group => :all do |app|
96-
sprockets_env = app.assets || Sprockets # Sprockets 3.x expects this in a different place
97-
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("4.x")
98-
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
99-
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
100-
elsif Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
101-
sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript")
102-
else
103-
sprockets_env.register_engine(".jsx", React::JSX::Template)
100+
# Sprockets 3.x expects this in a different place
101+
sprockets_env = app.assets || defined?(Sprockets) && Sprockets
102+
103+
if !sprockets_env.nil?
104+
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("4.x")
105+
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx", ".es.jsx", ".es6.jsx"])
106+
sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
107+
elsif Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
108+
sprockets_env.register_engine(".jsx", React::JSX::Processor, mime_type: "application/javascript")
109+
else
110+
sprockets_env.register_engine(".jsx", React::JSX::Template)
111+
end
104112
end
105113
end
106114
end

0 commit comments

Comments
 (0)