2017-06-16 65 views
0

我正在研究HTML/JS/CSS網站,並使用Gulp作爲自動化工具。Gulp - 使用gulp-uglify造成的事件錯誤

它運行gulp serve當工作完全正常,但運行gulp build當我收到以下錯誤:

[18:24:52] Using gulpfile myProject/gulpfile.js 
[18:24:52] Starting 'jshint'... 
[18:24:52] Starting 'css'... 
[18:24:52] Starting 'scripts'... 
[18:24:52] Starting 'partials'... 
[18:24:52] Starting 'images'... 
[18:24:53] Starting 'videos'... 
[18:24:53] Starting 'fonts'... 
[18:24:53] Starting 'fileinclude'... 
[18:24:53] Starting 'extras'... 
[18:24:53] Finished 'jshint' after 518 ms 
[18:24:53] all files 48.19 kB 
[18:24:53] Finished 'fileinclude' after 245 ms 

events.js:154 
     throw er; // Unhandled 'error' event 
    ^
Error 
    at new JS_Parse_Error (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:1534:18) 
    at js_error (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:1542:11) 
    at croak (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2089:9) 
    at token_error (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2097:9) 
    at unexpected (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2103:9) 
    at expr_atom (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2618:13) 
    at maybe_unary (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2792:19) 
    at expr_ops (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2827:24) 
    at maybe_conditional (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2832:20) 
    at maybe_assign (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2856:20) 
    at expression (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2875:20) 
    at expr_atom (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2608:26) 
    at maybe_unary (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2792:19) 
    at expr_ops (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2827:24) 
    at maybe_conditional (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2832:20) 
    at maybe_assign (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2856:20) 

這裏是gulpfile.js的摘錄:

/*global -$ */ 
'use strict'; 

var gulp = require('gulp'); 
var $ = require('gulp-load-plugins')(); 
var browserSync = require('browser-sync'); 
var reload = browserSync.reload; 
var sass = require('gulp-ruby-sass'); 
//var concat = require('gulp-concat'); 
var rename = require('gulp-rename'); 
var gulpUtil = require('gulp-util'); 
var uglify = require('gulp-uglify'); 

//script paths 
var jsFiles = 'app/scripts/**/*.js', 
    jsDest = 'dist/scripts'; 

gulp.task('scripts', function() { 
    return gulp.src(jsFiles) 
     //.pipe(concat('main.js')) 
     .pipe(rename('main.min.js')) 
     .pipe(uglify()) // issue comes from here 
     .pipe(gulp.dest(jsDest)); 
}); 

gulp.task('build', ['jshint','css', 'scripts', 'partials', 'images', 'videos', 'fonts', 'fileinclude', 'extras'], function() { 
    return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true})); 
}); 

gulp.task('default', ['clean'], function() { 
    gulp.start('build'); 
}); 

隨着錯誤消息似乎與uglify-js有關,我試圖在scripts任務中註釋掉uglify()調用,並且它編譯得很好。

你知道爲什麼會發生這種情況嗎?

+0

請給我們看[mcve]。目前還不清楚沒有它會發生什麼。 –

+0

@EmileBergeron添加了gulpfile.js的全部內容。希望現在更容易排除故障。謝謝你的幫助! – Surreal

+0

嘗試每個單獨的吞嚥任務,找到哪一個是問題,然後從您的問題中刪除任何不相關的代碼,以便我們只能關注相關部分。這就是你應該如何做[mcve]。 –

回答

0

在本post建議,我安裝了gulp-util包並用其log()方法來對這個錯誤

var gulpUtil = require('gulp-util'); 
gulp.task('scripts', function() { 
    return gulp.src(jsFiles) 
     .pipe(rename('main.min.js')) 
     .pipe(uglify().on('error', function(e){ 
      console.log(e); 
      return this.end(); 
     })) 
     .pipe(gulp.dest(jsDest)); 
}); 

這裏更多的細節是錯誤消息我:

{ [Error: myProject/app/scripts/main.min.js: SyntaxError: Unexpected token: punc())] 
    message: 'myProject/app/scripts/main.min.js: SyntaxError: Unexpected token: punc())', 
    fileName: 'myProject/app/scripts/main.min.js', 
    lineNumber: 114, 
    stack: 'Error\n at new JS_Parse_Error (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:1534:18)\n at js_error (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:1542:11)\n at croak (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2089:9)\n at token_error (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2097:9)\n at unexpected (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2103:9)\n at expr_atom (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2618:13)\n at maybe_unary (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2792:19)\n at expr_ops (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2827:24)\n at maybe_conditional (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2832:20)\n at maybe_assign (eval at <anonymous> (myProject/node_modules/uglify-js/tools/node.js:28:1), <anonymous>:2856:20)', 
    showStack: false, 
    showProperties: true, 
    plugin: 'gulp-uglify' } 

我從main.js中檢查了第114行,發現我留下了uglify不理解的ES6語法。我將它轉換爲ES5,它工作正常。