leshill / handlebars_assets

Use handlebars.js templates with the Rails asset pipeline.
MIT License
649 stars 159 forks source link

Sprockets 4 support #135

Closed tjgrathwell closed 8 years ago

tjgrathwell commented 8 years ago

I was trying to update an app to test out the latest Rails master, and found this gem was incompatible because it doesn't support Sprockets 4.

Apparently, the plugin architecture has changed such that Sprockets now requires you to install extensions like this:

config.assets.configure do |env|
  env.register_mime_type 'text/x-handlebars-template', extensions: Config.handlebars_extensions
  env.register_transformer 'text/x-handlebars-template', 'application/javascript', HandlebarsProcessor
end

Instead of the old way,

sprockets_environment.register_engine('.hbs', HandlebarsTemplate)

Additionally, the thing that gets registered now responds only to call, instead of using the Tilt interface. (See https://github.com/rails/sprockets/blob/da25579535aabf6935facd8c626f6588a90ad9f8/README.md#processor-interface in the Sprockets readme for a little more detail)

This commit attempts to use the new syntax on Sprockets 4 and up, while preserving the old syntax if the gem is used on a Sprockets 3 project.

AlexRiedler commented 8 years ago

@tjgrathwell see any reason not to go ahead and merge this ? How much manual testing have you done ?

tjgrathwell commented 8 years ago

I've tested it on https://github.com/railsbridge/bridge_troll in both the existing Rails 4.2 / Sprockets 3 master branch and this Rails Edge / Sprockets 4 branch. Seems to work?

tjgrathwell commented 8 years ago

This should work for Sprockets 3 as well, see for reference some prior art at https://github.com/reactjs/react-rails/commit/c8ecba2fd799b72ad77dd926bb814eff498b9431 for react-rails. Let me know if you want me to change the PR from Gem::Version.new(Sprockets::VERSION) < Gem::Version.new('4') to Gem::Version.new(Sprockets::VERSION) < Gem::Version.new('3')

AlexRiedler commented 8 years ago

@tjgrathwell merging this, then going to do some testing ; potentially refactor some small stuff before releasing