Gulp watch causes infinite build loop - javascript

my gulp watch keeps on a rebuild after saving js file, the Scss file working well, the problem is when saving other files inside the project folder, changing or replacing images, triggers the rebuild to start and finish building dist folder.
only while saving Scss files, the build starts and finishes as accepted.
terminal output:
[17:53:11] Starting 'js-build'...
[17:53:11] Finished 'js-build' after 452 ms
[17:53:11] Starting 'js-build'...
[17:53:12] Finished 'js-build' after 614 ms
[17:53:12] Starting 'js-build'...
please advice?
my gulp code:
var gulp = require('gulp'),
autoprefixer = require('autoprefixer'),
babel = require('gulp-babel');
uglify = require('gulp-uglify-es').default,
cssnano = require('gulp-cssnano'),
plumber = require('gulp-plumber'),
gcmq = require('gulp-group-css-media-queries'),
rename = require('gulp-rename'),
pngquant = require('imagemin-pngquant'),
cache = require('gulp-cache'),
concat = require('gulp-concat'),
del = require('del'),
sass = require('gulp-sass'),
scss = require('gulp-scss'),
browserSync = require('browser-sync'),
server = browserSync.create(),
fileinclude = require('gulp-file-include');
function reload(done) {
server.reload();
done();
}
function serve(done) {
server.init({
server: {
baseDir: './dist'
}
});
done();
}
gulp.task('html', function() {
return gulp.src('*.html')
.pipe(fileinclude({
prefix: '##',
basepath: '#file'
}))
.pipe(gulp.dest('dist'))
});
var scssFiles = [
'./scss/**/*.scss',
'!./node_modules/**',
'!./dist/**',
];
var jsFiles = [
'**/*.js',
'!./node_modules/**',
'!gulpfile.js',
'!./dist/**',
];
var imgFiles = [
'**/*.{jpg,png,jpeg,svg,gif,ico}',
'!./node_modules/**',
'!./dist/**',
];
var fontFiles = [
'fonts/**/*.*',
'!./node_modules/**',
'!./dist/**',
];
gulp.task('scss', function() {
var postCSSplugins = [
autoprefixer({ browsers: ['last 2 version'] }),
];
return gulp.src(scssFiles)
.pipe(sass())
// .pipe(concat('app.css'))
.pipe(plumber()) //if need unoptimezed file - comment this line
.pipe(gcmq()) //if need unoptimezed file - comment this line
.pipe(cssnano()) //if need unoptimezed file - comment this line
.pipe(gulp.dest('dist/css/'))
.pipe(server.stream()) //if need unoptimezed file - comment this line
});
gulp.task('js-build', function () {
return gulp.src(jsFiles)
.pipe(plumber())
.pipe(babel({
presets: ['#babel/env']
}))
.pipe(uglify())
.pipe(gulp.dest('dist/'))
});
gulp.task('img', function() {
return gulp.src(imgFiles)
.pipe(gulp.dest('dist/'))
});
gulp.task('font', function() {
return gulp.src(fontFiles)
.pipe(gulp.dest('dist/fonts'))
});
gulp.task('watch', function() {
gulp.watch(scssFiles, gulp.series('scss'), reload);
gulp.watch('*.html', gulp.series('html'));
gulp.watch(jsFiles, gulp.series('js-build'));
gulp.watch(imgFiles, gulp.series('img'));
});
gulp.task('clean', function() {
return del('./dist');
});
gulp.task('build', gulp.parallel('scss', 'html', 'js-build', 'img', 'font'));
gulp.task('serve', gulp.parallel('watch', serve));
gulp.task('default', gulp.series('clean', 'build', 'serve'));

Related

Gulp error File not found with singular glob

