svn*_*vnm 9 javascript browserify reactjs gulp watchify
我正在使用Gulp作为我的任务运行器并使用浏览器来捆绑我的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('./'));
});
Run Code Online (Sandbox Code Playgroud)
我正在使用Browserify,Watchify,Reactify和Vinyl Source Stream以及其他一些不相关的模块.
var browserify = require('browserify'),
watchify = require('watchify'),
reactify = require('reactify'),
source = require('vinyl-source-stream');
Run Code Online (Sandbox Code Playgroud)
谢谢
Bri*_*and 17
通过watchify查看快速浏览器构建.请注意,传递给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'));
}
Run Code Online (Sandbox Code Playgroud)