gulp styles finished immediately - javascript

styles/css is not working
my gulpfile
const gulp = require('gulp');
const browserSync = require('browser-sync');
const sass = require('gulp-sass')(require('sass'));
const cleanCSS = require('gulp-clean-css');
const autoprefixer = require('gulp-autoprefixer');
const rename = require("gulp-rename");
gulp.task('server', function() {
browserSync({
server: {
baseDir: "site2"
}
});
gulp.watch("site2/*.html").on('change', browserSync.reload); });
gulp.task('styles', function() {
return gulp.src("site2/sass/**/*.+(scss|sass)")
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(rename({suffix: '.min', prefix: ''}))
.pipe(autoprefixer())
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(gulp.dest("site2/css"))
.pipe(browserSync.stream());
});
gulp.task('watch', function() {
gulp.watch("src/sass/**/*.+(scss|sass)", gulp.parallel('styles'));
})
gulp.task('default', gulp.parallel('watch', 'server', 'styles'));
[22:09:46] Using gulpfile ~/Documents/Uber_step_3 (copy)/src/gulpfile.js
[22:09:46] Starting 'default'...
[22:09:46] Starting 'watch'...
[22:09:46] Starting 'server'...
[22:09:46] Starting 'styles'...
[Browsersync] Access URLs:
>! -------------------------------------
Local: http://localhost:3000
External: http://192.168.2.52:3000
>! -------------------------------------
UI: http://localhost:3001
UI External: http://localhost:3001
>! -------------------------------------
[Browsersync] Serving files from: site2
[Browsersync] 1 file changed (style.min.css)
[22:09:47] Finished 'styles' after 1.46 s
browser sync is working fine for html file. styles/css finishing immediately

Related

Gulp is not comile sass file and reload the page

I have this code in my gulpfile.js file:
var gulp = require('gulp');
// Requires the gulp-sass plugin
var sass = require('gulp-sass');
var browserSync = require('browser-sync').create();
gulp.task('browserSync', function() {
browserSync.init({
server: {
baseDir: 'app'
},
})
})
gulp.task('sass', function() {
return gulp.src('app/scss/**/*.scss') // Gets all files ending with .scss in app/scss
.pipe(sass())
.pipe(gulp.dest('app/css'))
.pipe(browserSync.reload({
stream: true
}))
});
gulp.task('watch', gulp.series(['browserSync', 'sass']), function() {
gulp.watch('app/scss/**/*.scss', gulp.series('sass'));
// Reloads the browser whenever HTML or JS files change
gulp.watch('app/*.html', browserSync.reload);
gulp.watch('app/js/**/*.js', browserSync.reload);
});
Now, when I run this command
gulp watch
It's open the project on browser BUT, when I do some changes on html, js or scss file then gulp does not compile the scss file and reloading the browser.
Can someone tell me what is the issues here?

Browsersync works only on second save

I never encountered this issue before cause I worked with one-page websites. But now I have several pages, and when I created third file in 'build' folder, browsersync started to work not as expected.
Basically, browsersync now makes changes in browser only on second save. You can see this behavior on video: https://youtu.be/lNFEDZMBiQY
Here's my gulp setup:
// Gulp.js configuration
var
// modules
gulp = require('gulp'),
sass = require('gulp-sass'),
pug = require('gulp-pug'),
concat = require('gulp-concat'),
//uglify = require('gulp-uglify'),
browserSync = require('browser-sync').create()
// browserSync
gulp.task('browserSync', function() {
browserSync.init({
server: {
baseDir: 'build',
directory: true
},
notify: false
})
})
// pug to html
gulp.task('pug', function buildHTML() {
return gulp.src('src/pug/*.pug')
.pipe(pug())
.pipe(gulp.dest('build/'))
.pipe(browserSync.reload({
stream: true
}))
});
// scss to css
gulp.task('sass', function(){
return gulp.src('src/scss/style.scss')
.pipe(sass()) // Converts Sass to CSS with gulp-sass
.pipe(gulp.dest('build/css'))
.pipe(browserSync.reload({
stream: true
}))
});
// js concat and minify
gulp.task('scripts', function() {
return gulp.src(['src/js/jquery-3.0.0.min.js', 'src/js/jquery-migrate-1.4.1.min.js', 'src/js/swiper.min.js', 'src/js/jquery.photoswipe-global.js', 'src/js/custom.js'])
.pipe(concat('main.min.js'))
//.pipe(uglify())
.pipe(gulp.dest('build/js/'));
});
// watcher
gulp.task('watch', ['browserSync', 'sass', 'pug', 'scripts'], function(){
gulp.watch('src/scss/**/*.scss', ['sass']);
gulp.watch('src/pug/**/*.pug', ['pug']);
gulp.watch('src/js/**/*.js', ['scripts']);
// Other watchers
})
gulp.task('default', ['watch']);
Maybe, there's an error in this setup? How I can fix this issue?

Task 'sass' is not in your Gulpfile - ERROR

