gulp browserify reactify task is quite slow

前端 未结 3 757
我寻月下人不归
我寻月下人不归 2021-02-04 14:48

I am using Gulp as my task runner and browserify to bundle my CommonJs modules.

I have noticed that running my browserify task is quite slow, it takes around 2 - 3 seco

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 14:54

    Many thanks to @PHaroZ for that answer. I had to modify a little bit that code for my needs though. I am working with ReactJS on Symfony2 framework and all my builds were taking 7s-21s!!! Crazy.. So that's what I have now:

    var path = {
        OUT : 'app.js',
        DEST_BUILD : './src/MyBundle/Resources/js/dist',
        ENTRY_POINT : './src/MyBundle/Resources/js/src/app.js'
    };
    
    gulp.task('watch', [], function() {
        var bundler = browserify({
            entries : [ path.ENTRY_POINT ],
            extensions : [ ".js", ".jsx" ],
    //        transform : [ 'reactify' ],
            debug : true,
            fullPaths : true,
            cache : {}, // <---- here is important things for optimization
            packageCache : {} // <----  and here
        }).transform("babelify", {presets: ["es2015", "react"]});
        bundler.plugin(watchify, {
    //      delay: 100,
    //      ignoreWatch: ['**/node_modules/**'],
    //      poll: false
        });
    
        var rebundle = function() {
            var startDate = new Date();
            console.log('Update start at ' + startDate.toLocaleString());
            return bundler.bundle(function(err, buf){
                    if (err){
                        console.log(err.toString());
                    } else {
                        console.log(' updated in '+(new Date().getTime() - startDate.getTime())+' ms');
                    }
                })
                .pipe(source(path.OUT))
                .pipe(gulp.dest(path.DEST_BUILD))
                ;
        };
    
        bundler.on('update', rebundle);
        return rebundle();
    });
    

    Now first compile takes around 20s and each time I update my file it takes around 800ms. It's just enough time to switch from IDE to my browser.

提交回复
热议问题