Gulp uglify 失败并显示 js 解析错误



我正在为一个非常烦人的错误而苦苦挣扎。首先:我是大口大口的新手。

我尝试使用Angular实现单页应用程序。为了使我的工作流程更好,我尝试实现gulp。

在安装一些gulp插件时,我遇到了一些严重的npm错误,但这是另一个故事,我意识到,它有或没有错误都可以工作。

我实际上碰到的是useref插件。或者更准确地说,丑陋的插件。

这是我的gulpfile.js:

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

这是它在尝试运行它时抛出的堆栈跟踪。C:\Work\evori-portfolio>gulp useref

[16:32:40] Using gulpfile C:Workevori-portfoliogulpfile.js
[16:32:40] Starting 'useref'...
events.js:141
      throw er; // Unhandled 'error' event
      ^
 Error
    at new JS_Parse_Error (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:1526:18)
    at js_error (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:1534:11)
    at croak (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2025:9)
    at token_error (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2033:9)
    at expect_token (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2046:9)
    at expect (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2049:36)
    at eval (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2602:13)
    at eval (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2072:24)
    at expr_atom (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2532:35)
    at maybe_unary (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2708:19)

这个堆栈跟踪对我来说绝对没有任何意义。

我查看了堆栈跟踪中列出的所有目的地,没有发现任何故障。

我认为这可能是由吞咽所做的异步工作引起的。可能丑陋的插件试图丑化目前尚未完成的useref文件。怎么知道?

所以我将所有内容分开,以确保错误不是由重叠的插件引起的。

这就是我重现错误的方式:1.大口大口地清洁:区

gulp.task('clean:dist', function() {
  return del.sync('dist')
});
  1. 咕噜咕噜咕��

    gulp.task('useref', function() { return gulp.src('app/*.html') .pipe(useref()) .pipe(gulp.dest('dist')) });

  2. 噜咕噜缩小

    gulp.task('minify', function() { return gulp.src('dist/**/*.js') /*.pipe(gulpIf('*.css', cssnano()))*/ .pipe(uglify()) .pipe(gulp.dest('dist')) });

  3. 对堆栈跟踪发疯

    C:Workevori-portfolio>gulp minify [11:55:50] Using gulpfile C:Workevori-portfoliogulpfile.js [11:55:50] Starting 'minify'...

    events.js:141 throw er; // Unhandled 'error' event ^ Error at new JS_Parse_Error (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:1526:18) at js_error (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:1534:11) at croak (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2025:9) at token_error (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2033:9) at expect_token (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2046:9) at expect (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2049:36) at eval (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2602:13) at eval (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2072:24) at expr_atom (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2532:35) at maybe_unary (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2708:19) at expr_ops (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2743:24) at maybe_conditional (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2748:20) at maybe_assign (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2772:20) at expression (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2791:20) at eval (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2173:39) at eval (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2072:24) at block_ (eval at <anonymous> (C:Workevori-portfolionode_modulesuglify-jstoolsnode.js:22:1), <anonymous>:2352:20)

编辑确定...我的索引中有.html文件有大量的.js文件。我试图获取该文件,该文件通过删除每个文件并尝试运行 useref+minify 来导致错误。我明白了。错误文件。当它从索引中删除时.html,缩小就像一个魅力。但我不知道,我做错了什么或可能导致错误。

'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();
      }
    };
  });

如果有人遇到同样的问题,我建议修改gulp代码以检查导致问题的文件。

转到node_modules/gulp-uglify/minifier.js找到函数:var mangled = trycatch(function () {}在返回之前写入console.log(file.path);保存文件然后重试,您将看到正在处理的文件

在错误出现之前,终端中出现的最后一个文件路径是出现错误的文件。

好的,看起来我找到了失败的语句。

我不明白为什么,但是当我删除dienstleisterRegObjService的返回对象中的语句时,它可以工作.js:

vorselektiertesProdukt,
regObj,

相关内容

  • 没有找到相关文章

最新更新