Gulp task executed by watch using cached config stream - gulp

I'm trying use gulp to bundle and minify my files using gulp-bundle-assets. Running the tasks on their own is fine. My problem is using gulp.watch to watch for any changes in my config file and re-bundle my scripts.
The first time the watch executes everything works correctly. On successive occasions everything runs, but the exact same files are bundled - any changes in the config are ignored.
If I run my "bundle" task while the watch is running, "bundle" will use the current configuration. While successive watches will continue to use the configuration on the first execution.
My guess would be the data for the stream retrieved by gulp.src is cached. So how do I tell it to always get the latest version?
var gulp = require('gulp');
var bundle = require('gulp-bundle-assets');
var del = require('del');
var index = 0;
gulp.task('bundle', function () {
console.log('Bundling files ' + (index++));
return gulp.src('./bundle.config.js')
.pipe(bundle())
.pipe(gulp.dest('./bundles'));
});
gulp.task('watch', function () {
gulp.watch(['./scripts/**/*.{js,css}', './bundle.config.js'], ['clean', 'bundle']);
});
gulp.task('clean', function (cb) {
console.log('Cleaning files');
del(['./bundles/**/*'], cb);
});
An alternative I tried was to use watch(...).on, and calling gulp.run, but that didn't fix the problem, either. I also tried pasting the code from the bundle task in to the on callback, but still got the same result.

The culprit isn't gulp.src(), but bundle(). The gulp-bundle-assets plugin uses require() to load your bundle.config.js. Since Node.js caches return values from require() you always get the same config object after the file is loaded for the first time.
The solution is to invalidate the require cache in your bundle task:
var gulp = require('gulp');
var bundle = require('gulp-bundle-assets');
var del = require('del');
var index = 0;
gulp.task('bundle', ['clean'], function () { // run clean task before bundle task
// invalidate require cache for ./bundle.config.js
delete require.cache[require.resolve('./bundle.config.js')];
console.log('Bundling files ' + (index++));
return gulp.src('./bundle.config.js')
.pipe(bundle())
.pipe(gulp.dest('./bundles'));
});
gulp.task('watch', function () {
gulp.watch(['./scripts/**/*.{js,css}',
'./bundle.config.js'], ['bundle']); // only run bundle task
});
gulp.task('clean', function () {
console.log('Cleaning files');
return del(['./bundles/**/*']); // return promise object
});
Unrelated to your problem, but I also fixed your clean task. The way you had it set up didn't work.

Related

execute tasks synchronously in gulp

