Browserify uglify via node script or command line?

2019-07-20 16:59发布

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条回答
虎瘦雄心在
2楼-- · 2019-07-20 17:44

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.

查看更多
登录 后发表回答