jonkemp / gulp-useref

Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.
MIT License
705 stars 93 forks source link

No documentation of transform functions. #136

Closed sthames42 closed 8 years ago

sthames42 commented 9 years ago

This seems to be the only way to make a comment but it is not an issue.

First of all, thanks for putting this together. I nearly cracked my skull trying to get usemin to work for me and finally gave up. It was able to handle the first two blocks in the app page but not the rest. Yours handled everything fine.

I had a problem where I could not concatenate before minifyCSS because of @import tags in the CSS files deep in the folder structure. This made the minify fail. There was no way to get the output file name and perform concat() after the minify so I searched your code and found you provide for a transform function BEFORE the concat().

Here is the code I used to solve this:

gulp.task('build-app', function()
  {
  /*-----------------------------------------*/
  /* Transform function called before        */
  /* useref.assets() concats files together. */
  /*-----------------------------------------*/
  var transform = function(name) 
    {
    /*--------------------------------------*/
    /* JS transformer to ng-annotate        */
    /* angularJS scripts and uglify all JS. */
    /*--------------------------------------*/
    var transformjs = lazypipe()
      .pipe(function() { return when(name.indexOf('.ng.js') > 0, ngAnnotate(OPTS_NG_ANNOTATE)); })
      .pipe(uglify);

    return when('*.css', minifyCSS(OPTS_MINIFY_CSS))
     .pipe(when('*.js',  transformjs()));
    };

  var deferred = Q.defer();

  /*--------------------------------------------*/
  /* Combine, minify, rev and write asset files */
  /* to public folder and get the rev manifest. */
  /*--------------------------------------------*/
  var manifest = gulp.src(FP_APP, { base: '.' })
      .pipe(  processhtml(OPTS_PROCESSHTML))
      .pipe(useref.assets({ searchPath: 'public' }, transform))
      .pipe(          rev())
      .pipe(    gulp.dest(path.join(DP_BUILD, 'public')))                  
      .pipe( rev.manifest());

  /*----------------------------------------*/
  /* Modify app file to include minified    */
  /* asset files and write to build folder. */
  /*----------------------------------------*/
  manifest.on('readable', function()
    {
    gulp.src(FP_APP, { base: '.' })
       .pipe(processhtml(OPTS_PROCESSHTML))
       .pipe(     useref())
       .pipe( revReplace({ manifest: manifest, replaceInExtensions: '.php' }))
       .pipe(  gulp.dest(DP_BUILD))
       .on('end', function()
         {
         deferred.resolve();
         });
    });

  return deferred.promise;
  });

Please continue to support the the transform function and maybe put in some documentation. If its there and I missed it, please disregard this notice.

jonkemp commented 9 years ago

Any help with this documentation would be greatly appreciated!

sthames42 commented 9 years ago

I'm happy to try Jon but I'm not really qualified. I've only been working with gulp for a couple of days and will probably miss something important. But I can give it a try.

jonkemp commented 9 years ago

Anybody can write documentation.