Browserify通过节点脚本或命令行丑化? [英] Browserify uglify via node script or command line?
问题描述
browserify entry.js | uglifyjs> bundle.js
这依赖于browserify和uglifyjs,我没有额外的gulp依赖,gulp-uglifyjs等等,我不必担心与npm中的直线uglifyjs相关的gulp-uglifyjs版本。现在看到以下版本:
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'))//给出乙烯基流文件对象
.pipe(buffer())//< -----从流转换为缓冲乙烯文件对象
.pipe(uglify())// now gulp-uglify works
.pipe(gulp.dest('./ build / scripts'));
});
看起来更加复杂,但是出于何种原因,这会是更高效和更安全的方式建立一个JavaScript项目?谢谢。
事实并非如此。命令行仍然是最安全的方式。并且 npm
允许默认构建这样的脚本。人们为 gulp
或 grunt
或其他此类构建的工具用于持续集成
。 Uglify
是您为生产目的只需要一次的东西,但假设您希望每次运行测试
你的文件改变了,或者你想用 JSLint
。那么,我知道很多这些插件提供了连续集成
支持,但并不是所有的都支持。 Gulp
,`Grunt```和其他这样的构建工具随附解决方案。
但我发现越来越多的人从 gulp
, grunt
移动到基本 npm
,我完全支持这个动作。
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.
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.
这篇关于Browserify通过节点脚本或命令行丑化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!