'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 gutil = require('gulp-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 bellow. * 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.0.5/README.md */ if(gutil.env.target) { server.middleware = proxyMiddleware(['/api/**', '/image_uploads/**'], { target: gutil.env.target, changeOrigin: true, proxyTable: { 'localhost:3000' : gutil.env.target } }); } else { // no target! Point to localhost server.middleware = proxyMiddleware('/api', { target: 'http://0.0.0.0:9000/', pathRewrite: { // rewrite paths '^/api/v1/articles' : '/articles' }, proxyTable: { // when request.headers.host == 'dev.localhost:3000', // override target 'http://www.example.org' to 'http://localhost:8000' // 'dev.localhost:3000' : 'http://localhost:8000' 'hom.dialoga.gov.br' : 'http://localhost:9000', 'login.dialoga.gov.br' : 'http://localhost:9000' } }); } browserSync.instance = browserSync.init({ startPath: '/', server: server, 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, []); });