Skip to content
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

Make angular-rails-templates work with Sprockets 3.0 #93

Closed
jonricaurte opened this issue Apr 8, 2015 · 55 comments
Closed

Make angular-rails-templates work with Sprockets 3.0 #93

jonricaurte opened this issue Apr 8, 2015 · 55 comments

Comments

@jonricaurte
Copy link

The new version of sprockets will require angular-rails-templates to use transformers instead of what it is currently using according to @rafaelfranca:

rails/sprockets#22

Sprockets 3.0.0 includes support for babel transpiling using the es6 gem which is going to be very important as angular moves towards using ecmascript 6.

Thanks.

@rafaelfranca
Copy link

👍 Let me know if you need help with the new API.

@jonricaurte
Copy link
Author

To get unblocked, here are some sample files until it gets merged in to angular-rails-templates:

https://gist.github.com/stevenharman/8493700

http://minhajuddin.com/2013/04/28/angularjs-templates-and-rails-with-eager-loading

@jonricaurte
Copy link
Author

Note: this doesn't watch for changes

@jonricaurte
Copy link
Author

@rafaelfranca With the temporary solution I have to have rails c open in a tab and have to delete temp assets every time I make a slim change. Is there a better way around this than listed where I don't have to delete the temp assets? Thanks!

@jonricaurte
Copy link
Author

So if you don't want to have to use rails c and Rails.cache.clear, you can run guard and have this in your guard file:

guard 'rake', :task => 'tmp:clear' do
watch(%r{^app/.+.(slim)})
end

and have this in your gem file

gem 'guard'
gem 'guard-compat'
gem 'guard-rake'

@tobidelius
Copy link

tmp:clear is annoying like h*ll :/

@styx
Copy link

styx commented Apr 17, 2015

/CC

@asciant
Copy link

asciant commented Apr 22, 2015

+1

1 similar comment
@bmulholland
Copy link

+1

@gottfrois
Copy link

Had to downgrade to version 2.x as well, would be great to see that out and working :)

@panzershreck
Copy link

+1

@ehahn9
Copy link

ehahn9 commented Apr 26, 2015

+1 (to use sprockets transformers instead)

@pitr
Copy link
Owner

pitr commented Apr 28, 2015

Thanks @davetron5000 for PR and everybody else for suggestions.

Did anybody manage to get this gem working with sprockets transformers?

@tobidelius
Copy link

any update on this? At the moment I'm stuck with:

angular.module('templates', []).run(['$templateCache', function($templateCache) {
  <%
    environment.context_class.instance_eval { include ActionView::Helpers::JavaScriptHelper }
    app_root  = File.expand_path('../../', __FILE__)
    templates = File.join(app_root, %w{templates ** *.html})

    Dir.glob(templates).each do |f|
      key  = f.gsub(%r(^#{app_root}/templates/), '')
      html = File.read(f).squish
  %>
    $templateCache.put("<%= key %>", "<%= escape_javascript(html) %>");
<% end %>
}]);

but I have to run rake tmp:clear everytime I make changes to the templates.

@sars
Copy link

sars commented May 27, 2015

any updates regarding support sprockets 3?

@jaybloke
Copy link

+1

@mcasimir
Copy link

mcasimir commented Jun 8, 2015

Having issues to both use Sprockets 3 and downgrade I ended up getting rid of the gem completely and solved it like that (hope this will help maintainers to fix the issue if it is not done yet):

1 - Add gem 'listen', '~> 2.7' to Gemfile

2 - Create config/initializers/angular_templates.rb like this:

require 'fileutils'

if Rails.env.development?
  cache_path = Rails.root.join('tmp/cache/assets/development')
  FileUtils.rm_rf(cache_path)

  listener = Listen.to(Rails.root.join('app/assets/templates')) do |modified, added, removed|
    # clearing cache
    FileUtils.rm_rf(cache_path)
  end
  listener.start
end

3 - Create app/assets/javascripts/templates.js.erb:

(function(){
  'use strict';

  angular.module('templates', []).run(['$templateCache', function($templateCache) {
    <%
      environment.context_class.instance_eval { include ActionView::Helpers::JavaScriptHelper }
      app_root  = File.expand_path('../../', __FILE__)
      templates = File.join(app_root, %w{templates ** *.html})

      Dir.glob(templates).each do |f|
        key  = f.gsub(%r(^#{app_root}/templates/), '')
        html = File.read(f).squish
    %>
      $templateCache.put("<%= key %>", "<%= escape_javascript(html) %>");
  <% end %>
  }]);
}());

Done.. now just require your templates in application.js or whatever and they will be complied and included as expected (no need to start guard, no need to run rake tmp:cache):

//= require angular
// ...
//= require ./templates
// ...
angular.module('myApp', ['templates']);

@turlockmike
Copy link

+1

6 similar comments
@Rodeoclash
Copy link

+1

@olegantonyan
Copy link

+1

@CompuIves
Copy link

+1

@lexi-lambda
Copy link

+1

@thontaddeo
Copy link

+1

@GarPit
Copy link

GarPit commented Jul 9, 2015

👍

@aidanhmiles
Copy link

@mcasimir thanks a million for the sample setup! works great. for anyone else using his fix, it expects templates to be placed in app/assets/templates. you may need to tweak for your own setup.

@bopm
Copy link

bopm commented Jul 10, 2015

👍

@mvarrieur
Copy link

@jonricaurte Is there a gulp workflow that will compile .html files into the angular $templateCache like this gem was doing? I'd be interested in learning about it.

@jonricaurte
Copy link
Author

@mvarrieur yes.

Here is what I use for gulp file:

gulp.task('slim', function(){
  return gulp.src('app/assets/javascripts/**/*.slim')
    .pipe(slm())
    .pipe(rename(function (path) {
      path.dirname = path.dirname.replace('beauty/', '');
      path.extname = '';
    }))
    .pipe(minifyHtml({
      empty: true,
      spare: true,
      quotes: true
    }))
    .pipe(ngHtml2Js({
      moduleName: 'templates'
    }))
    .pipe(concat('templates.js'))
    .pipe(gulp.dest('app/assets/javascripts/vendor'));


    // sfx build
    //.pipe(gulp.dest('public/assets'));
});

using gulp-rename, slim, gulp-ng-html2js, gulp-minify-html, gulp-concat, gulp-rename

@hendricius
Copy link

@jonricaurte how do you integrate this with your rails workflow? a quick example would be great :). thanks

jmoon90 added a commit to jmoon90/recipe-angular that referenced this issue Oct 6, 2015
@mikeover
Copy link

mikeover commented Oct 8, 2015

+1

2 similar comments
@courtness
Copy link

+1

@iagopiimenta
Copy link

👍

@ghost
Copy link

ghost commented Nov 5, 2015

A note in the README regarding this issue (until it is resolved) would help save a lot of time

@julianorinyol
Copy link

+1

@mikeborg
Copy link

Might as well try to fix it...running into issues implementing register_transformer. Any help would be appreciated. Thanks.

How to intercept '.html' requests in Rails using Sprockets >3.0 Transformers? (ie fix 'angular-rails-templates' gem) http://stackoverflow.com/questions/33701654/how-to-intercept-html-requests-in-rails-using-sprockets-3-0-transformers-i

@Saicheg
Copy link

Saicheg commented Nov 17, 2015

+1 to get this fixed

@evgeny-myasishchev
Copy link

+1

1 similar comment
@bramski
Copy link

bramski commented Nov 30, 2015

+1

@rafaelfranca
Copy link

The @superchris's fork has the proper fix to this issue. It can be improved to not have to rename templates to nghtml but that is the right direction.

@paul-ihnatolia
Copy link

@superchris are you going to release your solution as a separate gem and add the support of slim and other preprocessors? I think it will be a perfect solution, because it seems that current gem is not going to bring a support of sprockets 3.+.

Chryus added a commit to Chryus/watership-down that referenced this issue Dec 15, 2015
Angular on Rails 4.2.3 app, setup with bower-rails + angular-rails-templates. This app uses sprockets v2.12.3, as angular-rails-templates won't support v3. As a workaround you can delete angular-rails-templates and follow these steps: pitr/angular-rails-templates#93 (comment). This app will move to this format next.
@ghost
Copy link

ghost commented Dec 24, 2015

  1. Add 'angular-rails-templates', '0.2.0'
  2. Bundle install
  3. Bundle update (Resolving dependencies, sprockets 2.12.4 (was 3.5.2), sprockets-rails 2.3.3 (was 3.0.0))
  4. It works

@m2omou
Copy link

m2omou commented Jan 20, 2016

+1

@pitr
Copy link
Owner

pitr commented Feb 3, 2016

Thanks everyone for encouragement, and especially thanks to work by @superchris I was able to take his branch and adapt it so that the gem works like it used to with sprocket 2, eg. no need to rename templates to .nghaml (see #126)

I am releasing version 1.0.0.beta1 which supports Sprocket 3 (Rails 4.2) and in about a week 1.0.0

@bmulholland
Copy link

@pitr Thanks for your work on this! Much appreciated.

@pitr
Copy link
Owner

pitr commented Feb 11, 2016

Version v1.0.0 is out, supporting both Rails 4.2 and 5.0

@ericdegboe
Copy link

Version v1.0.0 It's work! Very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests