gulp-rename

时间:2016-06-08 15:34:12

标签: gulp gulp-uglify

Gulp noob在这里,我看到了gulp-uglify的奇怪行为。

这是我的任务:

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和缩小版本。

但是,Uglify会抛出错误:

[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' }

看起来它似乎不喜欢关于JS的东西,但是如果在第一次gulp.dest()调用之前调用了它,那么Uglify不会抛出错误(我确实得到了缩小的代码)。

This post表示源地图可能是一个问题,并使用gulp-ignore来排除它们。尝试过,它没有用;同样的错误。

我错过了一些明显的东西吗?

谢谢, 杰夫

1 个答案:

答案 0 :(得分:0)

因此,源地图导致Uglify无视。似乎有一个更普遍的问题,将Browserify与其他插件相结合,这些插件与Browserify有关,希望缓冲区和其他插件需要流中的东西。

为此,我将任务功能改为:

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

不像我喜欢它那样优雅,但是它完成了工作,我需要继续使用这个项目(而不是在构建过程中陷入困境)。