简体   繁体   English

浏览器同步gulp配置

[英]browser-sync gulp config

I am trying to improve my Gulp configuration to include browser sync. 我试图改善我的Gulp配置,以包括浏览器同步。 I currently use MAMP Pro using local.domain.com for each site I'm working on (WordPress sites). 我目前正在对每个正在使用的站点(WordPress站点)使用local.domain.com来使用MAMP Pro。

I'd like to get browser-sync to work using this current setup (watches my SASS files and minifies CSS when using watch). 我想使用当前的设置来使browser-sync工作(监视我的SASS文件并在使用监视时最小化CSS)。

I cannot seem to work out how to get browser sync to work with using the domain in my MAMP Pro setup. 我似乎无法弄清楚如何在MAMP Pro设置中使用域来使浏览器同步。

// Include gulp
var gulp = require('gulp'); 

// Include Our Plugins
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var cssnano = require('gulp-cssnano');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var uglifycss = require('gulp-uglifycss');

// Lint Task
gulp.task('lint', function() {
    return gulp.src('js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

// Compile Our Sass
gulp.task('sass', function() {
    return gulp.src('lib/themes/domain/styles/*.scss')
        .pipe(sass())
        .pipe(gulp.dest('lib/themes/domain/'));
});

// Concatenate & Minify JS
gulp.task('scripts', function() {
    return gulp.src('js/*.js')
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist'))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
});





gulp.task('uglify', function () {
  gulp.src('lib/themes/domain/style.css')
    .pipe(uglifycss({
      "max-line-len": 80
    }))
    .pipe(gulp.dest('lib/themes/domain/'));
});




// Watch Files For Changes
gulp.task('watch', function() {
    gulp.watch('js/*.js', ['lint', 'sass', 'scripts', 'uglify']);
    gulp.watch('lib/themes/domain/styles/*/*.scss', ['sass']);
    gulp.watch('lib/themes/domain/styles/*.scss', ['sass']);
    gulp.watch('lib/themes/domain/style.css', ['uglify']);
});

// Default Task
gulp.task('default', ['lint', 'sass', 'uglify', 'scripts', 'watch']);

I wont write the code for you, instead I'll show you how I like to do it. 我不会为您编写代码,而是向您展示我的方式。 I run my browser-sync in a separate task, simply watch your build files for changes. 我在单独的任务中运行浏览器同步,只需观察构建文件中的更改即可。

 var browserSync = require('browser-sync').create(); var urlPath = "your-url.com"; gulp.task('browser-sync', function (cb) { browserSync.init({ proxy: urlPath, }, function() { gulp.watch("Views/**/*.cshtml").on("change", browserSync.reload); gulp.watch('Assets/build/scripts/**/*.js').on('change', browserSync.reload); gulp.watch('Assets/build/styles/**/*.css').on('change', function () { gulp.src('Assets/build/styles/**/*.css') .pipe(browserSync.stream()); }); cb(); }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM