1

In my gulpfile.js, I'm trying to minify and enable sourcemaps in a release task.

Based on a config variable, I'm trying to pipe further actions into the stream. The check for the condition is using gulp-if-else. My set of dependencies (only relevant ones) are below

var gulp = require('gulp');
var browserify = require('browserify');
//... elided
var through= require('through2');
var sourcemaps= require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var ifelse = require('gulp-if-else');

Here's the code that I'm having trouble with. If I keep three ifelse pipe calls individually (currently commented out), then the source is uglified and sourcemap is created as expected.

Repeating the condition didn't feel clean - so I tried replacing it with the minify function - and then the behavior is strange.

  1. If I don't give relative path in sourcemaps.write then the js file has a inline sourcemap.
  2. With a relative path, source is uglified, the js has a sourcemap comment pointing to app.js.map but app.js.map itself is never generated
var minify = function() {
    var stream =  through({objectMode:true})
    stream
        .pipe(sourcemaps.init())
        .pipe(uglify())
        .pipe(sourcemaps.write("./"));
    return stream;
};

var bundledStream = through({objectMode:true});
bundledStream
    // Report compile errors
    .on('error', handleErrors)
    .on('end', reportFinished)
    // Use vinyl-source-stream to make the
    // stream gulp compatible. Specifiy the
    // desired output filename here.
    .pipe(source(entry + '.js'))
    // Specify the output destination
    .pipe(buffer())

    // uncommenting the following three lines works

    //.pipe(ifelse(!config.debug,function() {return sourcemaps.init()}))
    //.pipe(ifelse(!config.debug, uglify))
    //.pipe(ifelse(!config.debug,function() {return sourcemaps.write("./")}))


    // this works partially?!? but I don't know why. I'd like this version to work
    .pipe(ifelse(!config.debug, minify))


    .pipe(gulp.dest(config.destFolder));

I'm just picking up gulp (and node, really) and ran into this weird behavior that I can't seem to reason about. Would be a great help if someone can demystify it for me.

UPDATE: Complete gulpfile.js

UPDATE2: Added gulp-filelog. added pipe(filelog("minify")) and pipe(filelog("outer")) to the streams. filelog prints two files inside the fn but only one file outside. Why is the second file being dropped/ignored? What am I missing?

[09:43:24] [minify] [1] [E:\....\Dashboard\app.js.map]
[09:43:24] [minify] [2] [E:\....\app.js]
[09:43:24] [outer] [1] [E:\....\Dashboard\app.js]
[09:43:24] [outer] Found [1] files.
[09:43:24] [minify] Found [2] files.
4

0 回答 0