After changing gulp 3 to 4 I receive the error when I try to run gulp build:
Error: File not found with singular glob: /assets/css/argon.css (if this was purposeful, use `allowEmpty` option)
The code was working before with gulp 3 (as I was told) and I made a couple of changes to be able to work with gulp 4 as well. The error is received after all tasks has run correcltly.
File is located where it is looking for it:
And here is my gulpfile.js:
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var csscomb = require('gulp-csscomb');
var cleanCss = require('gulp-clean-css');
var cssnano = require('gulp-cssnano');
var composer = require('gulp-uglify/composer');
var concat = require('gulp-concat');
var del = require('del');
var imagemin = require('gulp-imagemin');
var htmlPrettify = require('gulp-html-prettify');
var gulp = require('gulp');
var gulpIf = require('gulp-if');
var gulpRun = require('gulp-run');
var gulpUtil = require('gulp-util');
var npmDist = require('gulp-npm-dist');
var postcss = require('gulp-postcss');
var runSequence = require('gulp4-run-sequence');
// var sass = require('gulp-sass');
var uglifyEs = require('uglify-es');
var uglify = composer(uglifyEs, console);
var rename = require('gulp-rename');
var useref = require('gulp-useref-plus');
var wait = require('gulp-wait');
var sass = require('gulp-sass')(require('sass'));
// Define paths
var paths = {
dist: {
base: 'dist',
img: 'dist/assets/img',
libs: 'dist/assets/vendor'
},
base: {
base: './',
node: 'node_modules'
},
src: {
base: './',
css: 'assets/css',
html: '**/*.html',
img: 'assets/img/**/*.+(png|jpg|gif|svg)',
js: 'assets/js/**/*.js',
scss: 'assets/scss/**/*.scss'
}
}
// Compile SCSS
gulp.task('scss', async function() {
return gulp.src(paths.src.scss)
.pipe(wait(500))
.pipe(sass().on('error', sass.logError))
.pipe(postcss([require('postcss-flexbugs-fixes')]))
.pipe(autoprefixer({
browsers: ['> 1%']
}))
.pipe(csscomb())
.pipe(gulp.dest(paths.src.css))
.pipe(browserSync.reload({
stream: true
}));
});
// Minify CSS
gulp.task('minify:css', async function() {
return gulp.src([
paths.src.css + '/argon.css'
])
.pipe(cleanCss())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(paths.dist.base + '/css'))
});
// Concat JS files
gulp.task('concat:js', async function(done) {
files = [
paths.src.base + '/assets/js/components/license.js',
paths.src.base + '/assets/js/components/layout.js',
paths.src.base + '/assets/js/components/init/*js',
paths.src.base + '/assets/js/components/custom/*js',
paths.src.base + '/assets/js/components/maps/*js',
paths.src.base + '/assets/js/components/charts/*js',
paths.src.base + '/assets/js/components/vendor/*js'
];
return gulp
.src(files)
.pipe(concat("argon.js"))
.pipe(gulp.dest(paths.dist.base + '/js'));
done();
});
// Minify JS
gulp.task('minify:js', async function(cb) {
return gulp.src([
paths.dist.base + '/js/argon.js'
])
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(paths.dist.base + '/js'))
});
// Live reload
gulp.task('browserSync', async function() {
browserSync.init({
server: {
baseDir: [paths.src.base, paths.base.base]
},
})
});
// Watch for changes
gulp.task('watch', gulp.series('browserSync', 'scss', async function() {
gulp.watch(paths.src.scss, ['scss']);
gulp.watch(paths.src.js, browserSync.reload);
gulp.watch(paths.src.html, browserSync.reload);
}));
// Clean
gulp.task('clean:dist', async function() {
return del.sync(paths.dist.base);
});
// Copy CSS
gulp.task('copy:css', async function() {
return gulp.src([
paths.src.base + '/assets/css/argon.css'
])
.pipe(gulp.dest(paths.dist.base + '/css'))
});
// Copy JS
gulp.task('copy:js', async function() {
return gulp.src([
paths.src.base + '/assets/js/argon.js'
])
.pipe(gulp.dest(paths.dist.base + '/js'))
});
// Build
gulp.task('build', async function(callback) {
runSequence('clean:dist', 'scss', 'copy:css', 'copy:js', 'concat:js', 'minify:js', 'minify:css',
callback);
});
// Default
gulp.task('default', async function(callback) {
runSequence(['scss', 'browserSync', 'watch'],
callback
)
});
Any help would be appreciated :)
In copy:css you have this line:
paths.src.base + '/assets/css/argon.css'
where apparently your error is. The problem is your paths.src.base is defined as
base: './' to which you are adding /assets/css/argon.css so you end up with
.//assets/css/argon.css note the two leading backslashes.
Get rid of the leading backslash in the /assets/... and check the rest of your code for the same problem.
Also since you are using gulp4 get rid of runSequence - look at the documentation for gulp.series.

Gulp watch task with Browsersync with Jekyll does not propagate modified content

When calling gulp the site and its assets are generated correctly once. The watch tasks start, but they never register new changes nor refresh the browser with Browsersync.
My goal is to have a full automation in the build process. For example, if _config.yml changes Jekyll will rebuild itself. Same idea applies to every file.
What I have tried so far:
Creating separate gulp.watch tasks for build:scripts, build:styles, build:images and build:jekyll.
Calling .on('change', browserSync.reload) on every separate gulp.watch task
Creating .pipe(browserSync.reload({stream: true})) in build:styles, build:images, build:scripts and build:jekyll
Here are the contents of gulpfile.js:
'use strict';
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var concat = require('gulp-concat');
var del = require('del');
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var rename = require('gulp-rename');
var run = require('gulp-run');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
gulp.task('build:styles', function() {
return gulp.src('_assets/styles/main.sass')
.pipe(sass({
outputStyle: 'compressed'
}))
.pipe(autoprefixer({
browsers: ['last 4 versions']
}))
.pipe(rename('main.min.css'))
.pipe(gulp.dest('assets/styles'))
.pipe(browserSync.stream());
});
gulp.task('build:scripts', function() {
return gulp.src('_assets/js/*.js')
.pipe(concat('main.js'))
.pipe(uglify())
.pipe(rename('main.min.js'))
.pipe(gulp.dest('assets/js'))
.pipe(browserSync.stream());
});
gulp.task('build:images', function() {
return gulp.src('_assets/img')
.pipe(imagemin())
.pipe(gulp.dest('assets/img'));
});
gulp.task('build:jekyll', function(callback) {
// --incremental regeneration doesn't update front matter
var shellCommand = 'jekyll build';
return gulp.src('')
.pipe(run(shellCommand))
browserSync.reload();
callback();
});
gulp.task('clean', function() {
return del(['_site', 'assets']);
});
gulp.task('build', function(callback) {
return runSequence('clean', ['build:scripts', 'build:styles', 'build:images'], 'build:jekyll', callback)
});
gulp.task('watch', ['build'], function() {
browserSync.init({
server: {
baseDir: '_site'
},
open: true
});
gulp.watch(['_config.yml' ,
'*.html', '_layouts/*.*',
'_pages/*.*', '_assets/**/**/*.*'],
['build']).on('change', browserSync.reload);
});
gulp.task('default', ['watch']);
Why would Browsersync and Jekyll register the changes correctly?
I have managed to get Browsersync see all the changes in the needed files and reload browser with the following gulpfile.js tasks:
'use strict';
var autoprefixer = require('gulp-autoprefixer');
var browserSync = require('browser-sync').create();
var concat = require('gulp-concat');
var del = require('del');
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var rename = require('gulp-rename');
var run = require('gulp-run');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
gulp.task('clean', function() {
return del(['_site', '.publish', 'assets']);
});
gulp.task('build:scripts', function() {
return gulp.src('_assets/js/*.js')
.pipe(concat('main.js'))
.pipe(uglify())
.pipe(rename('main.min.js'))
.pipe(gulp.dest('assets/js'));
});
gulp.task('build:styles', function() {
return gulp.src('_assets/styles/main.sass')
.pipe(sass({
outputStyle: 'compressed'
}))
.pipe(autoprefixer({
browsers: ['last 4 versions']
}))
.pipe(rename('main.min.css'))
.pipe(gulp.dest('assets/styles'));
});
gulp.task('build:images', function() {
return gulp.src('_assets/img')
.pipe(imagemin())
.pipe(gulp.dest('assets/img'));
});
gulp.task('build:jekyll', function(callback) {
// --incremental regeneration doesn't update front matter
var shellCommand = 'jekyll build';
return gulp.src('')
.pipe(run(shellCommand));
callback();
});
gulp.task('build', function(callback) {
return runSequence('clean', ['build:scripts', 'build:styles', 'build:images'], 'build:jekyll', callback)
});
gulp.task('rebuild', ['build'], function(){
browserSync.reload();
});
gulp.task('default', ['build'], function() {
// maybe important (injectChanges: true)
browserSync.init({
server: {
baseDir: '_site'
},
open: true
});
gulp.watch(['_config.yml' ,
'*.html', '_layouts/*.*',
'_pages/*.*', '_assets/**/**/*.*'],
['rebuild']);
});

When I run gulp, unexpected error pops up(event.js : 160)

