简体   繁体   English

Gulp uglify 因 js 解析错误而失败

[英]Gulp uglify fails with js parse error

I'm strugling over a very annoying error.我正在为一个非常烦人的错误而苦苦挣扎。 First at all: I'm new to gulp.首先:我是 gulp 的新手。

I try to realise a single page application with Angular.我尝试使用 Angular 实现单页应用程序。 To make my workflow better, I tried to implement gulp.为了使我的工作流程更好,我尝试实现 gulp。

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.我在安装一些 gulp 插件时遇到了一些严重的 npm 错误,但这是另一个故事,我意识到它可以正常工作,也可以没有错误。

What I'm actualy hit my head on is the useref plugin.我真正想到的是 useref 插件。 Or more exact, the uglify plugin.或者更准确地说,是 uglify 插件。

This is in my gulpfile.js:这是在我的 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'))
});

An this is the stacktrace it throws when trying to run it.这是它在尝试运行时抛出的堆栈跟踪。 C:\\Work\\evori-portfolio>gulp useref C:\\Work\\evori-portfolio>gulp userref

[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 <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:1526:18)
    at js_error (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:1534:11)
    at croak (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2025:9)
    at token_error (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2033:9)
    at expect_token (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2046:9)
    at expect (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2049:36)
    at eval (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2602:13)
    at eval (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2072:24)
    at expr_atom (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2532:35)
    at maybe_unary (eval at <anonymous> (C:\Work\evori-portfolio\node_modules\uglify-js\tools\node.js:22:1), <anonymous>:2708:19)

This stacktraces says absolutely nothing to me.这个堆栈跟踪对我来说绝对没有任何意义。

I watched at all of the destinations listed in the stacktrace and didn't found any failure.我查看了堆栈跟踪中列出的所有目的地,但没有发现任何故障。

I thought it could be caused by asynchrounus work that gulp does.我认为这可能是由 gulp 所做的异步工作引起的。 Probably the uglify plugin tries to uglify the at this moment not finished useref file.可能是 uglify 插件试图将此时未完成的 useref 文件丑化。 How knows?怎么知道?

So I seperatet everything to be sure the error is not caused by overlapping plugins.因此,我将所有内容分开以确保错误不是由重叠插件引起的。

This is how i went through to reproduce the error: 1. gulp clean:dist这是我重现错误的方式:1. gulp clean:dist

gulp.task('clean:dist', function() {
  return del.sync('dist')
});
  1. gulp useref吞下用户引用

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

  2. gulp minify吞咽缩小

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

  3. going crazy about the stack trace对堆栈跟踪感到疯狂

    C:\\Work\\evori-portfolio>gulp minify [11:55:50] Using gulpfile C:\\Work\\evori-portfolio\\gulpfile.js [11:55:50] Starting 'minify'...

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

EDIT OK... I've got in my index.html file a huge load of .js-Files.编辑好的...我在我的 index.html 文件中有大量的 .js 文件。 I tried to get that file, that causes the error by removing each and try to run useref+minify.我试图获取该文件,通过删除每个文件并尝试运行 useref+minify 导致错误。 And I got it.我明白了。 The bad file.坏文件。 When it is removed from index.html, minify works like a charm.当它从 index.html 中删除时,minify 就像一个魅力。 But I have no idea, what I'm doing wrong or could cause an error.但我不知道我做错了什么或可能导致错误。

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

In case someone faces the same issue i Suggest to modify the gulp code to check what the file causes the problem.如果有人遇到同样的问题,我建议修改 gulp 代码以检查文件导致问题的原因。

go to node_modules/gulp-uglify/minifier.js find the function : var mangled = trycatch(function () {} before return write console.log(file.path); save the file then try again, you will see the files which is processingnode_modules/gulp-uglify/minifier.js找到函数: var mangled = trycatch(function () {} before return write console.log(file.path);保存文件然后再试一次,你会看到文件正在处理

the last file path appears in the terminal before the error is the file which has the error.错误前终端中出现的最后一个文件路径是出现错误的文件。

Ok, looks like I found the statement where it fails.好的,看起来我找到了失败的声明。

I don't understand why but it works when I remove the statements in the return object of dienstleisterRegObjService.js:我不明白为什么,但是当我删除 dienstleisterRegObjService.js 的返回对象中的语句时它会起作用:

vorselektiertesProdukt,
regObj,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM