I have a gulpfile.js with this code:
var gulp = require('gulp'),
less = require('gulp-less'),
path_less = require('path'),
browserSync = require('browser-sync').create();
var path = {...paths...};
gulp.task('less', function(done){
gulp.src(path.src.def + path.src.less + '*.less')
.pipe(less({paths: [ path_less.join(__dirname, 'less', 'includes') ]}))
.pipe(gulp.dest(path.dev.def + path.dev.css))
.pipe(browserSync.stream());
done();
});
gulp.task('serve', gulp.series('less'), function(done){
browserSync.init({
server: './',
proxy : 'belaz.dev'
});
gulp.watch(path.src.def + path.src.less + '*.less', gulp.series('less'))
gulp.watch(path.dev.def + path.dev.css + '*.css').on('change', browserSync.reload());
done();
});
gulp.task('default', gulp.series('serve'), function(done){
done();
});
When i run Gulp browsersync doesn't do anything - all tasks starts, ends and nothing happens
How or why? And how i could fix it
You have a couple of unrelated problems. This
gulp.task('serve', gulp.series('less'), function(done){ // gulp v3 syntax, three arguments
change to:
gulp.task('serve', gulp.series('less', function(done){ // gulp v4 syntax, two arguments
and then when I run the fixed version I get this error:
Invalid config. You cannot specify both server & proxy options.
because of your
browserSync.init({
server: './', // can't have both a server and proxy option apparently
proxy : 'belaz.dev'
});
Related
I am using Gulp to compile and minify my SASS. This works fine, but I want to extend the automation by using BrowserSync.
I've followed the instructions on a few tutorial sites but cannot get it working - the browser does not refresh when I update either my .scss or .html files, not do any errors appear on the Terminal.
My gulpfile.js is as such. Would anyone know why browserSync fails to run?
(ps running gulp browserSync does sucessfully open the browser window and index file with http://localhost:3000/ but there is no automatic refreshing).
const gulp = require('gulp');
const sass = require('gulp-sass');
const cssnano = require('gulp-cssnano');
const browserSync = require('browser-sync').create();
// Normal .scss compilation
gulp.task('sass', function(){
return gulp.src('scss/main.scss')
.pipe(sass())
.pipe(gulp.dest('dist/css'))
});
// Minifys .css, is meant to reload browser
gulp.task('mini-css', function() {
return gulp.src('dist/css/main.css')
.pipe(cssnano())
.pipe(gulp.dest('dist/css'))
.pipe(browserSync.reload({
stream: true
}));
});
// Do both of the above
gulp.task('do-sass', gulp.series('sass', 'mini-css'))
gulp.task('watch', function(){
gulp.watch('scss/**/*.scss', gulp.series('do-sass'));
gulp.watch("*.html").on('change', browserSync.reload);
});
gulp.task('browserSync', function() {
browserSync.init({
server: {
baseDir: './'
},
})
})
Your watch task should be your default task. Try to put the browserSync.init() in the watch task und then start your gulp with gulp watch
const gulp = require('gulp');
const sass = require('gulp-sass');
const cssnano = require('gulp-cssnano');
const browserSync = require('browser-sync').create();
// Normal .scss compilation
gulp.task('sass', function(){
return gulp.src('scss/main.scss')
.pipe(sass())
.pipe(gulp.dest('dist/css'))
});
// Minifys .css, is meant to reload browser
gulp.task('mini-css', function() {
return gulp.src('dist/css/main.css')
.pipe(cssnano())
.pipe(gulp.dest('dist/css'))
.pipe(browserSync.reload({
stream: true
}));
});
// Do both of the above
gulp.task('do-sass', gulp.series('sass', 'mini-css'))
gulp.task('watch', function(){
browserSync.init({
server: {
baseDir: './'
}
});
gulp.watch('scss/**/*.scss', gulp.series('do-sass'));
gulp.watch("*.html").on('change', browserSync.reload);
});
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', done => {
gulp.src('./css/**/*.sass')
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'expanded'}).on('error', sass.logError))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('.'));
done()
});
gulp.task('sass:watch', done => {
gulp.watch('./css/**/*.sass', gulp.series('sass'));
done()
});
gulp.task('compressed-js', done => {
gulp.src('js/src/*.js')
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('js'));
done()
});
gulp.task('compressed-js:watch', done => {
gulp.watch('./js/src/*.js', gulp.series('compressed-js'));
done()
});
In the phpstorm getting this error: "ReferenceError: sourcemaps is not defined"
The error is pretty self-explanatory: you're referring to the sourcemaps variable while it doesn't exist.
Normally, you would need to declare it at the top of your gulpfile, like so:
var sourcemaps = require('gulp-sourcemaps');
However, Gulp 4 has sourcemap functionality built-in, so you can rewrite your sass task like this:
gulp.task('sass', () =>
gulp.src('./css/**/*.sass', { sourcemaps: true })
.pipe(sass({ outputStyle: 'expanded' }).on('error', sass.logError))
.pipe(gulp.dest('.', { sourcemaps: '.' }))
);
Note that I removed the done callback, which isn't needed if you just return the stream.
Note that you will run into the same problem when running the compressed-js task. You'll need to define uglify and rename:
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
And make sure the packages you require are installed.
var gulp = require('gulp')
uglify = require('gulp-uglify');
gulp.task('default', function(){
gulp.src('js/*.js')
.pipe(uglify())
.pipe(gulp.dest('minjs'));
});
Please don't post errors as images next time.
You need to return the pipeline from the function:
gulp.task("default", function() {
return gulp
.src("js/*.js")
.pipe(uglify())
.pipe(gulp.dest("minjs"));
});
In my project i use gulp + browsersync. After update gulp v.4.0.0., browsersync not working and my styles not compiled when watch run (only after in manual restart gulp watch).
This is my gulpfile.js:
var gulp = require('gulp'),
sass = require('gulp-sass'),
browserSync = require('browser-sync'),
concat = require('gulp-concat'),
uglify = require('gulp-uglifyjs'),
cssnano = require('gulp-cssnano'),
rename = require('gulp-rename'),
del = require('del'),
imagemin = require('gulp-imagemin'),
pngquant = require('imagemin-pngquant'),
autoprefixer = require('gulp-autoprefixer');
gulp.task('sass', function(){
return gulp.src('app/scss/**/*.scss')
.pipe(sass())
.pipe(autoprefixer(['last 15 versions', '> 1%'], {cascade: true}))
.pipe(cssnano())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('css'))
.pipe(browserSync.reload({stream: true}))
});
gulp.task('css-libs', gulp.parallel('sass'), function(){
return gulp.src('app/css/main.css')
.pipe(cssnano())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('app/css'));
});
gulp.task('browser-sync', function(){
browserSync({
server: {
//baseDir: 'app'
baseDir: './'
},
notify:false
})
});
gulp.task('watch', gulp.parallel('browser-sync', 'css-libs'), function(){
gulp.watch('app/scss/*.scss', ['sass']);
gulp.watch('*.html', browserSync.reload);
});
How to fix it?
UPD
This is my package.json
Gulp-watch not working when i change scss file.
I believe your 'watch' task should be rewritten like so:
gulp.task('browser-sync', function(){
// note the .init
browserSync.init({
server: {
//baseDir: 'app'
baseDir: './'
},
notify:false
})
});
gulp.task('watch', gulp.parallel('browser-sync', 'css-libs', function(done){
// change in the previous line and the following line
gulp.watch('app/scss/*.scss', gulp.series('sass'));
gulp.watch('*.html', browserSync.reload);
done();
}));
so that the function call with the watches is part of the gulp.parallel call. See the parallel tasks documentation and gulp.task signature.
gulp.task('default', gulp.parallel('one', 'two', function(done) {
// do more stuff
done();
}));
I have created a gulpfile.js which works fine so far but as soon I try to add browser-sync and I change my .scss-file I get this error ParseError: unexpected token and I have no clue why this happens. Another thing is, that even though in my server.js I have set app.listen(3000) it opens a window with localhost:3001???
Here is my gulpfile.js:
var gulp = require('gulp'),
connect = require('gulp-connect'),
uglify = require('gulp-uglify'),
sass = require('gulp-ruby-sass'),
webserver = require('gulp-webserver'),
browserify = require('gulp-browserify'),
browserSync = require("browser-sync").create();
gulp.task('browser-sync', function(){
browserSync.init({
server: {
baseDir: "./public"
}
});
})
gulp.task('styles', function() {
return sass('public/scss/*.scss')
.pipe(browserify())
.pipe(uglify())
.pipe(gulp.dest('public/css'));
});
gulp.task('watch', function(){
gulp.watch('public/scss/*.scss', ['styles'])
})
gulp.task('webserver', function() {
gulp.src('root')
.pipe(webserver({
livereload: true,
directoryListing: true,
port: 3000
}));
});
gulp.task('default', ['webserver', 'watch', 'browser-sync'])
So, does anyone know what I'm doing wrong??