2016-08-09 6 views
1

2つのタスクを持つgulpファイルを用意しています。これは、一連のファイルに対してまったく同じアクションを実行します。入力ファイルと出力名と出力先は唯一の違いです。 2つのバッチのファイルを処理し、コードを複製しないために同じタスクを使用できる方法はありますか?私はある種の機能のようだと思う。 Iveはパラメータを渡してgoogleで探してみましたが、達成しようとしているものの例を見つけることができます。処理するファイルのGulpパス

var outPutFolder = '../'; 

//Array of all the JS files to compile for scripts.min.js 
var js = [ 
    'js/library/_helpers.js', 
    'js/library/_form-validation.js', 
    'js/page/_global.js', 
    'js/library/_ga_event_tracking.js', 
    'js/library/_postcode-anywhere.js', 
    'js/library/_count-down-timer.js', 
    'js/page/_home-page.js', 
    'js/page/_main_navigation.js', 
    'js/page/_myaccount.js', 
]; 

//Array of all the JS files to compile for scripts-external.min.js 
var externalJs = [ 
    'js/external/_bootstrap.min.js', 
    'js/external/_bootstrap-select.js', 
    'js/external/_bootbox.min.js', 
    'js/external/_jquery.lazyload.js', 
    'js/external/_jquery.bxslider.js', 
]; 

//JS Task to check files for errors, compile and then minify 
gulp.task('scripts', function() { 
    return gulp.src(js) 
     .pipe(eslint()) 
     .pipe(eslint.format()) 
     .pipe(concat('scripts.min.js')) 
     .pipe(gulp.dest(outPutFolder + 'js')) 
     .pipe(notify({ title:"scripts.min.js",message: "Successfully Compiled", onLast: true })) 
     .on('end', function() { gutil.log('scripts.min.js compiled successfully!'); }); 
}); 

//JS Task to check files for errors, compile and then minify 
gulp.task('scriptsExternal', function() { 
    return gulp.src(externalJs) 
    .pipe(eslint()) 
    .pipe(eslint.format()) 
    .pipe(concat('scripts-external.min.js')) 
    .pipe(gulp.dest(outPutFolder + 'js')) 
    .pipe(notify({ title: "scripts-external.min.js", message: "Successfully Compiled", onLast: true })) 
    .on('end', function() { gutil.log('scripts-external.min.js compiled successfully!'); }); 
}); 

答えて

0

バリアント1:あなたが機能を使用できるように

gulpfileは、JavaScriptが純粋です。ただし、このタスクを順番に使用している場合は、ファイルストリームを返す必要があります。

var outPutFolder = '../'; 

//Array of all the JS files to compile for scripts.min.js 
var js = [ … ]; 

//Array of all the JS files to compile for scripts-external.min.js 
var externalJs = [ … ]; 

function doStuff(in, out) { 
    return gulp.src(in) 
    .pipe(eslint()) 
    .pipe(eslint.format()) 
    .pipe(concat(out)) 
    .pipe(gulp.dest(outPutFolder + 'js')) 
    .pipe(notify({ title: out,message: "Successfully Compiled", onLast: true })) 
    .on('end', function() { gutil.log(out + ' compiled successfully!'); }); 
} 

//JS Task to check files for errors, compile and then minify 
gulp.task('scripts', function() { 
    return doStuff(js, 'scripts.min.js'); 
}); 

//JS Task to check files for errors, compile and then minify 
gulp.task('scriptsExternal', function() { 
    return doStuff(externalJs, 'scripts-external.min.js'); 
}); 

バリアント2:

それとも、environment variablesを使用してこれをトリガすることができます。

var outPutFolder = '../'; 

//Array of all the JS files to compile for scripts.min.js 
var js = [ … ]; 

//Array of all the JS files to compile for scripts-external.min.js 
var externalJs = [ … ]; 

var in = gutil.env.external ? externalJs : js; 
var out = gutil.env.external ? 'scripts-external.min.js' : 'scripts.min.js'; 

//JS Task to check files for errors, compile and then minify 
gulp.task('scripts', function() { 
    return gulp.src(in) 
    .pipe(eslint()) 
    .pipe(eslint.format()) 
    .pipe(concat(out)) 
    .pipe(gulp.dest(outPutFolder + 'js')) 
    .pipe(notify({ title: out,message: "Successfully Compiled", onLast: true })) 
    .on('end', function() { gutil.log('scripts.min.js compiled successfully!'); }); 
}); 

そして

gulp scripts --external 
+0

でタスクを実行し、これを試してみてくださいE私が望んでいたことをxactly、ありがとう! – gvperkins

関連する問題