2015-05-20 135 views
7

我在我們的構建過程中添加了watchify,但我希望提前注意運行,那就是更改的文件通過了我們的linting步驟(使用ESLint)。在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()); 
} 

問題是掉毛的步驟是異步所以也沒有完成捆綁將完成之前(也拋出所以我可能需要使用plumber阻止它終止watch步驟)。

那麼在我打電話給bundleShared之前,我會怎麼做一個先決條件?

+0

你知道嗎? – Loknar

+0

nope。我必須爲他們並排運行,所以我有時會錯過ESLint錯誤 –

+1

我想知道是否也許這可以通過將關閉傳遞給watchify.on('update',func)來完成? https://github.com/substack/watchify我會嘗試一下,讓你知道 –

回答

2

我能夠使用上面提到的閉包方法做到這一點。我還將我的Browserify和Watchify代碼移入每個構建都可以利用的幫助函數中。

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(因此沒有重新打包()方法),所以我保持「皮棉:JS '作爲依賴項的任務:

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

感謝你!如果我想運行一個實時刷新服務器以及手錶功能,那麼您的配置會在哪裏? –

+0

立即在'buildDev'中調用'rebundle()'之後。你真正想做的是在dist /'文件夾中創建一個監視任務。這樣,每當'dist /'改變時,你的監視任務將檢測到它並運行服務器重載代碼。我用代碼更新了代碼,並顯示了它的位置。 –

+0

是的,我目前的手錶任務已經看過'dist' :) 感謝您的澄清!我喜歡你的解決方案,儘管我寧願讓Watchify運行一個任務,而不是調用一個函數,這樣依賴性和所有內容仍然可以用Gulp來管理,而不是通過在這裏和那裏調用方法來管理(就像你使用'lintJS'一樣): | –

相關問題