I have read online that 'run-sequence' will make sure all specified tasks will run synchronously. For some reason this is not true in my case. Am I missing something?
'convertSassToCss' is the task that does not work as intended
If I would run tasks 'cleanAllCss' and 'convertSassToCss' seperatelly, it would work.
The idea here is to first remove all css files from directory, then convert all sass files into css and place the into the cleaned dir
/// <binding BeforeBuild='clean, min:css' Clean='clean' />
"use strict";
var gulp = require("gulp"),
rimraf = require("rimraf"),
concat = require("gulp-concat"),
cssmin = require("gulp-cssmin"),
uglify = require("gulp-uglify"),
sass = require('gulp-sass'),
rename = require('gulp-rename'),
del = require('del'),
runSequence = require('run-sequence');
var paths = {
webroot: "./wwwroot/"
};
paths.cssPath = paths.webroot + "css/*.css";
paths.cssOutputPath = paths.webroot + "css";
//sass
paths.sassPath = paths.webroot + "sass/**/*.scss";
paths.sassOutputPath = paths.webroot + "./css/file";
gulp.task("cleanAllCss", function (cb) {
console.log("2 -- Removing all CSS files");
del([paths.cssOutputPath + "/*.css"], cb);
console.log("2 -- DONE - Removed all css files");
});
gulp.task("convertSassToCss", function (cb) {
console.log("3 -- Converting all SASS files into corresponding CSS");
gulp.src(paths.sassPath)
.pipe(sass())
.pipe(gulp.dest(paths.cssOutputPath));
console.log("3 -- DONE - Converting all SASS files into corresponding CSS");
});
//not working, should run in sequence
gulp.task("convertAllSassIntoCssMin", function (callback) {
console.log("1 -- Converting all SASS files into corresponding min CSS files")
runSequence('cleanAllCss', 'convertSassToCss', callback);
console.log("1 -- DONE - Converting all SASS files into corresponding min CSS files")
});
I cannot speak to run-sequence as I haven't used it before.
However, you can run tasks in sequence by using Gulp's task dependency feature, where a task will NOT run until it's dependencies have finished running.
Your new tasks signatures
cleanAllCss stays the same:
gulp.task("cleanAllCss", function (cb) { ... }
convertSassToCss changes to:
gulp.task("convertSassToCss", ['cleanAllCss'], function (cb) { ... }
convertAllSassIntoCssMin changes to:
gulp.task("convertAllSassIntoCssMin", ['convertSassToCss'], function (cb) { ... }
This ensures that convertAllSassIntoCssMin won't run until convertSassToCss has finished which in turn won't run until cleanAllCss has finished.
Refer to gulp deps:
deps
Type: Array
An array of tasks to be executed and completed before your task will
run.
gulp.task('mytask', ['array', 'of', 'task', 'names'], function() {
// Do stuff }); Note: Are your tasks running before the dependencies
are complete? Make sure your dependency tasks are correctly using the
async run hints: take in a callback or return a promise or event
stream.
You can also omit the function if you only want to run a bundle of
dependency tasks:
gulp.task('build', ['array', 'of', 'task', 'names']); Note: The tasks
will run in parallel (all at once), so don't assume that the tasks
will start/finish in order.
The problem is in the cleanAllCss task. The second parameter accepted by del is options, not the callback which you're trying to pass. The callback is never executed. Try running it manually when the removal is finished.
gulp.task("cleanAllCss", function (cb) {
console.log("2 -- Removing all CSS files");
del([paths.cssOutputPath + "/*.css"]).then(paths => {
console.log("2 -- DONE - Removed all css files");
cb();
};
});

Gulp use changed-in-place with multiple tasks that depend on each other

I'm using the gulp-changed-in-place package to only run certain Gulp tasks with the files that have changed (https://github.com/alexgorbatchev/gulp-changed-in-place). I'm having an issue where I only want to run my linting and code style tasks on changed files to speed up development time.
My current setup is as follows:
var gulp = require('gulp');
var changedInPlace = require('gulp-changed-in-place');
var eslint = require('gulp-eslint');
var jscs = require('gulp-jscs');
var config = {
paths: {
js: './app/**/*.js'
}
}
gulp.task('jscs', function() {
return gulp.src(config.paths.js)
.pipe(changedInPlace())
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'));
});
gulp.task('lint', ['jscs'], function() {
return gulp.src(config.paths.js)
.pipe(changedInPlace())
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('js', ['lint'], function() {
// do some stuff
});
gulp.task('watch', function() {
gulp.watch(config.paths.js, ['js']);
});
The issue is probably pretty obvious. The js task has a dependency on the lint task which itself has a dependency on the jscs task - so the jscs task runs first. It accesses changedInPlace() which causes the cache to get updated and therefore the changedInPlace() call from the lint task doesn't think anything has changed and doesn't check the files I expect.
Has anyone used this package with this issue and do you have any suggestions on what to do? Also open to other ways of accomplishing the task - only running the js task on changed files.

Gulp tasks not running synchronously

I am trying to install packages through bower, and once the installation is finished, I want the files to be moved from dist folder to its parent, and then delete the dist folder. But somehow, its not working properly.
var gulp = require('gulp'),
bower = require('gulp-bower'),
del = require('del'),
fs = require('fs'),
path = require('path'),
merge = require('merge-stream');
vinylPaths = require('vinyl-paths');
runSequence = require('run-sequence');
var appPath="./www";
var folders = getFolders(appPath);
/* Function to get folders present in www */
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
gulp.task('clean', function (){
var tasks = folders.map(function(folder) {
return gulp.src(path.join(appPath, folder))
.pipe(vinylPaths(del))
});
});
/* Gulp task to run bower install */
gulp.task('bower', ['clean'], function() {
return bower({cmd: "install"});
});
/* Gulp task to copy files from dist folder to their parent folders */
gulp.task('moveFiles', ['bower'], function (){
var tasks = folders.map(function(folder) {
console.log("FOLDER", folder);
return gulp.src(path.join(appPath, folder, '/dist/**/*'))
.pipe(gulp.dest(path.join(appPath, folder )));
});
return tasks;
});
/* Gulp task to clean the dist folder after files have been copied */
gulp.task('delete-dist-folder', ['moveFiles'], function () {
var tasks = folders.map(function(folder) {
return gulp.src(path.join(appPath, folder, '/dist'))
.pipe(vinylPaths(del))
});
return tasks;
});
gulp.task('default', ['delete-dist-folder']);
This is what I wrote till now.
After bower, the rest two tasks don't run. But if I run them individually, they work perfectly fine.
I am unable to figure out what is going wrong here. Any help would be greatly appreciated.
The problem is that you return arrays of event streams. Look at your delete-dist-folder task. You return tasks and tasks is created from calling .map on an array. So tasks is an array (of event streams).
Gulp does not know what to do with your arrays and assumes that your tasks are synchronous. There are only 3 ways to mark a task as asynchronous: 1) return an even stream; Gulp will wait for the stream to end, 2) return a promise; Gulp will wait for the promise to be resolved or rejected, or 3) declare your task callback to take a single parameter which you'll call when your task is done; Gulp will wait for the callback to be called. An array of event streams does not correspond to any of these options.
The Gulp wiki suggest using merge-stream to return more than one stream from a task. Judging by the code of merge-stream, and supposing you imported merge-stream as merge, you could do return merge.apply(undefined, tasks).
Side node: in your clean task you forgot to return a value.
I had a similar issue a while back when nesting task(requiring one task in another). So to solve this issue and run task in sequence I used run-sequence.
You seem to have installed run-sequence. Use it this way:
runSequence = require('run-sequence');
gulp.task('default', function(callback) {
runSequence('clean', 'bower', 'moveFiles', 'delete-dist-folder',
callback);
});
Hope this might help you. You can refer this for any help.
This is a temporary solution untill gulp 4.0, which will include option to configure your task run in parallel or sequentially.

Copy/Deletion in Gulp randomly gives ENOENT

New to Gulp. My default task is using the pluginrun-sequence which tells task deleteBuild to run, then makeBuild.
Randomly, I am getting an ENOENT error which seems to be telling me that I'm either referencing files that don't exist for deletion or copy. My tasks are:
deleteBuild:
gulp.task('deleteBuild', function(done) {
var del = require('del');
del(['build/**/*'], done);
});
makeBuild:
gulp.task('makeBuild', function() {
var stream = gulp.src(['src/**/*'], { base: 'src/' })
.pipe(gulp.dest('build/');
});
Can someone inform me as to how to best address this issue? I'm hoping to seek a low-level understanding rather than to be shown a solution w/o an explanation. Thanks.
Aside: I tried the deleteBuild without a callback function as well, under the assumption that, as is, it would perform the deletion and only continue to the next task once it is complete, though this doesn't seem to be what is happening.
That's probably because the deleteBuild does not return a gulp stream and thus leave the pipe broken. I would propose the following:
gulp.task('deleteBuild', function() {
var del = require('del');
var vinylPaths = require('vinyl-paths');
return gulp.src(['build']) // no need for glob, just delete the build directory
.pipe(vinylPaths(del));
});
gulp.task('makeBuild', function() {
var stream = gulp.src(['src/**/*'], { base: 'src/' })
.pipe(gulp.dest('build/');
});
gulp.task('default', function(cb) {
var runSequence = require('run-sequence');
runSequence('deleteBuild', ['makeBuild'], cb);
});
These tasks will first delete the build directory before executing the makeBuild task.
You'll need to install one additional plugin:
npm install vinyl-paths
For a ready to use example, please take a look a the gulpfile of skeletonSPA. This works for me ;-)

How to make gulp remove deleted files while watching on a particular glob pattern?

I have a gulp watch task over a glob, looking for changes and updating an output folder. It is successfully able to updated changed files, add new files but it fails to remove deleted files.
The callback looks like this:
var cb = function (event) {
console.log('Moving assets...');
return gulp.src('app/assets/**/*', {base: 'app'})
.pipe(changed('build/assets'))
.pipe(gulp.dest('build'));
};
The gulp watch task is this:
gulp.watch('app/assets/**/*', cb);
I know that the gulp watch is running fine because the callback receives all the deleted events. But the callback itself is unable to remove the deleted file from the build/assets/ folder.
Do I have to explicitly handle the deleted event or am I missing something out?
The question is old, but I think it's still relevant.
Here is the solution I came up with, based on ondaplana answer in this github issue and the documentation of the gulp modules that are used:
Basically, it uses gulp-watch instead of gulp native watch function, to pipe gulp-filter and del modules.
var filter = require('gulp-filter');
var vinylPaths = require('vinyl-paths');
var del = require('del');
var watch = require('gulp-watch');
var notDeletedFilter = filter(
function(file) {
return file.event !== 'unlink' && file.event !== 'unlinkDir';
},
{restore: true}
);
notDeletedFilter.restore
.pipe(gulp.dest('dist'))
.pipe(vinylPaths(del));
//use gulp-watch instead of gulp native watch function
watch(['app/assets/**/*'], {events: ['add', 'change', 'unlink', 'unlinkDir']})
.pipe(notDeletedFilter)
.pipe(anyModule())
.pipe(gulp.dest('build'));
Documentation :
Gulp recipes