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.
Raghu
  • 1,140
  • 1
  • 14
  • 22
  • This doesn't have anything to do with passing `{loadmaps:true}` in one case and not the other? By the way, if you like, `through2.obj()` is shorthand for `through2({objectMode: true}, ...)`. – JMM Apr 22 '15 at 15:13
  • I'll double check but pretty sure load maps didn't make any difference. – Raghu Apr 22 '15 at 15:15
  • Ok. If it doesn't then please edit the post to make the 2 cases consistent. If it makes no difference it would obviously be better to omit it in both cases to cut down on noise. – JMM Apr 22 '15 at 15:27
  • @JMM - confirmed and post updated. Removed the `loadMaps: true` – Raghu Apr 22 '15 at 15:35
  • Is your example code in a `gulp.task()` function and is there another task that depends on it? – JMM Apr 22 '15 at 15:39
  • Here's the complete gulpfile.js - https://gist.github.com/raghur/734447b813e66113d7f9#file-gulpfile-js – Raghu Apr 22 '15 at 16:04

0 Answers0