gulp partials causes browser sync errors - gulp

When I use partials on the scss structure, I have to deal with frequent browser sync error (like reloading forever). Notice that it is an intermittent error so it is not a common compiling problem (although it might be somewhat related) and it doesn't happen when I don't use partials. Also, I don't think it is project related or a gulpfile issue either, since it occurs with any project and I have tried more than one gulpfile structure. Anyway, you can check it out below:
var gulp = require('gulp');
var sass = require('gulp-sass');
var clean = require('gulp-clean');
var browserSync = require('browser-sync').create();
var autoprefixer = require('gulp-autoprefixer');
gulp.task('styles', function () {
gulp.src('src/scss/app.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gulp.dest('src/css'))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('copy', ['clean'], function () {
return gulp.src('src/**/*')
.pipe(gulp.dest('dist'));
});
gulp.task('clean', function () {
return gulp.src('dist')
.pipe(clean());
});
gulp.task('serve',function () {
browserSync.init({
server: {
baseDir: 'src/'
}
});
gulp.watch('src/scss/*.scss', ['styles']);
gulp.watch('src/**/*').on('change', browserSync.reload)
});
gulp.task('default', ['styles', 'serve']);

A couple of things that might help: (1) add a return statement in your 'styles' task and (2) remove the second watch because it calls browserSync.reload which is already called at the end of the 'styles' task and you don't need to call it twice. So make these changes:
gulp.task('styles', function () {
// added return below
return gulp.src('src/scss/app.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gulp.dest('src/css'))
.pipe(browserSync.reload({stream: true}));
});
and the second watch is unnecessary and possibly a problem:
gulp.task('serve',function () {
browserSync.init({
server: {
baseDir: 'src/'
}
});
gulp.watch('src/scss/*.scss', ['styles']);
// remove below watch
// gulp.watch('src/**/*').on('change', browserSync.reload)
gulp.watch("./*.html").on("change", browserSync.reload);
});

Related

gulp-requirejs: Did you forget to signal async completion?

Just attempting to move from grunt to gulp and learning as I go. One item which has had me scratching my head is the error "Did you forget to signal async completion?" when trying to run requirejs (js:rjs in the gulpfile below). My very basic gulpfile.js is:
'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
var rjs = require('gulp-requirejs');
gulp.task('sass', function()
{
return gulp.src('./*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./'));
});
gulp.task('sass:watch', function()
{
gulp.watch('./*.scss', gulp.series('sass'));
});
gulp.task('js:rjs', function()
{
return rjs({
baseUrl: "./script.js",
out: "./script.rjs.js",
})
.pipe(gulp.dest('./'));
});
The answer at https://stackoverflow.com/a/36899424/1424591 seems pretty exhaustive but I want to know how I can identify which of these solutions is required here and why. I have tried the callback method
gulp.task('js:rjs', function(done)
{
return rjs({
baseUrl: "./script.js",
out: "./script.rjs.js",
})
.pipe(gulp.dest('./'));
done();
});
which results in the same issue.

Gulp workflow for Express with SASS, BrowserSync, Uglify and Nodemon

Problems I'm facing:
The browser doesn't reflect live changes in .scss or .js
I don't understand whether we have to return the stream from the gulp.task() or not, I visited some websites and watched lectures some of which used return and some didn't.
Cannot understand the flow of execution of gulpfile (which statement runs first, then which and so on)
This is my current code of gulpfile.js.
"use strict";
var gulp = require('gulp');
var sass = require('gulp-sass');
var nodemon = require('gulp-nodemon');
var browserSync = require('browser-sync').create();
var uglify = require('gulp-uglify');
gulp.task('default', ['nodemon'], function(){
gulp.watch("src/sass/*.scss", ['sass']);
gulp.watch("src/js/*.js", ['js']);
gulp.watch("views/*.ejs").on('change',browserSync.reload); //Manual Reloading
})
// Process JS files and return the stream.
gulp.task('js', function () {
return gulp.src('src/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('public/javascripts'));
});
// Compile SASS to CSS.
gulp.task('sass', function(){
// gulp.src('src/sass/*.scss') //without return or with return? why?
return gulp.src('src/sass/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('public/stylesheets'))
.pipe(browserSync.stream());
});
// Setup proxy for local server.
gulp.task('browser-sync', ['js','sass'], function() {
browserSync.init(null, {
proxy: "http://localhost:3000",
port: 7000,
});
});
gulp.task('nodemon', ['browser-sync'], function(cb){
var running = false;
return nodemon({script: 'bin/www'}).on('start', function(){
if(!running)
{
running = true;
cb();
}
});
})
You may look at project structure at https://github.com/DivyanshBatham/GulpWorkflow
Try this:
"use strict";
var gulp = require('gulp');
var sass = require('gulp-sass');
var nodemon = require('gulp-nodemon');
var browserSync = require('browser-sync').create();
var uglify = require('gulp-uglify');
// First, run all your tasks
gulp.task('default', ['nodemon', 'sass', 'js'], function(){
// Then watch for changes
gulp.watch("src/sass/*.scss", ['sass']);
gulp.watch("views/*.ejs").on('change',browserSync.reload); //Manual Reloading
// JS changes need to tell browsersync that they're done
gulp.watch("src/js/*.js", ['js-watch']);
})
// create a task that ensures the `js` task is complete before
// reloading browsers
gulp.task('js-watch', ['js'], function (done) {
browserSync.reload();
done();
});
// Process JS files and return the stream.
gulp.task('js', function () {
return gulp.src('src/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('public/javascripts'));
});
// Compile SASS to CSS.
gulp.task('sass', function(){
return gulp.src('src/sass/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('public/stylesheets'))
.pipe(browserSync.stream());
});
// Setup proxy for local server.
gulp.task('browser-sync', ['js','sass'], function() {
browserSync.init(null, {
proxy: "http://localhost:3000",
port: 7000,
});
});
gulp.task('nodemon', ['browser-sync'], function(cb){
var running = false;
return nodemon({script: 'bin/www'}).on('start', function(){
if(!running)
{
running = true;
cb();
}
});
})
Also, you should consider adding the JS file to your index.ejs
Eg: <script src='/javascripts/main.js'></script>
More help: https://browsersync.io/docs/gulp
I'll answer what I can.
You don't always have to return the stream in a gulp task but you should since you have some tasks, like 'browser-sync' that are dependent on other tasks, ['js','sass'], finishing. The purpose of returning the stream is to signal task completion. It is not the only way to signal task completion but one easy way.
You are already doing this in your ['js','sass'] tasks with the return statements.
Your 'js' task needs a .pipe(browserSync.stream()); statement at the end of it like your 'sass' task. Or try .pipe(browserSync.reload({stream:true})); sometimes that variant works better.
You have both browser-sync and nodemon running - that I believe is unusual and may cause problems - they do much the same thing and do not typically see them running together. I would eliminate nodemon from your file.
Flow of execution:
(a) default calls ['nodemon', 'sass', 'js'] : these run in parallel.
(b) nodemon call 'browser-sync'. 'browserSync' must finish setting up before 'nodemon' gets into its function.
(c) 'browser-sync', ['js','sass'], Here browser-sync is dependent upon 'js' and 'sass' which run in parallel and must finish and signal that they have finished by returning the stream for example before browser-sync continues.
(d) After 'js', 'sass', 'browser-sync' and 'nodemon' have completed, your watch statements are set up and begin to watch.

Why am I getting a "JavaScript heap out of memory" on gulp watch?

I have a pretty simple and straightforward gulpfile for sass compilation, concat, minification and browser reload on changes using a watch task that observes for *.php, *.js and *.sass.
require('es6-promise').polyfill();
var gulp = require('gulp'),
sass = require('gulp-sass');
var concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
livereload = require('gulp-livereload');
var config = {
scripts: [
'./bower_components/jquery/dist/jquery.min.js',
'./bower_components/materialize/dist/js/materialize.js',
'./js/**/*.js'
]
};
gulp.task('sass', function() {
return gulp.src('./sass/style.scss')
.pipe(sass.sync({
includePaths: ['./bower_components/materialize/sass'],
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(livereload())
.pipe(gulp.dest('./'));
});
gulp.task('scripts', function() {
return gulp.src(config.scripts)
.pipe(concat('scripts.js'))
.pipe(gulp.dest('./js/'))
.pipe(uglify())
.pipe(rename({
extname: '.min.js'
}))
.pipe(livereload())
.pipe(gulp.dest('./js/'));
});
gulp.task('copyassets', function(){
})
// default task
gulp.task('default', ['sass', 'scripts']);
gulp.task('watch', function () {
livereload.listen(35729);
gulp.watch('**/*.php').on('change', function(file) {
livereload.changed(file.path);
});
gulp.watch('./sass/**/*.scss', ['sass']);
gulp.watch('./js/**/*.js', ['scripts']);
});
I execute the default task with gulp, and it was working fine for me until a few hours ago. All of a sudden, I have started getting FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory on SASS changes.
I have tried allocating it more space with '--max-old-space-size=8192', but to no use.
I have reverted all recent changes too in my code base, but that did not seem to fix it too.
Any help or pointers will be appreciated.
I am using this in combination with a _s WP theme.

Gulp browser-sync not reloading

This is my gulpfile.js file. It doesn't reload when I change and save my css or html, but it shows the "Connected to Browser-sync" message when I use gulp serve. I can't seem to figure out what is wrong.
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var gulp = require('gulp');
gulp.task('styles', function() {
gulp.src('./css/*.css')
.pipe(gulp.dest('./css'))
});
//Watch task
gulp.task('default',function() {
gulp.watch('./css/*.css',['styles']);
});
gulp.task('js', function () {
return gulp.src('./js/*.js')
// .pipe(concat('all.js'))
.pipe(gulp.dest('./js'));
});
gulp.task('html', function () {
return gulp.src('./*.html')
.pipe(gulp.dest('./'));
});
gulp.task('js-watch', ['js'], browserSync.reload);
gulp.task('html-watch', ['html'], browserSync.reload);
gulp.task('css-watch', ['css'], browserSync.reload);
gulp.task('serve', ['js', 'html', 'styles'], function () {
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch("./*.js", ['js-watch']);
gulp.watch("./*.html", ['html-watch']);
gulp.watch("./css/*.css", ['css-watch']);
});
Try this.
All i did was add a new dependency.. run-sequence, to make it easy to organize how things are run. All you need is to run gulp to start the script and everything should run perfectly.
Aside from that i created a site folder at the root of my project. just because it seems more organize that way, and if you ever think of using jade, or sass, scss, etc. in your project and all you have to do is change the src path and keep the rendered output in the site folder..
Aside from that, everything is the same.
EDIT
I forgot to mention that i also added the pipe(bs.stream()); line at the end of each task such as HTML, styles, and JS, so that the browser reloads every time you make a change.
In case you don't want to make the server folder to keep your project organized, the all you have to do is delete the site/ path where ever you see it. and for server: 'site' just replace site with ./. Port you can delete it or define your own port
var gulp = require('gulp'),
bs = require('browser-sync').create(),
sequence = require('run-sequence');
gulp.task('styles', function() {
gulp.src('site/css/*.css')
.pipe(gulp.dest('site/css'))
.pipe(bs.stream());
});
gulp.task('js', function() {
gulp.src(['site/js/*.js'])
// .pipe(concat('all.js'))
.pipe(gulp.dest('site/js'))
.pipe(bs.stream());
});
gulp.task('html', function() {
gulp.src('site/*.html')
.pipe(gulp.dest('site'))
.pipe(bs.stream());
});
gulp.task('browser-sync', function() {
bs.init({
server: 'site',
port: 3010
});
});
gulp.task('watch', ['build'], function() {
gulp.watch(['site/css/*.css'], ['styles']);
gulp.watch(['site/js/*.js'], ['js']);
gulp.watch(['site/*.html'], ['html']);
});
gulp.task('build', function(done) {
sequence(
['html', 'js', 'styles'],
'browser-sync',
done);
});
gulp.task('default', ['watch']);

Gulp-minify-css does not produce output files

I have set up a very simple gulpfile.js There are only two task - 'sass' and 'minify-js'. These two tasks are fired by the task 'watch' when a change is detected. It all seems to be working well: Gulp is listening for changes, *.scss files are compiled into CSS, the console generates output as expected, without any errors. However, the CSS files do not get minified there are no output files from the 'minify-css' task whatsoever.
Why is 'minify-css' not working? What am I missing here?
This is my gulpfile.js:
var gulp = require('gulp');
var sass = require('gulp-sass');
var watch = require('gulp-watch');
var minifyCSS = require('gulp-minify-css');
gulp.task('sass', function() {
gulp.src('plugins/SoSensational/styles/sass/*.scss')
.pipe(sass())
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'));
});
gulp.task('minify-css', function() {
gulp.src('plugins/SoSensational/styles/dest/*.css')
.pipe(minifyCSS())
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'));
});
gulp.task('watch', function() {
gulp.watch('plugins/SoSensational/styles/sass/*.scss', ['sass', 'minify-css']);
});
Sounds like a race condition. Sass and MinifyCSS are executed in parallel, might be that your Sass task isn't done when you're already running MinifyCSS. Sass should be a dependency, so you have two options:
Make Sass a dependency from minifycss:
gulp.task('minify-css', ['sass'], function() {
return gulp.src('plugins/SoSensational/styles/dest/*.css')
.pipe(minifyCSS())
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'));
});
gulp.task('watch', function() {
gulp.watch('plugins/SoSensational/styles/sass/*.scss', ['minify-css']);
});
Have one task that does both!
gulp.task('sass', function() {
return gulp.src('plugins/SoSensational/styles/sass/*.scss')
.pipe(sass())
.pipe(minifyCSS())
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'));
});
The latter one is actually the preferred version. You save yourself a lot of time if you don't have an intermediate result
Btw: Don't forget the return statements
I know this is kind of an old question but thought I'd throw this out there because it's something that's helped me. To build on the answer from ddprrt, I'd recommend changing:
gulp.task('sass', function() {
return gulp.src('plugins/SoSensational/styles/sass/*.scss')
.pipe(sass())
.pipe(minifyCSS())
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'));
});
to:
var rename = require('gulp-rename');
gulp.task('sass', function() {
return gulp.src('plugins/SoSensational/styles/sass/*.scss')
.pipe(sass('site.css'))
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'))
.pipe(minifyCSS())
.pipe(rename('site.min.css'))
.pipe(gulp.dest('plugins/SoSensational/styles/dest/'));
});
This allows you to debug with the un-minified CSS and deploy the minified version.
It's because the gulp-minify-css is deprecated. Use gulp-clean-css instead.
Click here(https://www.npmjs.com/package/gulp-minify-cssĀ "npm-clean-css")!