Skip to content

Instantly share code, notes, and snippets.

@Rakonda
Created August 2, 2017 08:00
Show Gist options
  • Save Rakonda/9a66df275a5294c0436a8a75049c435c to your computer and use it in GitHub Desktop.
Save Rakonda/9a66df275a5294c0436a8a75049c435c to your computer and use it in GitHub Desktop.
Sample gulpfile with browserify proxy
var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var concat = require('gulp-concat');
// var livereload = require('gulp-livereload');
var refresh = require('gulp-refresh');
var connect = require('gulp-connect');
var path = require('path');
var less = require('gulp-less');
var exec = require('gulp-exec');
var open = require('gulp-open');
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var cleanCSS = require('gulp-clean-css');
var fileinclude = require('gulp-file-include');
var changed = require('gulp-changed');
var htmlbeautify = require('gulp-html-beautify');
// var rename = require('gulp-rename');
// var sh = require('shelljs');
// var clean = require('gulp-clean');
const port = gutil.env.port ? gutil.env.port : Math.floor(Math.random() * (8045 - 8020) + 8020);
var reportOptions = {
err: true, // default = true, false means don't write err
stderr: true, // default = true, false means don't write stderr
stdout: true // default = true, false means don't write stdout
};
var options = {
continueOnError: true, // default = false, true means don't emit error event
pipeStdout: false, // default = false, true means stdout is written to file.contents
customTemplatingThing: "test" // content passed to gutil.template()
};
var paths = {
css: ['frontend/css/*.css'],
html: ['frontend/*.html'],
script: ['frontend/js/*.js'],
image: ['frontend/img/*'],
less: ['frontend/css/*.less'],
};
var project = {
title: 'Cour de cassation',
source_path: 'frontend',
temp_path: 'tmp',
debug: 'info',
domain: 'cour-de-cassation.frontend'
};
gulp.task('proxy', function () {
browserSync.init({
proxy: 'link of page to make it proxy like www.google.com',
files: ['frontend/**'],
serveStatic: ['frontend'],
cors: true,
rewriteRules: [
{
match: new RegExp('Content/style.css'),
fn: function () {
return 'css/style.css';
}
},
{
match: new RegExp('Scripts/scripts.js'),
fn: function () {
return 'js/scripts.js';
}
}
]
});
});
gulp.task('serve', function () {
connect.server({
root: ['frontend'],
base: 'localhost',
livereload: false,
port: port
});
browserSync.init({
proxy: 'localhost:' + port,
// browser: ['google chrome'],
// server: 'frontend',
notify: true,
open: gutil.env.open ? gutil.env.open : true,
online: false,
logLevel: project.debug,
logPrefix: project.title,
logFileChanges: true,
ghostMode: false,
port: port + 1
});
// gulp.watch("frontend/image/*.*").on('change', browserSync.reload);
gulp.watch("frontend/js/*.js").on('change', browserSync.reload);
gulp.watch("frontend/css/*.less", ['less-watch']);
gulp.watch("src/*.html", ['src-watch']);
gulp.watch("src/templates/*.html", ['src-templates-watch']);
// gulp.watch("src/*.html").on('change', browserSync.reload);
});
gulp.task('less-watch', function () {
return gulp.src('frontend/css/style.less')
.pipe(less())
.pipe(autoprefixer())
.on('error', onError)
.pipe(gulp.dest('frontend/css'))
.pipe(exec.reporter(reportOptions))
.pipe(browserSync.stream());
});
gulp.task('src-watch', function () {
return gulp.src('src/*.html')
.pipe(changed('frontend'))
.pipe(fileinclude({
prefix: '@@',
basepath: 'src/templates/'
}))
.on('error', onError)
.pipe(htmlbeautify({ indentSize: 2 }))
.pipe(gulp.dest('frontend/'))
.pipe(exec.reporter(reportOptions))
.on('end', function () {
browserSync.reload();
});
});
gulp.task('src-templates-watch', function () {
return gulp.src('src/*.html')
// .pipe(changed('frontend'))
.pipe(fileinclude({
prefix: '@@',
basepath: 'src/templates/'
}))
.on('error', onError)
.pipe(htmlbeautify({ indentSize: 2 }))
.pipe(gulp.dest('frontend/'))
.pipe(exec.reporter(reportOptions))
.on('end', function () {
browserSync.reload();
});
});
function onError(err) {
console.log(err);
this.emit('end');
}
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment