Skip to content

Switch to the new Sprockets processor #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/react/jsx.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'execjs'
require 'react/jsx/processor'
require 'react/jsx/template'
require 'react/jsx/jsx_transformer'
require 'react/jsx/babel_transformer'
Expand Down
9 changes: 9 additions & 0 deletions lib/react/jsx/processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module React
module JSX
class Processor
def self.call(input)
JSX::transform(input[:data])
end
end
end
end
7 changes: 6 additions & 1 deletion lib/react/rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ module Rails
class Engine < ::Rails::Engine
initializer "react_rails.setup_engine", :group => :all do |app|
sprockets_env = app.assets || Sprockets # Sprockets 3.x expects this in a different place
sprockets_env.register_engine(".jsx", React::JSX::Template)
if Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("3.0.0")
sprockets_env.register_mime_type("application/jsx", extensions: [".jsx", ".js.jsx"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for handling requests with mime-type application/jsx ? Where do those come from?

For react-rails, I think all requests are application/javascript. If that's correct, let's remove this line since we don't need it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only the internal Sprockets mime-type definition. That's the way Sprockets maps extensions with transformers - via mime-types.

register_mime_type arbitrary_mime_type, extensions: list_of_extensions
register_transformer arbitrary_mime_type, target_mime_type, SomeProcessor

https://github.com/rails/sprockets/blob/master/lib/sprockets.rb

Starting version 4 you will no longer be able to map extensions directly to transformers so the mime-type is actually required. We cannot use the existing application/javascript because that's the target mime-type reserved for vanilla JS files.

Their 2->3 upgrade doc even says:
Register a content type for file extension, its okay if its made up

Alternatives to application/jsx would be to:

  • Follow the ERB convention and map JSX files to application/javascript+jsx mime-type similar to the way Sprockets maps .html.erb files to text/html+ruby.
  • Follow the CoffeeScript convention and map JSX files to text/jsx mime-type similar to the way Sprockets maps .js.coffee to text/coffeescript.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation! I see why you chose to do it this way and it sounds good to me!

sprockets_env.register_transformer("application/jsx", "application/javascript", React::JSX::Processor)
else
sprockets_env.register_engine(".jsx", React::JSX::Template)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/react/jsx/jsx_transformer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JSXTransformerTest < ActionDispatch::IntegrationTest
FileUtils.rm replacing_path

assert_response :success
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body.strip
end

test 'accepts harmony: true option' do
Expand Down Expand Up @@ -52,7 +52,7 @@ class JSXTransformerTest < ActionDispatch::IntegrationTest

FileUtils.rm_rf custom_path
assert_response :success
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body
assert_equal 'test_confirmation_token_jsx_transformed;', @response.body.strip
end

end