Skip to content

Commit 46c3f70

Browse files
committed
Merge pull request #36 from johnthethird/fix_production_config
Fix asset path munging to work in production env.
2 parents 0e12182 + 27e8ce5 commit 46c3f70

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

lib/react/rails/railtie.rb

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,42 @@ module Rails
55
class Railtie < ::Rails::Railtie
66
config.react = ActiveSupport::OrderedOptions.new
77

8+
# Sensible defaults. Can be overridden in application.rb
9+
config.react.variant = (::Rails.env.production? ? :production : :development)
10+
config.react.addons = false
11+
812
# run after all initializers to allow sprockets to pick up react.js and
913
# jsxtransformer.js from end-user to override ours if needed
10-
config.after_initialize do |app|
14+
initializer "react_rails.setup_vendor", :after => "sprockets.environment" do |app|
1115
# Mimic behavior of ember-rails...
1216
# We want to include different files in dev/prod. The unminified builds
1317
# contain console logging for invariants and logging to help catch
1418
# common mistakes. These are all stripped out in the minified build.
15-
if variant = app.config.react.variant || ::Rails.env.test?
16-
variant ||= :development
17-
addons = app.config.react.addons || false
1819

19-
# Copy over the variant into a path that sprockets will pick up.
20-
# We'll always copy to 'react.js' so that no includes need to change.
21-
# We'll also always copy of JSXTransformer.js
22-
tmp_path = app.root.join('tmp/react-rails')
23-
filename = 'react' + (addons ? '-with-addons' : '') + (variant == :production ? '.min.js' : '.js')
24-
FileUtils.mkdir_p(tmp_path)
25-
FileUtils.cp(::React::Source.bundled_path_for(filename),
26-
tmp_path.join('react.js'))
27-
FileUtils.cp(::React::Source.bundled_path_for('JSXTransformer.js'),
28-
tmp_path.join('JSXTransformer.js'))
29-
app.assets.prepend_path tmp_path
20+
# Copy over the variant into a path that sprockets will pick up.
21+
# We'll always copy to 'react.js' so that no includes need to change.
22+
# We'll also always copy of JSXTransformer.js
23+
tmp_path = app.root.join('tmp/react-rails')
24+
filename = 'react' +
25+
(app.config.react.addons ? '-with-addons' : '') +
26+
(app.config.react.variant == :production ? '.min.js' : '.js')
27+
FileUtils.mkdir_p(tmp_path)
28+
FileUtils.cp(::React::Source.bundled_path_for(filename),
29+
tmp_path.join('react.js'))
30+
FileUtils.cp(::React::Source.bundled_path_for('JSXTransformer.js'),
31+
tmp_path.join('JSXTransformer.js'))
32+
app.assets.prepend_path tmp_path
33+
34+
# Allow overriding react files that are not based on environment
35+
# e.g. /vendor/assets/react/JSXTransformer.js
36+
dropin_path = app.root.join("vendor/assets/react")
37+
app.assets.prepend_path dropin_path if dropin_path.exist?
3038

31-
# Allow overriding react files that are not based on environment
32-
# e.g. /vendor/assets/react/JSXTransformer.js
33-
dropin_path = app.root.join("vendor/assets/react")
34-
app.assets.prepend_path dropin_path if dropin_path.exist?
39+
# Allow overriding react files that are based on environment
40+
# e.g. /vendor/assets/react/react.js
41+
dropin_path_env = app.root.join("vendor/assets/react/#{app.config.react.variant}")
42+
app.assets.prepend_path dropin_path_env if dropin_path_env.exist?
3543

36-
# Allow overriding react files that are based on environment
37-
# e.g. /vendor/assets/react/react.js
38-
dropin_path_env = app.root.join("vendor/assets/react/#{variant}")
39-
app.assets.prepend_path dropin_path_env if dropin_path_env.exist?
40-
end
4144
end
4245
end
4346
end

0 commit comments

Comments
 (0)