Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browsersync serve local files instead of remote files

So I want to connect browsersync to a site I don't have any control over. Basically I want it to replace a css file on the live site with a css file on my computer (which gets compiled from a bunch of less files). When that local css file is changed then browsersync should inject the css like it normally would. I'm using browsersync with gulp since I also want to have other tasks run.

The code below will open gulpjs.com and correctly match main.css from gulpjs.com and attempt to serve something in it's place. But all I'm getting is a 404 for the main.min.css. It's looking for it here

http://localhost:3000/css/main.min.css

I zipped up the test project (node modules not included) you can download it here - https://www.dropbox.com/s/5x5l6bbxlwthhoo/browsersyncTest.zip?dl=0

Here's what I have so far...

Project Structure

css/ main.min.css (compiled from app.less)

less/ app.less

gulpfile.js 

package.json

package.json

{
  "name": "browsersyncTest",
  "devDependencies": {
    "autoprefixer": "^6.7.0",
    "browser-sync": "^2.18.6",
    "cssnano": "^3.10.0",
    "gulp": "^3.9.1",
    "gulp-clean-css": "^2.3.2",
    "gulp-less": "^3.3.0",
    "gulp-postcss": "^6.3.0",
    "gulp-rename": "^1.2.2",
    "gulp-uglify": "^2.0.0",
    "gulp-watch": "^4.3.11"
  }
}

gulpfile.js

var gulp = require('gulp');
var less = require('gulp-less');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');
var cleancss = require('gulp-clean-css');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var browserSync = require('browser-sync').create();

gulp.task('default', function() {

});

gulp.task('less', function () {
    var plugins = [autoprefixer({browsers: ['last 2 versions', 'ie >= 10']})];
    return gulp.src('less/app.less')
        .pipe(less())
        .pipe(rename('main.min.css'))
        //.pipe(postcss(plugins))
        //.pipe(cleancss())
        .pipe(gulp.dest('css'))
        .pipe(browserSync.stream());
});

gulp.task('watch', ['less'], function () {

    browserSync.init({
        proxy: "http://gulpjs.com/",
        files: ['css/**'],
        serveStatic: ['css'],
        rewriteRules: [{
            match: new RegExp('css/main.css'),
            fn: function() {
                return 'css/main.min.css';
            }
        }]
    });

    gulp.watch('less/**/*.less', ['less']);
});

Thanks a bunch!

like image 844
John_911 Avatar asked Jan 24 '17 16:01

John_911


1 Answers

Got it all figured out.

Here's the updated code.

browserSync.init({
    proxy: "http://gulpjs.com/",
    rewriteRules: [
        {
            match: new RegExp("css/main.css"),
            fn: function() {
                return "main.min.css"
            }
        }
    ],
    files: "css/*.css",
    serveStatic: ['./css']
    });
like image 155
John_911 Avatar answered Oct 23 '22 12:10

John_911