Browserify uglify via node script or command line?

2019-07-20 17:37发布

问题:

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:

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.