Skip to content

Instantly share code, notes, and snippets.

@jackfranklin
Created December 3, 2013 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackfranklin/7779720 to your computer and use it in GitHub Desktop.
Save jackfranklin/7779720 to your computer and use it in GitHub Desktop.
gulp.task("concat", function() {
var stream = gulp.src("./src/*.js")
.pipe(concat("concat.js"))
.pipe(gulp.dest("./tmp/"));
return stream;
});
gulp.task("uglify", ["concat"], function() {
gulp.src("./tmp/concat.js")
.pipe(uglify())
.pipe(gulp.dest("./dist"));
});
var gulp = require("gulp");
var concat = require("gulp-concat");
var uglify = require("gulp-uglify");
gulp.task("concat", function() {
gulp.src("./src/*.js")
.pipe(concat("concat.js"))
.pipe(gulp.dest("./tmp/"));
});
gulp.task("uglify", ["concat"], function() {
gulp.src("./tmp/concat.js")
.pipe(uglify())
.pipe(gulp.dest("./dist"));
});

The first time I run gulp uglify, it won't create ./dist/concat.js, because ./tmp/concat.js doesn't exist. I presume this is because the concat task is async, and hence there's no guarantee it's finished before uglify gets run.

You can fix this as shown in Gulpfile-fixed.js, presumably because when you return a stream gulp checks to see if it's finished before calling Uglify?

@yocontra
Copy link

yocontra commented Dec 3, 2013

@jackfranklin You need to tell gulp somehow that the task is asynchronous and callback when it is done. There are a few ways you can do this. https://github.com/wearefractal/gulp#gulptaskname-deps-fn

I recommend returning the stream you create in the task. Also you don't need to create tmp files and folders, you can just

gulp.src("./src/*.js")
    .pipe(concat("concat.js"))
    .pipe(uglify())
    .pipe(gulp.dest("./dist"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment