2016-06-08 67 views
0

gulp noob在這裏,我看到奇怪的行爲與gulp-uglify。gulp-uglify stream在gulp-rename後出錯

這是我的任務:

var gulp = require ("gulp"); 
var util = require ("gulp-util"); 
var clean = require ("gulp-clean"); 
var cleanCSS = require ("gulp-clean-css"); 
var debug = require ("gulp-debug"); 
var filesize = require ("gulp-filesize"); 
var rename = require ("gulp-rename"); 
var sass = require ("gulp-ruby-sass"); 
var browserify = require (BROWSERIFY); 
var source = require ("vinyl-source-stream"); 
var buffer = require ("vinyl-buffer"); 
var sourceMaps = require("gulp-sourcemaps"); 
var babelify = require ("babelify"); 
var uglify = require ("gulp-uglify"); 
var concatFileNames = require ("gulp-concat-filenames"); 
var header = require ("gulp-header"); 

function browserifyTask() { 

    return browserify ("./app/main.js") 
     .transform(babelify) 
     .bundle() 
     .pipe (source("main.js")) 
     .pipe (buffer()) 
     .pipe (sourceMaps.init({loadMaps: true})) 

     .pipe (sourceMaps.write("./")) // Ensure the source map gets written 
     .pipe (gulp.dest("./public/js")) 

     // Now do production build stuff 

     .pipe (rename("main.min.js")) 
     .pipe (sourceMaps.init({loadMaps: true})) 
     .pipe (uglify().on ("error", util.log)) 
     .pipe (sourceMaps.write("./")) // Ensure the source map gets written 
     .pipe (gulp.dest("./public/js")) 
} 

的想法是產生正常(非精縮)和JS的縮小版本。

然而,醜化拋出一個錯誤:

[16:20:26] Using gulpfile C:\play\untitled\gulpfile.js 
[16:20:26] Starting 'browserify'... 
[16:20:28] { [Error: C:\play\untitled\public\js\main.min.js: Unexpected  token: punc (:)] 
    message: 'C:\\play\\untitled\\public\\js\\main.min.js: Unexpected token: punc (:)', 
    fileName: 'C:\\play\\untitled\\public\\js\\main.min.js', 
    lineNumber: 1, 
    stack: 'Error\n at new JS_Parse_Error (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1), <anonymous>:1526:18)\n at js_error (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1), <anonymous>:1534:11)\n at croak (eval at <anonymo 
us> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1), <anonymous>:2026:9)\n at token_error (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1), <anonymous>:2034:9)\n at unexpected (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tool 
s\\node.js:22:1), <anonymous>:2040:9)\n at semicolon (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1),  <anonymous>:2060:56)\n at simple_statement (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1), <anonymous>:2240:73)\n at ev 
al (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify- js\\tools\\node.js:22:1), <anonymous>:2093:47)\n at eval (eval at <anonymous> (C:\\play\\untitled\\node_modules\\uglify-js\\tools\\node.js:22:1), <anonymous>:2073:24)\n at block_ (eval at <anonymous> (C:\\play\\untitled\\node_modules\\ugli 
fy-js\\tools\\node.js:22:1), <anonymous>:2353:20)', 
    showStack: false, 
    showProperties: true, 
    plugin: 'gulp-uglify' } 

這似乎不喜歡的東西,關於它的管道輸送,但醜化不會引發錯誤,如果它是第一個gulp.dest之前稱爲JS( )電話(我確實得到縮小的代碼)。

This post表示源地圖可能是一個問題,並使用gulp-ignore來排除它們。試過了,它不起作用;同樣的錯誤。

我在這裏遺漏了一些明顯的東西嗎?

感謝, 傑夫

回答

0

所以這是造成醜化在所不惜源地圖。似乎有一個更普遍的問題,將Browserify與其他插件相關聯,這些插件與Browserify相關,希望緩衝區中的內容和其他插件需要流中的內容。

爲此,我refind我的任務功能,這一點:

function browserifyTask() { 

    function doBrowserify (isProduction) { 
     browserify ("./app/main.js") 
      .transform(babelify) 
      .bundle() 
      .pipe (source("main.js")) 
      .pipe (isProduction ? rename("main.min.js") : util.noop()) 
      .pipe (buffer()) 
      .pipe (sourceMaps.init({loadMaps: true})) 
      .pipe (isProduction ? uglify().on ("error", util.log) : util.noop()) 
      .pipe (sourceMaps.write("./")) 
      .pipe (gulp.dest("./public/js")) 
    } 

    doBrowserify(false); 
    doBrowserify(true); 
} 

不優雅,我想它,但它能夠完成任務,我需要這個項目破解的(和不會在構建過程中陷入困境)。

相關問題