2017-10-16 60 views
2

我的程序有一個小問題,尤其是因爲瀏覽器同步。 我想從我的項目中刪除BrowserSync。從server.js刪除瀏覽器同步吞噬

這是我目前一飲而盡/ server.js

'use strict'; 

var path = require('path'); 
var gulp = require('gulp'); 
var conf = require('./conf'); 

var browserSync = require('browser-sync'); 
var browserSyncSpa = require('browser-sync-spa'); 

var util = require('util'); 

var proxyMiddleware = require('http-proxy-middleware'); 

function browserSyncInit(baseDir, browser) 
{ 
    browser = browser === undefined ? 'default' : browser; 

    var routes = null; 
    if (baseDir === conf.paths.src || (util.isArray(baseDir) && baseDir.indexOf(conf.paths.src) !== -1)) 
    { 
     routes = { 
      '/bower_components': 'bower_components' 
     }; 
    } 

    var server = { 
     baseDir: baseDir, 
     routes : routes 
    }; 

    /* 
    * You can add a proxy to your backend by uncommenting the line below. 
    * You just have to configure a context which will we redirected and the target url. 
    * Example: $http.get('/users') requests will be automatically proxified. 
    * 
    * For more details and option, https://github.com/chimurai/http-proxy-middleware/blob/v0.9.0/README.md 
    */ 
    // server.middleware = proxyMiddleware('/users', {target: 'http://jsonplaceholder.typicode.com', changeOrigin: true}); 

    browserSync.instance = browserSync.init({ 
     startPath: '/', 
     server : server, 
     ghostMode : false, 
     browser : browser 
    }); 
} 

browserSync.use(browserSyncSpa({ 
    selector: '[ng-app]'// Only needed for angular apps 
})); 

gulp.task('serve', ['watch'], function() 
{ 
    browserSyncInit([path.join(conf.paths.tmp, '/serve'), conf.paths.src]); 
}); 

gulp.task('serve:dist', ['build'], function() 
{ 
    browserSyncInit(conf.paths.dist); 
}); 

gulp.task('serve:e2e', ['inject'], function() 
{ 
    browserSyncInit([conf.paths.tmp + '/serve', conf.paths.src], []); 
}); 

gulp.task('serve:e2e-dist', ['build'], function() 
{ 
    browserSyncInit(conf.paths.dist, []); 
}); 

而且我目前gulpfiles.js

/** 
* Welcome to your gulpfile! 
* The gulp tasks are splitted in several files in the gulp directory 
* because putting all here was really too long 
*/ 

'use strict'; 

var gulp = require('gulp'); 
var wrench = require('wrench'); 

/** 
* This will load all js or coffee files in the gulp directory 
* in order to load all gulp tasks 
*/ 
wrench.readdirSyncRecursive('./gulp').filter(function(file) { 
    return (/\.(js|coffee)$/i).test(file); 
}).map(function(file) { 
    require('./gulp/' + file); 
}); 


/** 
* Default task clean temporaries directories and launch the 
* main optimization build task 
*/ 
gulp.task('default', ['clean'], function() { 
    gulp.start('build'); 
}); 

我下載未安裝瀏覽器同步的服務器上,這些不同的文件,所以我m尋找一種方式來啓動我不同的gulp.task,而無需通過browserSyncInit或至少有一個browserSyncInit不包含對BrowserSync和BrowserSyncSpa的調用。

(我指定我有我的一口文件夾以外的.js,我已遷到BrowserSync的電話和我有這些文件沒問題。)

回答

1

刪除browser-sync最簡單的方法是使用gulp-connect

下面的例子爲任務服務

var connect = require('gulp-connect'); 

gulp.task('serve', ['watch'], function() { 
    connect.server({ 
    root: conf.paths.src, 
    port: 3000 
    }); 
} 

不要忘記改變端口並安裝有npm install --save gulp-connect一飲而盡-連接。

這裏的gulp-connect package