从server.js中删除browsersync以获取gulp

时间:2017-10-16 13:51:34

标签: javascript node.js angular gulp

我的程序有一个小问题,特别是因为浏览器同步。 我想从我的项目中删除BrowserSync。

这是我目前的gulp / 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');
});

我在没有安装浏览器同步的服务器上下载这些不同的文件所以我正在寻找一种方法来启动我的不同gulp.task而不通过browserSyncInit或者至少让browserSyncInit不包含对BrowserSync和BrowserSyncSpa的调用

(我指定我的gulp文件夹中有其他.js,我已经删除了对BrowserSync的调用,我对这些文件没有任何问题。)

1 个答案:

答案 0 :(得分: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。

这里是gulp-connect package