Following is the code I am facing this error please help me solve this, i would be really graced to get clear answer ___
Everything runs smooth but except if I save the file, following error occurs
ERROR : Task 'sass' is not in your gulpfile
// require gulp
var gulp = require('gulp');
// require other packages
var concat = require('gulp-concat'),
cssmin = require('gulp-minify-css'),
rename = require("gulp-rename"),
sass = require('gulp-sass'),
browserSync = require('browser-sync'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin'),
autoprefix = require('gulp-autoprefixer');
changed = require('gulp-changed'),
// scripts task
gulp.task('scripts', function() {
return gulp.src('./src/js/*.js')
.pipe(concat('app.js'))
.pipe(gulp.dest('./dist/js/'))
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./dist/js/'));
});
// styles task and autoprefix
gulp.task('styles', function() {
gulp.src(['./src/sass/*.scss'])
.pipe(autoprefix('last 2 versions'))
.pipe(sass())
.pipe(concat('styles.css'))
.pipe(cssmin())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest('./dist/css/'));
});
// browser sync
gulp.task('browser-sync', ['styles'], function() {
browserSync({
server: {
baseDir: ''
},
notify: false
});
gulp.watch("./src/sass/*.scss", ['scss']);
gulp.watch("*.html").on('change', browserSync.reload);
});
// watch task
gulp.task('watch', function() {
gulp.watch('./src/js/*.js', ['scripts']);
gulp.watch('./src/sass/*.scss', ['styles']);
});
// image minification
gulp.task('imagemin', function() {
var imgSrc = './src/img/**/*',
imgDst = './dist/img';
gulp.src(imgSrc)
.pipe(changed(imgDst))
.pipe(imagemin())
.pipe(gulp.dest(imgDst));
});
gulp.task('default', ['scripts', 'styles', 'browser-sync', 'imagemin', 'watch']);
You do not have, indeed, defined "sass" task nor "scss" task thats marked as dependable under "browser-sync" file.
Try Replacing:
gulp.watch("./src/sass/*.scss", ['scss']);
with
gulp.watch("./src/sass/*.scss", ['styles']);

Why would Browsersync live reload for one compilation task but not another?

I have a gulpfile running to compile Sass, CoffeeScript, and live reload both through Browsersync:
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var sass = require('gulp-sass');
var autoprefixer = require('gulp-autoprefixer');
var cssmin = require('gulp-cssmin');
var coffee = require('gulp-coffee');
var uglify = require('gulp-uglify');
var browserSync = require('browser-sync');
gulp.task('sass', function() {
return gulp.src('./src/scss/**/*.scss')
.pipe(plumber())
.pipe(sass({
style: 'expanded',
precision: 10
}))
.pipe(autoprefixer({browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']}))
.pipe(cssmin())
.pipe(gulp.dest('./dist'))
.pipe(browserSync.stream());
});
gulp.task('coffee', function() {
return gulp.src('./src/coffee/**/*.coffee')
.pipe(plumber())
.pipe(coffee())
.pipe(uglify())
.pipe(gulp.dest('./dist'))
.pipe(browserSync.stream());
});
gulp.task('serve', function() {
browserSync.init({
proxy: 'http://mamp-site.dev'
});
gulp.watch('./src/scss/**/*.scss', ['sass']);
gulp.watch('./src/coffee/**/*.coffee', ['coffee']);
});
Browsersync successfully reloads my compiled JS every time I make a change, but won't reload my compiled CSS. I know placing browserSync.stream() after gulp.dest() is correct, because Browsersync says it only cares about your CSS when it's finished compiling. I also verified that my Sass is compiling.
Why would Browsersync work for my 'coffee' task but not my 'sass' task? This is my first go at gulp, so I'm hoping it's something simple.
EDIT: watching 'sass' only within the 'serve' task works, but not with both 'sass' and 'coffee'
I don't know why your sass is not reloading, but you should not be using browserSync.stream for your javascript. Perhaps the streams are getting mixed up. Your watch should be
gulp.watch('./src/coffee/**/*.coffee', ['coffee-watch']);
and there should be
gulp.task('coffee-watch', ['coffee'], function (done) {
browserSync.reload();
done();
});
Remove .pipe(browserSync.stream()); from your coffee task.

Gulp - JADE - browserSync not reload automatically

I use gulp for create. I use browser sync for see the preview of my pages. But I can't configure gulp to reload automatically. When I make a change in the browser, I have to reload manually only with jade files. I use and compile CSS to SCSS and this if change automatically...
Which one is my error, why gulp not reload my jade or html files in the preview?
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var sass = require('gulp-sass');
var jade = require('gulp-jade');
var autoprefixer = require('gulp-autoprefixer');
var minimifyCss = require('gulp-minify-css');
// JADE
gulp.task('jade', function() {
gulp.src('./jade/*.jade')
.pipe(jade({
pretty: true
}))
.pipe(gulp.dest('./'))
});
// SASS
gulp.task('sass', function() {
gulp.src('./sass/styles.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: true
}))
.pipe(minimifyCss({
keepBreaks: true
}))
.pipe(gulp.dest('./css'))
.pipe(browserSync.stream());
});
// browserSync
gulp.task('default', function() {
browserSync.init({
server: "./"
});
gulp.watch("./sass/styles.scss", ['sass']);
gulp.watch("./jade/*.jade", ['jade']);
gulp.watch("./*.html").on('change', browserSync.reload);
});
You are not including your jade or sass function in your parameters
gulp.task('default', ['sass'], ['jade'], function(){
gulp.watch("./sass/styles.scss", ['sass']);
gulp.watch("./jade/*.jade", ['jade']);
})
gulp default

Categories

Resources