我将Gulp用作任务运行器,并通过browserify捆绑我的CommonJs模块。
我注意到,运行我的browserify任务非常慢,大约需要2到3秒,而我所拥有的只是React和为开发而构建的一些非常小的组件。
有什么方法可以加快任务的执行速度,还是我的任务有任何明显的问题?
gulp.task('browserify', function() { var bundler = browserify({ entries: ['./main.js'], // Only need initial file transform: [reactify], // Convert JSX to javascript debug: true, cache: {}, packageCache: {}, fullPaths: true }); var watcher = watchify(bundler); return watcher .on('update', function () { // On update When any files updates var updateStart = Date.now(); watcher.bundle() .pipe(source('bundle.js')) .pipe(gulp.dest('./')); console.log('Updated ', (Date.now() - updateStart) + 'ms'); }) .bundle() // Create initial bundle when starting the task .pipe(source('bundle.js')) .pipe(gulp.dest('./')); });
我正在使用Browserify,Watchify,Reactify和Vinyl Source Stream以及其他一些不相关的模块。
var browserify = require('browserify'), watchify = require('watchify'), reactify = require('reactify'), source = require('vinyl-source-stream');
谢谢
使用watchify查看快速的browserify构建。请注意,传递给browserify的唯一内容是主入口点和watchify的配置。
转换将添加到watchify包装器中。
逐字粘贴文章中的代码
var gulp = require('gulp'); var gutil = require('gulp-util'); var sourcemaps = require('gulp-sourcemaps'); var source = require('vinyl-source-stream'); var buffer = require('vinyl-buffer'); var watchify = require('watchify'); var browserify = require('browserify'); var bundler = watchify(browserify('./src/index.js', watchify.args)); // add any other browserify options or transforms here bundler.transform('brfs'); gulp.task('js', bundle); // so you can run `gulp js` to build the file bundler.on('update', bundle); // on any dep update, runs the bundler function bundle() { return bundler.bundle() // log errors if they happen .on('error', gutil.log.bind(gutil, 'Browserify Error')) .pipe(source('bundle.js')) // optional, remove if you dont want sourcemaps .pipe(buffer()) .pipe(sourcemaps.init({loadMaps: true})) // loads map from browserify file .pipe(sourcemaps.write('./')) // writes .map file // .pipe(gulp.dest('./dist')); }