joshwhatk
4/20/2016 - 8:24 PM

Compile Angular Views in Laravel Elixir

Compile Angular Views in Laravel Elixir

// Installation
// - Place this file in the same folder as your gulpfile
// - Add this to your gulpfile:
// require('./elixir-extensions');
// - It can be used like this:
// mix.views('views/**/*.html', elixir.config.assetsPath + 'js/compiled', {
//   module: 'app',
//   root: 'views/'
// });

var Elixir = require('laravel-elixir');
var Task = Elixir.Task;
var gulp = require('gulp');
var templateCache = require('gulp-angular-templatecache');
var _ = require('underscore');


Elixir.extend('views', function(src, output, options)
{
  src = Elixir.config.publicPath + '/' + src;

  var templateOptions = _.extend({
    root: 'views/'
  }, options);

  //-- compile the angular templates
  new Task('views', function ()
  {
    this.log(src, output + '/templates.js');

    return gulp.src(src)
      .pipe(templateCache(templateOptions))
      .pipe(gulp.dest(output))
      .on('error', function(e) {
          new Elixir.Notification().error(e, 'View Compilation Failed!');
          this.emit('end');
      })
      .pipe(new Elixir.Notification('Views Compiled!'));
  })
  .watch(src);
});