Here is the snapshot of command prompt when I run gulp. Previously it used to work correctly but now, it is showing some weird errors.
my view folder is supposed to contain only templates, so why is it displaying bootstrap.social.css not found?
and my project folder looks like-
Project-
app-
fonts-...
images-...
scripts-
app.js
controllers.js
services.js
styles-...
index.html
views-
various html templates
bower_components-...
dist-..
node_modules-...
gulpfile.js
package.json
...
here is my gulpfile.js' code:
var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
uglify = require('gulp-uglify'),
usemin = require('gulp-usemin'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
changed = require('gulp-changed'),
rev = require('gulp-rev'),
browserSync = require('browser-sync'),
ngannotate = require('gulp-ng-annotate'),
del = require('del');
gulp.task('jshint', function() {
return gulp.src('app/scripts/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
// Clean
gulp.task('clean', function() {
return del(['dist']);
});
// Default task
gulp.task('default', ['clean'], function() {
gulp.start('usemin', 'imagemin','copyfonts');
});
gulp.task('usemin',['jshint'], function () {
return gulp.src('./app/**/*.html')
.pipe(usemin({
css:[minifycss(),rev()],
js: [ngannotate(),uglify(),rev()]
}))
.pipe(gulp.dest('dist/'));
});
// Images
gulp.task('imagemin', function() {
return del(['dist/images']), gulp.src('app/images/**/*')
.pipe(cache(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })))
.pipe(gulp.dest('dist/images'))
.pipe(notify({ message: 'Images task complete' }));
});
gulp.task('copyfonts', ['clean'], function() {
gulp.src('./bower_components/font-awesome/fonts/**/*.{ttf,woff,eof,svg}*')
.pipe(gulp.dest('./dist/fonts'));
gulp.src('./bower_components/bootstrap/dist/fonts/**/*.{ttf,woff,eof,svg}*')
.pipe(gulp.dest('./dist/fonts'));
});
// Watch
gulp.task('watch', ['browser-sync'], function() {
// Watch .js files
gulp.watch('{app/scripts/**/*.js,app/styles/**/*.css,app/**/*.html}', ['usemin']);
// Watch image files
gulp.watch('app/images/**/*', ['imagemin']);
});
gulp.task('browser-sync', ['default'], function () {
var files = [
'app/**/*.html',
'app/styles/**/*.css',
'app/images/**/*.png',
'app/scripts/**/*.js',
'dist/**/*'
];
browserSync.init(files, {
server: {
baseDir: "dist",
index: "index.html"
}
});
// Watch any files in dist/, reload on change
gulp.watch(['dist/**']).on('change', browserSync.reload);
});

gulpfile.js is not concatenating all css after sass build

This is my gulpfile.js
"use strict";
var gulp = require('gulp');
var connect = require('gulp-connect'); //Runs a local dev server
var open = require('gulp-open'); //Open a URL in a web browser
var browserify = require('browserify'); // Bundles JS
var reactify = require('reactify'); // Transforms React JSX to JS
var source = require('vinyl-source-stream'); // Use conventional text streams with Gulp
var concat = require('gulp-concat'); //Concatenates files
var lint = require('gulp-eslint'); //Lint JS files, including JSX
var livereload = require('gulp-livereload');
var sass = require('gulp-sass');
var config = {
port: 9005,
devBaseUrl: 'http://localhost',
paths: {
html: './src/*.html',
js: './src/**/*.js',
scss: [
'node_modules/font-awesome/scss/font-awesome.scss',
'./src/scss/default.scss'
],
sassBuilds: './src/builds/css/',
css: [
'node_modules/bootstrap/dist/css/bootstrap.min.css',
'node_modules/bootstrap/dist/css/bootstrap-theme.min.css',
'./src/builds/css/*.css'
],
fonts:[
'node_modules/font-awesome/fonts/**.*'
],
dist: './dist',
mainJs: './src/main.js'
}
}
//Start my local development server
gulp.task('connect', function() {
connect.server({
root: ['dist'],
port: config.port,
base: config.devBaseUrl,
livereload: true
});
});
gulp.task('open', ['connect'], function() {
gulp.src('dist/index.html')
.pipe(open({ uri: config.devBaseUrl + ':' + config.port + '/', app: 'Chrome'}));
});
gulp.task('livereload', function (){
gulp.src('./src/**/*')
.pipe(connect.reload());
});
gulp.task('html', function() {
gulp.src(config.paths.html)
.pipe(gulp.dest(config.paths.dist))
.pipe(connect.reload());
});
gulp.task('js', function() {
browserify(config.paths.mainJs)
.transform(reactify) //this would 'bundle' the JSX
.bundle()
.on('error', console.error.bind(console))
.pipe(source('bundle.js'))
.pipe(gulp.dest(config.paths.dist + '/scripts'))
.pipe(connect.reload());
});
gulp.task('css', function() {
gulp.src(config.paths.css)
.pipe(concat('bundle.css'))
.pipe(gulp.dest(config.paths.dist + '/css'));
});
gulp.task('lint', function() {
return gulp.src(config.paths.js)
.pipe(lint({config: 'eslint.config.json'}))
.pipe(lint.format());
});
gulp.task('watch', function() {
gulp.watch(config.paths.html, ['html', 'livereload']);
gulp.watch(config.paths.scss, ['sass', 'livereload']);
gulp.watch(config.paths.js, ['js', 'lint']);
});
gulp.task('sass', function () {
return gulp.src(config.paths.scss)
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(config.paths.sassBuilds));
});
gulp.task('icons', function() {
return gulp.src(config.paths.fonts)
.pipe(gulp.dest(config.paths.dist + '/fonts'));
});
gulp.task('default', ['html', 'sass', 'js', 'lint', 'css', 'icons', 'open', 'watch']);
sass task first runs to create 2 css files into builds/css/
After that css task runs and should concat all css files from the config.paths.css but it doesn't, only watch task does and barely, sometimes it does, sometimes it doesn't (seriously). I feel there is some spaghetti going on here.
If you are expecting that the sass task will always finish before the css task starts Don't. From the gulp documentation
Note: The tasks will run in parallel (all at once), so don't assume that the tasks will start/finish in order.
The usual way to fix this is to make the css task wait for the sass task to finish by:
gulp.task('css', ['sass'], function() {

Error when running Gulp

I am setting up gulp for my project,and get this message when I run it.
stream.js:74
throw er; // Unhandled stream error in pipe.
^
Error: EEXIST: file already exists, mkdir
'C:\Users\Theodosios\Desktop\AngularJs
\Week4\conFusion\dist\fonts'
at Error (native)
I am spending hours to see what's wrong,but I don't know what's happening. So here is my gulp code.
var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
uglify = require('gulp-uglify'),
usemin = require('gulp-usemin'),
imagemin = require('gulp-imagemin'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
changed = require('gulp-changed'),
rev = require('gulp-rev'),
browserSync = require('browser-sync'),
del = require('del'),
ngannotate = require('gulp-ng-annotate');
gulp.task('jshint', function () {
return gulp.src('app/scripts/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish', {beep: true}));
});
// Clean
gulp.task('clean', function () {
return del(['dist']), del(['../json-server/public'], {force: true});
});
// Default task
gulp.task('default', ['clean'], function () {
gulp.start('usemin', 'imagemin', 'copyfonts');
});
gulp.task('usemin', ['jshint'], function () {
return gulp.src('./app/**/*.html')
.pipe(usemin({
css: [minifycss(), rev()],
js: [ngannotate(), uglify(), rev()]
}))
.pipe(gulp.dest('dist/'))
.pipe(gulp.dest('../json-server/public/'));
});
// Images
gulp.task('imagemin', function () {
return del(['dist/images']), gulp.src('app/images/**/*')
.pipe(cache(imagemin({optimizationLevel: 3, progressive: true,
interlaced: true})))
.pipe(gulp.dest('dist/images')).pipe(gulp.dest('../json-
server/public/images'))
.pipe(notify({message: 'Images task complete'}));
});
gulp.task('copyfonts', ['clean'], function () {
gulp.src('./bower_components/font-awesome/fonts/**/*.{ttf,woff,eof,svg}*')
.pipe(gulp.dest('./dist/fonts')).pipe(gulp.dest('../json-
server/public/fonts'));
gulp.src('./bower_components/bootstrap/dist/fonts/**/*.{ttf,woff,eof,svg}*')
.pipe(gulp.dest('./dist/fonts')).pipe(gulp.dest('../json-
server/public/fonts'));
});
// Watch
gulp.task('watch', ['browser-sync'], function () {
// Watch .js files
gulp.watch('{app/scripts/**/*.js,app/styles/**/*.css,app/**/*.html}',
['usemin']);
// Watch image files
gulp.watch('app/images/**/*', ['imagemin']);
});
gulp.task('browser-sync', ['default'], function () {
var files = [
'app/**/*.html',
'app/styles/**/*.css',
'app/images/**/*.png',
'app/scripts/**/*.js',
'dist/**/*'
];
browserSync.init(files, {
server: {
baseDir: "dist",
index: "index.html"
}
});
// Watch any files in dist/, reload on change
gulp.watch(['dist/**']).on('change', browserSync.reload);
});
Any ideas? There is something wrong in one the pipes of node stream,but can't see where is the error.

Categories