2015-05-20 5 views
7

मैं अपनी निर्माण प्रक्रिया में watchify जोड़ रहा हूं लेकिन मैं चलने को देखने के लिए एक पूर्व शर्त रखना चाहता हूं, और यही वह फ़ाइल है जो हमारे लिफ्टिंग चरण को पास करती है (जो ESLint का उपयोग कर रही है)।रनिफ़ाई चलाने से पहले एस्लिंट चलाएं

मैं ऐसा करने का सोच रहा था:

function runBrowserify(watch){ 
    var babel = babelify.configure({ 
     optional: ['es7.objectRestSpread'] 
    }); 
    var b = browserify({ 
    entries: './app/main.js', 
    debug: true, 
    extensions: ['.jsx', '.js'], 
    cache: {}, 
    packageCache: {}, 
    fullPaths: true 
    }) 
    .transform(babel); 

    if(watch) { 
    // if watch is enable, wrap this bundle inside watchify 
    b = watchify(b); 
    b.on('update', function(ids) { 
     //run the linting step 
     lint(ids); 

     //run the watchify bundle step 
     gutil.log(gutil.colors.blue('watchify'), 'Started'); 
     bundleShare(b); 
    }); 
    b.on('time', function (time) { 
     gutil.log(gutil.colors.blue('watchify'), 'Finished', 'after', gutil.colors.magenta(time), gutil.colors.magenta('ms')); 
    }); 
    } 

    bundleShare(b); 
} 

function bundleShare(b) { 
    b.bundle() 
    .pipe(source('main.min.js')) 
    .pipe(gulp.dest('./dist')); 
} 

function lint(glob) { 
    return gulp.src(glob) 
    .pipe(eslint()) 
    .pipe(eslint.format()) 
    .pipe(eslint.failOnError()); 
} 

समस्या है कि linting कदम async है, इसलिए यह बंडलिंग किया जा रहा था जब खत्म नहीं करता है (यह भी थ्रो तो मैं शायद plumber उपयोग करने की आवश्यकता watch चरण को समाप्त करने से रोकने के लिए)।

तो bundleShared पर कॉल करने से पहले मैं पूर्व शर्त कैसे बना सकता हूं?

+0

क्या आपको यह पता चला? – Loknar

+0

नहीं। मुझे उनके लिए एक साथ चलने के लिए व्यवस्थित करना है, इसलिए मुझे कभी-कभी ईएसलिंट त्रुटियों को याद आती है –

+1

मुझे आश्चर्य है कि शायद यह देखने के लिए बंद करने के द्वारा किया जा सकता है। ('अपडेट', func)? https://github.com/substack/watchify मैं इसे कभी-कभी कोशिश करूंगा और आपको –

उत्तर

2

मैं ऊपर उल्लिखित बंद विधि का उपयोग करके ऐसा करने में सक्षम था। मैंने अपने ब्राउजरिफा और वॉचइफी कोड को सहायक कार्यों में भी ले जाया जो प्रत्येक निर्माण का लाभ उठा सकता है।

gulpfile.js (आंशिक)

gulp.task('build:dev', buildDev); 
gulp.task('lint:js', lintJS); 

function lintJS(callback) { 
    return gulp.src(['src/**/*.js', 'src/**/*.jsx', '!src/js/vendor/**/*.*',]) 
     .pipe(eslint()) 
     .pipe(eslint.format()) 
     .pipe(eslint.failAfterError()); 
} 

function buildDev(callback) { 
    var bundler = getBundler('src/js/app.jsx', { debug: true }, callback); 
    var watcher = getWatcher(bundler, rebundle); 

    function rebundle() { 
    lintJS(callback); 

    return watcher.bundle() 
     .pipe(source('bundle.min.js')) 
     .pipe(buffer()) 
     .pipe(gulp.dest('dist/js')); 
    } 

    rebundle(); 
    // Call watch methods here, i.e.: watchHTML() 
    return callback(); 
} 

/****************************** Helper functions ******************************/ 

/** 
* Gets the default Browserify bundler used by all builds. 
* 
* 
* @param path A string representing where Browserify should start from 
* @param options An Object containing options for the bundler 
* @param callback The Gulp callback function from the calling task 
* @return A basically configured Browserify bundler 
*/ 
function getBundler(path, options, callback) { 
    var bundler = browserify(path, { debug: options.debug, cache: {}, packageCache: {} }); 
    bundler.transform(babelify); 
    bundler.on('log', gutil.log); 
    bundler.on('error', gutil.log.bind(gutil.colors.red, 'Browserify Error')); 

    return bundler; 
} 

/** 
* Gets the default Watchify watcher used by dev builds. By default, the watcher 
* will rebundle the Browserify package when an update occurs. 
* 
* @param bundle The Browserify bundler object 
* @param rebundle A function to perform when Watchify detects a code update 
* @return A basically configured Watchify watcher 
*/ 
function getWatcher(bundle, rebundle) { 
    var watcher = watchify(bundle); 
    watcher.on('update', rebundle); 

    return watcher; 
} 

अपने परीक्षण के लिए और उत्पादन बनाता है, मैं Watchify का उपयोग नहीं करते (और इस प्रकार कोई rebundle() है विधि) तो मैं 'फाहा रखें: js 'निर्भरता के रूप में कार्य:

gulp.task('build:test', ['lint:js'], buildTest); 
gulp.task('build:prod', ['lint:js'], buildProd); 
+0

इस बारे में बता दूंगा! अगर मैं घड़ी सुविधा के साथ एक लाइव-रीलोड सर्वर चलाने के लिए चाहता हूं, तो यह आपके कॉन्फ़िगरेशन में कहां जाएगा? –

+0

'buildDev' में 'रिबंडल()' पर कॉल के तुरंत बाद। आप वास्तव में क्या करना चाहते हैं अपने 'dist/'फ़ोल्डर पर एक घड़ी कार्य बनाना है। इस तरह जब भी 'dist/'बदलता है, तो आपका घड़ी कार्य इसका पता लगाएगा और सर्वर-रीलोड कोड चलाएगा। मैंने कोड को एक टिप्पणी के साथ अपडेट किया जिसमें यह दिखाया गया है कि यह कहां जाता है। –

+0

हाँ, मेरा वर्तमान घड़ी कार्य पहले से ही 'dist' देखता है :) स्पष्टीकरण के लिए धन्यवाद! मुझे आपका समाधान पसंद है, हालांकि मैं वॉचइफी को फ़ंक्शन को कॉल करने के बजाए एक कार्य चलाने की बजाय काम करता हूं ताकि निर्भरताएं और सभी को अभी भी वहां और वहां विधियों को कॉल करके गुलप के साथ प्रबंधित किया जा सके (जैसे आपने 'लिंटजेएस' के साथ किया था): | –

संबंधित मुद्दे