I have set up gulp so that it will reload when the gulpfile is modified. That basic process is working, but for some reason it seems the old tasks are not being terminated properly.
var gulp = require('gulp');
var less = require('gulp-less');
var spawn = require('child_process').spawn;
gulp.task('default', function(){
var p;
if(p)
p.kill();
p = spawn('gulp', ['watch'], {stdio: 'inherit'});
});
gulp.task('watch', ['styles', 'watch-styles', 'watch-gulp']);
gulp.task('styles', function(){
return gulp.src('./**/*.less')
.pipe(less())
.pipe(gulp.dest('./'))
});
gulp.task('watch-styles', ['styles'], function(){
gulp.watch('./**/*.less', ['styles']);
});
gulp.task('watch-gulp', ['styles'], function(){
gulp.watch('./gulpfile.js', ['default']);
});
That is a basic gulpfile that demonstrates the problem. When I tweak that gulpfile to cause it to reload I receive this in the console:
[11:21:02] Starting 'default'...
[11:21:02] Finished 'default' after 5.25 ms
[11:21:02] Using gulpfile ~/repo/gulp-reload/gulpfile.js
[11:21:02] Starting 'styles'...
[11:21:02] Finished 'styles' after 34 ms
[11:21:02] Starting 'watch-styles'...
[11:21:03] Finished 'watch-styles' after 669 ms
[11:21:03] Starting 'watch-gulp'...
[11:21:03] Finished 'watch-gulp' after 1.83 ms
[11:21:03] Starting 'watch'...
[11:21:03] Finished 'watch' after 2.92 μs
Then modifying a less file within the directory gives this output
[11:21:24] Starting 'styles'...
[11:21:24] Finished 'styles' after 8.95 ms
[11:21:24] Starting 'styles'...
[11:21:24] Finished 'styles' after 2.92 ms
Each subsequent restart seems to lead to an additional call to styles, as if the old watchers are still there.
I created a small repo on Github with this example project as well.
In this task:
gulp.task('default', function(){
var p;
if(p)
p.kill();
p = spawn('gulp', ['watch'], {stdio: 'inherit'});
});
p is never defined, because you always create a new variable when you call that function. Also, you don't have a pointer to your own process which you want to kill. A safer way would be to end all the other watchers by hand:
gulp.task('watch-styles', ['styles'], function(){
var watcher = gulp.watch('./**/*.less', ['styles']);
watchers.push(watcher);
});
gulp.task('watch-gulp', ['styles'], function(){
var watcher = gulp.watch('./gulpfile.js', ['default']);
watchers.push(watcher);
watcher.on('change', function(){
watchers.forEach(function(el){
el.end();
})
})
});
Related
I've got this gulp file
const gulp = require('gulp')
const sass = require('gulp-sass')
const cleanCSS = require('gulp-clean-css')
const rimraf = require('rimraf')
const concat = require('gulp-concat')
gulp.task('clean', function(cb) {
rimraf('./dist/*', cb)
})
gulp.task('sass', function(cb) {
gulp.src('./src/**/*.scss')
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('./dist'))
console.log('before')
cb(undefined)
})
gulp.task('minify', function(cb) {
gulp.src('./src/**/*.css')
.pipe(cleanCSS())
.pipe(gulp.dest('./dist'))
console.log('before')
cb(undefined)
})
gulp.task('build:css', ['sass', 'minify'], function() {
gulp.src('./dist/**/*.css')
.pipe(concat('style.min.css'))
.pipe(gulp.dest('./dist'))
console.log('after')
})
gulp.task('build', ['clean'], function() {
gulp.start('build:css')
})
gulp.task('build:watch', function () {
gulp.watch('./src/**/*.scss', ['build'])
})
But when I run build:watch
[11:26:18] Using gulpfile C:\dev\web\ikea_design\gulpfile.js
[11:26:18] Starting 'build:watch'...
[11:26:18] Finished 'build:watch' after 16 ms
[11:26:24] Starting 'clean'...
[11:26:24] Finished 'clean' after 11 ms
[11:26:24] Starting 'build'...
[11:26:24] Starting 'sass'...
before
[11:26:24] Finished 'sass' after 12 ms
[11:26:24] Starting 'minify'...
before
[11:26:24] Finished 'minify' after 2.27 ms
[11:26:24] Starting 'build:css'...
after
[11:26:24] Finished 'build:css' after 1.16 ms
[11:26:24] Finished 'build' after 25 ms
even if it displays the two befores before the after, style.css.min doesn't get created. That's strange since the css files should have already been saved in the dist folder by the time concat gets invoked.
What's wrong?
Edit
I've run gulp-debug inside of build:css and it shows 0 items in the pipe
I'd like to create a watcher to watch some files and compile them.
I have two tasks :
minCss
compile
I'd like to execute compile in first and wait the end before executing minCss... But it seem's not working.
My code :
var scssToCompile = [
'./public/sass/themes/adms/adms.scss',
'./public/sass/themes/arti/arti.scss',
'./public/sass/themes/avantage/avantage.scss',
'./public/sass/themes/basique/basique.scss',
'./public/sass/themes/mairie/mairie.scss',
'./public/sass/themes/vehik/vehik.scss',
'./public/sass/themes/concept/concept.scss',
'./public/sass/themes/news/news.scss',
'./public/sass/components/*.scss',
'./public/sass/_functions.scss',
'./public/sass/_settings.scss',
'./public/sass/app.scss',
'./public/sass/bottom.scss'
];
gulp.task('compile', function(){
return gulp.src(scssToCompile)
.pipe(sassGlob())
.pipe(sass({includePaths: ['./public/sass']}).on('error', function(err) {
cb(err);
}))
.pipe(gulp.dest('./public/stylesheets'));
});
gulp.task('minCss', function() {
return gulp.src('public/stylesheets/themes/**/*.css')
.pipe(minifyCss())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('public/build/css'));
});
// my watcher
gulp.task('watch', function(){
gulp.watch(scssToCompile, ['compile', 'minCss']);
});
Results :
[15:21:36] Using gulpfile C:\xampp\htdocs\gulpfile.js
[15:21:36] Starting 'watch'...
[15:21:37] Finished 'watch' after 153 ms
[15:21:41] Starting 'compile'...
[15:21:46] Starting 'minCss'...
[15:21:46] Finished 'minCss' after 6.42 μs
[15:21:48] Finished 'compile' after 6.44 s
It used to work, if I run gulp with one or few instances already running, it will find new port (3001, 3002, 3003, 3004, [...]) and run without any issue.
I tweaked code occasionally and then I noticed that it doesn't work anymore. I don't know when it stopped working and what changed. It demands to run on only port 3000, without automatically finding new port and running on it.
Can anyone look at my gulpfile.js to see anything that is stopping from multiple instances running?
Error:
λ gulp
[19:32:57] Using gulpfile D:\Dev\Projects\NASDAQ\gulp\gulpfile.js
[19:32:57] Starting 'sass'...
[19:32:57] Finished 'sass' after 29 ms
[19:32:57] Starting 'serve'...
[19:33:07] Finished 'serve' after 10 s
[19:33:07] Starting 'default'...
[19:33:07] Finished 'default' after 5.43 μs
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at Server._listen2 (net.js:1231:14)
at listen (net.js:1267:10)
at Server.listen (net.js:1363:5)
at module.exports.plugin (D:\Dev\Projects\NASDAQ\gulp\node_modules\browser-sync\lib\server\index.js:24:25)
at Object.module.exports.startServer [as fn] (D:\Dev\Projects\NASDAQ\gulp\node_modules\browser-sync\lib\async.js:236:52)
at D:\Dev\Projects\NASDAQ\gulp\node_modules\browser-sync\lib\browser-sync.js:149:14
at iterate (D:\Dev\Projects\NASDAQ\gulp\node_modules\browser-sync\node_modules\async-each-series\index.js:8:5)
at D:\Dev\Projects\NASDAQ\gulp\node_modules\browser-sync\node_modules\async-each-series\index.js:16:16
gulpfile.js
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
var plumber = require('gulp-plumber');
var sass = require('gulp-sass');
// Static Server + watching scss/html files
gulp.task('serve', ['sass'], function() {
browserSync.init({
server: "..",
notify: false
});
gulp.watch("../assets/scss/*.scss", ['sass']);
gulp.watch("../**/*").on('change', browserSync.reload);
});
// Compile sass into CSS & auto-inject into browsers
gulp.task('sass', function() {
return gulp.src("../assets/scss/*.scss")
.pipe(plumber())
.pipe(sass.sync().on('error', sass.logError))
.pipe(sass())
.pipe(gulp.dest("../assets/css"))
.pipe(browserSync.stream());
});
gulp.task('default', ['serve']);
I hope this issue can be resolved. Thanks.
Everytime I run my gulp task, it generates a error on first run. The error is below. When I run it again, it builds without any error.
I am confused, why does it throw an error the first time but not after that.
[08:38:08] Using gulpfile ~/Documents/Code/proj2/Gulpfile.js
[08:38:08] Starting 'stylus'...
[08:38:08] Finished 'stylus' after 7.99 ms
[08:38:08] Starting 'copyClientHTML'...
[08:38:08] Finished 'copyClientHTML' after 2.27 ms
[08:38:08] Starting 'clientLint'...
[08:38:08] Starting 'demon'...
[08:38:08] Finished 'demon' after 1.88 ms
[gulp] [nodemon] v1.2.1
[gulp] [nodemon] to restart at any time, enter `rs`
[gulp] [nodemon] watching: *.*
[gulp] [nodemon] starting `node server/js/server.js`
[08:38:08] Starting 'watch'...
[08:38:09] Finished 'watch' after 31 ms
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, stat '/Users/jhans/Documents/Code/proj2/client/build/index.html'
Including Gulpfile.
/*jshint globalstrict: true*/
'use strict';
var gulp = require('gulp'),
browserify = require('gulp-browserify'),
clean = require('gulp-clean'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
stylus = require('gulp-stylus'),
del = require('del'),
nodemon = require('gulp-nodemon'),
concat = require('gulp-concat');
var paths = {
client: {
scripts: 'client/js/**/*.js',
html: 'client/views/*.html',
index: 'client/*.html',
css: 'client/css/*.styl',
conf: 'client/conf.js'
},
server: {
scripts: 'server/js/**/*.js'
}
};
// Rerun the task when a file changes
gulp.task('watch', function () {
gulp.watch(paths.client.css, ['stylus']);
gulp.watch(paths.client.scripts, ['browserify']);
gulp.watch([paths.client.html, paths.client.index], ['copyClientHTML']);
gulp.watch(paths.server.scripts, ['serverLint']);
});
gulp.task('demon', function () {
nodemon({
script: 'server/js/server.js',
ext: 'js',
env: {
'NODE_ENV': 'development'
}
})
.on('start', ['watch'])
.on('change', ['watch'])
.on('restart', function () {
console.log('restarted!');
});
});
// Default Task
gulp.task('default', ['build', 'demon']);
gulp.task('build', ['stylus', 'copyClientHTML', 'browserify']);
/********** Building CSS *********/
gulp.task('stylus', function () {
del(['client/build/css/*']);
gulp.src(paths.client.css)
.pipe(stylus())
.pipe(concat('all.css'))
.pipe(gulp.dest('client/build/css/'));
});
gulp.task('clientLint', function () {
return gulp.src([paths.client.scripts])
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
gulp.task('serverLint', function () {
return gulp.src([paths.server.scripts])
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
gulp.task('browserify',['clientLint'], function () {
del(['client/build/js/*']);
gulp.src('client/js/app.js')
.pipe(browserify({
insertGlobals: true,
debug: true
}))
.pipe(gulp.dest('client/build/js'));
});
gulp.task('copyClientHTML', function () {
del(['client/build/views/*.*']);
del(['client/build/index.html']);
gulp.src(paths.client.html)
.pipe(gulp.dest('client/build/views'));
gulp.src(paths.client.index)
.pipe(gulp.dest('client/build'));
});
Your problem might come from your copyClientHTML task (and potentially some others).
Indeed, you have to know that the del module does not run synchronously. So it might delete your files after there were created, and can explain the issue you're experiencing.
I may advice you to create a task dedicated to the cleaning of your files that you can put as dependency of some of your tasks; or better a little function where you can pass a parameter to clean specified folders.
This will ensure you that the cleaning is finished before creating the new files.
I was working on a new project at home last week and now I am in the office I am having no luck with Gulp.
There are no errors when I run Gulp but it isn't creating any files, I tried using Gulp-tap to see if anything was getting piped but its not. I am sure I am in the correct cwd and the file exists, but I have no luck. The task is also been run, confirmed by console.log'ing.
C:\Users\Jonathan\Documents\GitHub\ITMS-Backend>gulp
[11:02:16] Using gulpfile ~\Documents\GitHub\ITMS-Backend\Gulpfile.js
[11:02:16] Starting 'lint'...
[11:02:16] Starting 'sass'...
[11:02:16] Starting 'global-scripts'...
[11:02:16] Starting 'polymer'...
[11:02:16] Finished 'polymer' after 4.4 ms
[11:02:16] Finished 'lint' after 23 ms
[11:02:16] Finished 'global-scripts' after 14 ms
[11:02:16] Finished 'sass' after 19 ms
[11:02:16] Starting 'default'...
[11:02:16] Finished 'default' after 4.53 µs
My Gulp file as of current
var bower_root = 'public/components/'
webcomponents_dir = 'public/webcomponents';
var gulp = require('gulp');
var fs = require('fs');
// Include Our Plugins
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var vulcanize = require('gulp-vulcanize');
var polymer_modules = [
"core-elements/core-elements.html",
"paper-elements/paper-elements.html"
];
// Lint Task
gulp.task('lint', function() {
return gulp.src('app/assets/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// Compile Our Sass
gulp.task('sass', function() {
return gulp.src('app/assets/scss/*.scss')
.pipe(sass())
.pipe(gulp.dest('public/css'));
});
// Concatenate & Minify JS
gulp.task('global-scripts', function() {
return gulp.src('app/assets/js/*.js')
.pipe(concat('global.js'))
.pipe(gulp.dest('public/js'))
.pipe(rename('global.min.js'))
.pipe(uglify())
.pipe(gulp.dest('public/js'));
});
// Watch Files For Changes
gulp.task('watch', function() {
gulp.watch('app/assets/js/*.js', ['lint', 'global-scripts']);
gulp.watch('app/assets/scss/*.scss', ['sass']);
});
gulp.task('polymer', function() {
// Move Polymer JS File
gulp.src("polymer/polymer.min.js", {cwd: bower_root})
.pipe(gulp.dest("public/js/"));
// Move webcomponents file
gulp.src(bower_root + "webcomponentsjs/webcomponents.min.js")
.pipe(gulp.dest("public/js/"));
// CSS Polymer is shipped with
gulp.src("polymer/layout.html", {cwd: bower_root})
.pipe(gulp.dest(webcomponents_dir));
// Vulcanise
gulp.src(polymer_modules, {cwd: bower_root})
.pipe(vulcanize({
dest: './public/webcomponents',
strip: true
}))
.pipe(gulp.dest(webcomponents_dir));
});
// Default Task
gulp.task('default', ['lint', 'sass', 'global-scripts', 'polymer']);