Arekkku
Arekkku

Reputation: 21

remove browsersync from server.js for gulp

I have a small problem with my program and especially because of browser-sync. I would like to remove BrowserSync from my project.

Here's my current 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, []);
});

And my current 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');
});

I download these different files on a server where browser-sync is not installed so I'm looking for a way to launch my different gulp.task without going through browserSyncInit or at least having a browserSyncInit containing no call to BrowserSync and BrowserSyncSpa.

(I specify that I have other .js in my gulp folder where I have removed the calls to BrowserSync and I have no problem with these files.)

Upvotes: 2

Views: 1629

Answers (1)

Ankirama
Ankirama

Reputation: 498

The easiest way to remove browser-sync is to use gulp-connect.

Here an example for your task serve:

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

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

Don't forget to change the port and install gulp-connect with npm install --save gulp-connect.

Here the gulp-connect package

Upvotes: 1

Related Questions