2
votes

It seems in many places the professionals are building their projects using a node.js script that involves either gulp or grunt. What I can't figure out though, is why the script method is preferrable? When switching from the command line version to the script version, you add other packages in: i.e. gulp-uglify, vinyl-source-stream and vinyl-buffer. Wouldn't it be safer long-term-wise to use a method with the least amount of dependencies? Take for example the following command line method which I am currently using:

browserify entry.js | uglifyjs > bundle.js

This relies on browserify and uglifyjs, and I don't have the additional dependencies of gulp, gulp-uglifyjs, etc... and I don't have to worry about the version of gulp-uglifyjs relative to the straight uglifyjs at npm. Now see the following version:

var browserify = require('browserify');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');

gulp.task('browserify', function() {
    return browserify('./source/scripts/app.js')
        .bundle()
        .pipe(source('bundle.js')) // gives streaming vinyl file object
        .pipe(buffer()) // <----- convert from streaming to buffered vinyl file object
        .pipe(uglify()) // now gulp-uglify works
        .pipe(gulp.dest('./build/scripts'));
});

It seems so much more complex, but for what reason would this ever be a more efficient and safer way to build a javascript project? Thanks.

1
I would say its because of node's default asynchronous behaviour, speeding up the build process (although shell commands can be parallelized as well ofcourse). If you use npm, i recommend this article (which confirms your findings): blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool - bryanph
I can't speak for gulp but as a long term grunt user I find the power of the file matching to be a primary attraction, and the ability to chain commands. If you're just doing simple things then yes, you can use the command line, but for more complex build systems with partial rebuilds, packing, live code reload, watches etc etc etc there's nothing quite like being able to just run grunt and let everything else magically happen. There's a reason why build systems like make and such exist, and that still applies to hip languages too. - Lex R

1 Answers

2
votes

It's not. The command line is still the safest way. And npm alows building such a script by default. People go for gulp or grunt or other such built tools for Continuous Integration. Uglify is something that you only need once for production purposes, but say you want to run your tests each time one of your file changes, or you want to use JSLint. Well, I know many of these plugins provide Continous Integration support, but not all of them do. Gulp, `Grunt``` and other such build tools come with the solution out-of-the box.

But I see more and more people moving from gulp, grunt to basic npm and I totally support this movement.