ericclemmons / grunt-angular-templates

Grunt build task to concatenate & pre-load your AngularJS templates
MIT License
710 stars 107 forks source link

Add usemin option #63

Closed ericclemmons closed 10 years ago

ericclemmons commented 10 years ago

From @pheuter:

@ericclemmons In my case, I am generating two javascript files, vendor.js and application.js. When concatenating ng-templates, I need to append solely to application.js. The error I was running into was due to the templates file being concatenated to both files and that caused errors.

// build:js(.) javascripts/vendor.js

// build:js(.) javascripts/application.js

In which case, the jump to 0.5.0 may not be warranted (whoops!), because this solution can let someone specify usemin: 'javascripts/application.js' to target that specific dest.

concat is useful if you have a specific concat task, but trying to target what usemin generates for concat is annoying at best.

pheuter commented 10 years ago

@ericclemmons I'm getting the following error now:

Running "ngtemplates:dist" (ngtemplates) task
File resources/views.js created.
>> Concat target not found: generated.files[3]
pheuter commented 10 years ago

@ericclemmons nevermind, that was due to a leftover concat property

pheuter commented 10 years ago

@ericclemmons Not getting any errors now, but it doesn't seem like it's actually getting concatenated to application.js

marcalj commented 10 years ago

@pheuter I assume @ericclemmons have to fix this issue first to make it work :)

paulyoung commented 10 years ago

I'm confused about the status of this issue. It appears that @ericclemmons fixed something in #44 which should have fixed @pheuter's use case.

@pheuter - are you saying that it doesn't?

ericclemmons commented 10 years ago

I attempted to fix it in #44, but there was another edge case to be resolved here ;) The good news is, this will be BC-compatible with 0.4.x

szimek commented 10 years ago

I'm currently trying to set it up using v0.5.0 and I'm getting Concat target not found: scripts/scripts.js error. I guess I should just wait for usemin option then? :)

ericclemmons commented 10 years ago

Wait, everyone just wait! :) Go to #63 now. concat works with legit concat targets, but usemin screwed the pooch with how they're doing things now. =/

marcalj commented 10 years ago

tl;dr If you just create a sigle js file with usemin use generated as concat target.

@szimek You have to use generated as concat target:

ngtemplates:  {
      dist: {
        cwd: '<%= yeoman.app %>',
        src: 'views/**.html',
        dest: '<%= yeoman.dist %>/scripts/templates.js',
        options: {
          module: 'app', // Can replace ngtemplates.dist with ngtemplates.app
          concat: 'generated',
//          htmlmin:  { collapseWhitespace: true, collapseBooleanAttributes: true },
          htmlmin: {}
        }
      }
    },

But this will concat templates for all js groups.

szimek commented 10 years ago

@marcalj Thanks, but unfortunately I've got 2 js groups... Also I've just noticed that I've got rev task running after ngtemplates...

ericclemmons commented 10 years ago

Yep, let's wait for this issue to get fixed, then. Everyone be sure to thank grunt-usemin for changing how it builds concat targets now!

szimek commented 10 years ago

Here's a small snippet that adds usemin option:

if (options.usemin) {
  var _ = grunt.util._;
  var config = grunt.config('concat');
  var prefix = '.tmp/concat/';

  var files = _.find(config.generated.files, function (item) {
    return item.dest === prefix + options.usemin;
  });

  files.src.push(file.dest);

  grunt.config('concat', {
    generated: { files: config.generated.files }
  });
}

It doesn't handle errors (i.e. invalid path to destination file in concat configuration) and I'm not sure how to read prefix path from usemin configuration, but it works. It doesn't solve the issue that usemin updates paths in HTML files to files renamed by rev after these HTML files are processed by ngtemplates.

Here's commit with these changes: https://github.com/szimek/grunt-angular-templates/commit/1235cd40f0f5a2baa87ae666cf2d9eb1368bfc66

ericclemmons commented 10 years ago

Just released v0.4.10 & v0.5.1 to be effectively the same thing, which adds a usemin option. Check out the updated README & release!

danielmcormond commented 10 years ago

@ericclemmons Thanks Eric! Much appreciated. :+1:

marcalj commented 10 years ago

@ericclemmons Works perfectly! Excellent job! :)

orlando commented 10 years ago

I'll leave this here in case someone is looking into how make grunt-rev work with grunt-angular-templates

https://github.com/yeoman/grunt-usemin/issues/235#issuecomment-33266949