jlouns / gulp-requirejs-optimize

Wrapper for the requirejs optimizer for use in gulp
MIT License
62 stars 9 forks source link

How to reduce the Optimizing cost time? #20

Closed ursulashan closed 8 years ago

ursulashan commented 8 years ago

Frist,thanks for your great job. I used this package on my project, the function is runing well ,but cost too much time on Optimizing progress. Each file cost 3s or more.

my gulpfile.js code is:

gulp.task('js',function(){
    return gulp.src(jsSrc)
    .pipe(flatmap(function(stream, file){
        var fileInfo=Tools.getFileInfo(file.path),
            fileName=fileInfo.taskName+'.min.js';
        return gulp.src(file.path)
            .pipe(requirejsOptimize(function(file){
               return {
                   optimize: 'none',
                   paths:{
                       jquery: path.join(__dirname,'vendor_bower/jquery/dist/jquery'),
                       header: path.join(__dirname,'src/js/lib/header'),
                       headerDark: path.join(__dirname,'src/js/lib/header-dark'),
                       footer: path.join(__dirname,'src/js/lib/footer'),
                       layout: path.join(__dirname,'src/js/lib/layout'),
                       handlebars: path.join(__dirname,'vendor_bower/handlebars/handlebars'),
                       jBox: path.join(__dirname,'vendor_bower/jbox/Source/jBox'),
                       playlist: path.join(__dirname,'vendor/playlist/js/playlist'),
                       wx: path.join(__dirname,'vendor/justshare/js/jweixin-1.0.0'),
                       justshare: path.join(__dirname,'vendor/justshare/js/justshare'),
                       widget: path.join(__dirname,'src/js/widget'),
                       vendor_bower: path.join(__dirname,'vendor_bower'),
                       vendor: path.join(__dirname,'vendor')
                   },
                   //非规范模块加载方式
                   shim: {
                       'playlist': {
                           deps: ['jquery'],
                           exports: 'playlist'
                       },
                       'justshare': {
                           deps: ['jquery'],
                           exports: 'justshare'
                       },
                       'jBox': {
                           deps: ['jquery'],
                           exports: 'jBox'
                       }
                   }
               }
            }))
            .pipe(uglify())
            .pipe(rename(fileName))
            .pipe(gulp.dest(dest_js))
            .pipe(debug({title: createBonus()+'--------脚本【'+ fileInfo.taskName +'】'}))
    }))
});
jlouns commented 8 years ago

Typically it is not necessary to optimize every file in the project. Instead, most projects just optimize a single main file that loads the rest of the files and bootstraps the app, or a set of module files that define specific layers in the app.

Depending on the size of your project, 3s is a reasonable optimization time--just make sure you are only doing it for the main files in your app, not all of them.