Created
October 14, 2016 19:52
-
-
Save tonydewan/7929defe3ef4b7c91cce61f144c0897d to your computer and use it in GitHub Desktop.
Simple SprocketsCoffeeReact replacement for Sprockets 3.0+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/sprockets/coffee_react.rb | |
require "sprockets" | |
require "coffee-react" | |
require "coffee-script" | |
module Sprockets | |
# Transforms cjsx to javascript | |
module CoffeeReactTransformer | |
def self.call(input) | |
coffeescript = CoffeeReact.transform(input[:data]) | |
javascript = CoffeeScript.compile(coffeescript) | |
{ data: javascript } | |
end | |
end | |
# Postprocessing adds displayname to components | |
module CoffeeReactPostprocessor | |
def self.call(input) | |
{ data: CoffeeReact.jstransform(input[:data]) } | |
end | |
end | |
end | |
Sprockets.register_mime_type 'text/x-cjsx', extensions: ['.js.cjsx', '.js.coffee.cjsx'] | |
Sprockets.register_transformer 'text/x-cjsx', 'application/javascript', Sprockets::CoffeeReactTransformer | |
Sprockets.register_postprocessor 'application/javascript', Sprockets::CoffeeReactPostprocessor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "coffee-react" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initializers/sprockets_extensions.rb | |
Dir[Rails.root + 'lib/sprockets/*.rb'].each do |file| | |
require file | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment