2014-05-04 19 views
17

मैं अपने gulpfile को सूखने की कोशिश करता हूं। वहां मेरे पास कोड का छोटा सा डुप्लिकेशंस है जिसके साथ मैं सहज नहीं हूं। इसे बेहतर कैसे बनाया जा सकता है?गुलप में किसी अन्य कार्य को पाइप कैसे करें?

gulp.task('scripts', function() { 
    return gulp.src('src/scripts/**/*.coffee') 
    .pipe(coffeelint()) 
    .pipe(coffeelint.reporter()) 
    .pipe(coffee()) 
    .pipe(gulp.dest('dist/scripts/')) 
    .pipe(gulp.src('src/index.html')) // this 
    .pipe(includeSource())    // needs 
    .pipe(gulp.dest('dist/'))   // DRY 
}); 

gulp.task('index', function() { 
    return gulp.src('src/index.html') 
    .pipe(includeSource()) 
    .pipe(gulp.dest('dist/')) 
}); 

मैं एक अलग कार्य के रूप में index मिला है, के बाद से मैं src/index.html देखने के लिए livereload की जरूरत है। लेकिन मैं अपने .coffee स्रोतों को भी देख रहा हूं और जब वे बदलते हैं, तो मुझे src/index.html भी अपडेट करना होगा।

index पर scripts में मैं कैसे पाइप कर सकता हूं?

+0

संभावित डुप्लिकेट (http://stackoverflow.com/questions/22824546/how- [अन्य के बाद तुल्यकालिक/एक Gulp कार्यों को चलाने के लिए कैसे] टू-रन-गल्प-वर्क्स-सिंक्रोनस-एक-बाद-दूसरे) – falsarella

उत्तर

25

gulp आपको तर्कों के आधार पर कार्यों की श्रृंखला का ऑर्डर करने में सक्षम बनाता है।

उदाहरण:

gulp.task('scripts', function() { 
    return gulp.src('src/scripts/**/*.coffee') 
    .pipe(coffeelint()) 
    .pipe(coffeelint.reporter()) 
    .pipe(coffee()) 
    .pipe(gulp.dest('dist/scripts/')); 
}); 

gulp.task('index', ['scripts'], function() { 
    return gulp.src('src/index.html') 
    .pipe(includeSource()) 
    .pipe(gulp.dest('dist/')) 
}); 

कार्य index अब scripts की आवश्यकता होगी से पहले तैयार किया जाए:

gulp.task('second', ['first'], function() { 
    // this occurs after 'first' finishes 
}); 

दोनों कार्यों को चलाने के लिए निम्न कोड, आप कार्य चल रहा हो जाएगा 'सूचकांक' प्रयास करें यह कोड के कार्य के अंदर कोड चलाता है।

+0

मैं जितनी जल्दी हो सके इस दृष्टिकोण को आजमाउंगा। – srigi

+1

और क्या होगा यदि 'स्क्रिप्ट' चलेंगी, और 'स्क्रिप्ट' के ठीक बाद 'इंडेक्स' चलाने की आवश्यकता है? – vsync

+2

मैं अपने कार्यों को ऑर्केस्ट्रेट करने के लिए 'रन-अनुक्रम' का उपयोग करना पसंद करता हूं, इसलिए आम तौर पर यह समाधान सही है - स्वयं को gulp.task में अलग-अलग दोहराने का काम। – srigi

2

यदि आप ऑर्केस्ट्रेटर स्रोत, विशेष रूप से .start() कार्यान्वयन में देखते हैं तो आप देखेंगे कि यदि अंतिम पैरामीटर एक फ़ंक्शन है तो यह इसे कॉलबैक के रूप में पेश करेगा।

मैं अपने खुद के कार्यों के लिए इस स्निपेट लिखा है:

gulp.task('task1',() => console.log(a)) 
    gulp.task('task2',() => console.log(a)) 
    gulp.task('task3',() => console.log(a)) 
    gulp.task('task4',() => console.log(a)) 
    gulp.task('task5',() => console.log(a)) 

    function runSequential(tasks) { 
    if(!tasks || tasks.length <= 0) return; 

    const task = tasks[0]; 
    gulp.start(task,() => { 
     console.log(`${task} finished`); 
     runSequential(tasks.slice(1)); 
    }); 
    } 
    gulp.task("run-all",() => runSequential([ "task1", "task2", "task3", "task4", "task5")); 
की
संबंधित मुद्दे