Gulp uglify fails with js parse error
<p>I'm strugling over a very annoying error. First at all: I'm new to gulp.</p>

<p>I try to realise a single page application with Angular. To make my workflow better, I tried to implement gulp. </p>

<p>I was heading some serious npm errors while installing some gulp plugins, but this is another story and i realised, that it's working with or without errors. </p>

<p>What I'm actualy hit my head on is the useref plugin. Or more exact, the uglify plugin. </p>

<p>This is in my gulpfile.js:</p>

<pre><code>gulp.task('useref', function() {
  return gulp.src('app/*.html')
    .pipe(useref())
    .pipe(gulpIf('*.js', uglify()))
    .pipe(gulpIf('*.css', cssnano()))
    .pipe(gulp.dest('dist'))
});
</code></pre>

<p>An this is the stacktrace it throws when trying to run it. 
C:\Work\evori-portfolio>gulp useref</p>

<pre><code>[16:32:40] Using gulpfile C:\Work\evori-portfolio\gulpfile.js
[16:32:40] Starting 'useref'...

events.js:141
      throw er; // Unhandled 'error' event
      ^
 Error
    at new JS_Parse_Error (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:1526:18)
    at js_error (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:1534:11)
    at croak (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2025:9)
    at token_error (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2033:9)
    at expect_token (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2046:9)
    at expect (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2049:36)
    at eval (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2602:13)
    at eval (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2072:24)
    at expr_atom (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2532:35)
    at maybe_unary (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2708:19)
</code></pre>

<p>This stacktraces says absolutely nothing to me.</p>

<p>I watched at all of the destinations listed in the stacktrace and didn't found any failure. </p>

<p>I thought it could be caused by asynchrounus work that gulp does. Probably the uglify plugin tries to uglify the at this moment not finished useref file. How knows?</p>

<p>So I seperatet everything to be sure the error is not caused by overlapping plugins.</p>

<p>This is how i went through to reproduce the error:
1. gulp clean:dist</p>

<pre><code>gulp.task('clean:dist', function() {
  return del.sync('dist')
});
</code></pre>

<ol start="2">
<li><p>gulp useref</p>

<p><code>gulp.task('useref', function() {
  return gulp.src('app/*.html')
    .pipe(useref())
    .pipe(gulp.dest('dist'))
});</code></p></li>
<li><p>gulp minify</p>

<p><code>gulp.task('minify', function() {
  return gulp.src('dist/**/*.js')
    /*.pipe(gulpIf('*.css', cssnano()))*/
    .pipe(uglify())
    .pipe(gulp.dest('dist'))
});</code></p></li>
<li><p>going crazy about the stack trace</p>

<p><code>C:\Work\evori-portfolio&gt;gulp minify
[11:55:50] Using gulpfile C:\Work\evori-portfolio\gulpfile.js
[11:55:50] Starting 'minify'...</code></p>

<p><code>events.js:141
      throw er; // Unhandled 'error' event
      ^
 Error
    at new JS_Parse_Error (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:1526:18)
    at js_error (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:1534:11)
    at croak (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2025:9)
    at token_error (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2033:9)
    at expect_token (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2046:9)
    at expect (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2049:36)
    at eval (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2602:13)
    at eval (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2072:24)
    at expr_atom (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2532:35)
    at maybe_unary (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2708:19)
    at expr_ops (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2743:24)
    at maybe_conditional (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2748:20)
    at maybe_assign (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2772:20)
    at expression (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2791:20)
    at eval (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2173:39)
    at eval (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2072:24)
    at block_ (eval at &lt;anonymous&gt; (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), &lt;anonymous&gt;:2352:20)</code></p></li>
</ol>

<p><strong>EDIT</strong> OK... I've got in my index.html file a huge load of .js-Files. I tried to get that file, that causes the error by removing each and try to run useref+minify. And I got it. The bad file. When it is removed from index.html, minify works like a charm. But I have no idea, what I'm doing wrong or could cause an error. </p>

<pre><code>'use strict';

angular.module('previewApp')
  .factory('dienstleisterRegObjService', function() {

    /* Aktuell ausgewähltes Produkt */
    var vorselektiertesProdukt = {
      servicetyp: '',
      dienstleistungstyp: '',
      produkt: ''
    };

    /* Registrations Objekt; wird per API an Server übergeben */
    var regObj = {
      organisation: '',
      vorname: '',
      nachname: '',
      mail: '',
      nationalitaet: 'Schweiz',
      sprache: 'Deutsch',
      produkte: []
    };

    /* Reset Methods */
    var getNewVorSelektProd = function() {
      vorselektiertesProdukt.servicetyp = '';
      vorselektiertesProdukt.dienstleistungstyp = '';
      vorselektiertesProdukt.produkt = '';
    };

    var getNewRegObj = function() {
      regObj.organisation = '';
      regObj.vorname = '';
      regObj.nachname = '';
      regObj.mail = '';
      regObj.nationalitaet = 'Schweiz';
      regObj.sprache = 'Deutsch';
      regObj.produkte = [];
    };

    /* Service Objekt */
    return {
      vorselektiertesProdukt,
      regObj,
      addProduct: function(servicetyp, dienstleistungstyp, produktparam) {
          var produkt = {};
          produkt.servicetyp = servicetyp;
          produkt.dienstleistungstyp = dienstleistungstyp;
          produkt.produkt = produktparam;
          regObj.produkte.push(produkt);
      },
      deleteProduct: function(produkt) {
        var index = regObj.produkte.indexOf(produkt);
        regObj.produkte.splice(index, 1);
      },
      resetVorSelektProd: function() {
        getNewVorSelektProd();
      },
      resetRegObj: function() {
        getNewRegObj();
      },
      resetAllObj: function() {
        getNewVorSelektProd();
        getNewRegObj();
      }
    };
  });
</code></pre>