Why is Gulp running the same task twice? - gulp

I am having a similar issue as the question posted here but none of the answers given there apply to my situation.
When I run the one and only task defined in my Gulpfile.js file it is getting executed twice.
I am using Gulp version 4.0.2
This is the contents of my Gulpfile.js file:
const { src, dest, watch, series, parallel } = require('gulp');
const sass = require('gulp-sass');
const rename = require('gulp-rename');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const files = {
bootstrapSrcPath: 'bootstrap-sass/bootstrap.scss',
bootstrapDstPath: 'Test'
};
exports.scssTask = series(
scssTaskFunc
);
function scssTaskFunc() {
return src(files.bootstrapSrcPath)
.pipe(sass({ style: 'expanded' }))
.pipe(dest(files.bootstrapDstPath))
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(rename({ suffix: '.min' }))
.pipe(dest(files.bootstrapDstPath));
}
This is the command I am running in the CMD prompt and the results:
C:\Users\myUser\source\repos\myProject> cmd.exe / c gulp - b "C:\Users\myUser\source\repos\myProject" --color--gulpfile "C:\Users\myUser\source\repos\myProject\Gulpfile.js" scssTask
[16: 10: 15]Using gulpfile ~\source\repos\myProject\Gulpfile.js
[16: 10: 15]Starting 'scssTask'...
[16: 10: 15] Starting 'scssTaskFunc'...
[16: 10: 17] Finished 'scssTaskFunc' after 1.7 s
[16: 10: 17]Finished 'scssTask' after 1.71 s
Process terminated with code 0.
It works and the output file is what I expect but it seems like a waste to do it twice.
This is the what gulp shows for tasks:
C:\Users\myUser\source\repos\myProject> gulp--tasks
[15: 57: 29]Tasks for ~\source\repos\myProject\Gulpfile.js
[15: 57: 29]└─┬ scssTask
[15: 57: 29]└─┬ <series>
[15:57:29] └── scssTaskFunc
Why is it running the task twice, once as the 'scssTask' and the second as the 'scssTaskFunc'?
Btw, this is my first attempt at gulp so I apologize if this is a derp question.

Your task isn't running twice, it just seems like it does because you're using gulp.series.
gulp.series and gulp.parallel are normally used to combine and compose tasks into larger operations. If, for example, you'd have a jsTaskFunc as well, you could create a task build like so:
exports.build = parallel(scssTaskFunc, jsTaskFunc);
and running gulp build would log something like this in your terminal:
[09:42:12] Starting 'build'...
[09:42:12] Starting 'scssTaskFunc'...
[09:42:12] Starting 'jsTaskFunc'...
[09:42:12] Finished 'scssTaskFunc' after 93 ms
[09:42:12] Finished 'jsTaskFunc' after 94 ms
[09:42:12] Finished 'build' after 111 ms
Something similar is happening now due to your use of gulp.series, because scssTask runs scssTaskFunc as a dependent task, but scssTask and scssTaskFunc are strictly speaking not the same task. Nothing gets run twice.
To avoid confusion, and because gulp.series isn't necessary, simply do:
exports.scssTask = scssTaskFunc;

Related

Exporting a task file to gulpfile.js and run on command line

(Pertains to Gulp 4.0/ES6)
I would like to create a task file that can be imported by main gulpfile.js and can also be run directly from the command line using gulp (in this case without even referencing the gulpfile.js file on command line). Is this possible?
I can create a gulp task file that can be run directly:
task-a.js:
const log = require('fancy-log');
function taskA(done){
// do stuff
log('Executing task A');
done();
}
exports.default = taskA;
Run from command line:
gulp -f task-a.js
[17:53:21] Using gulpfile C:\app\task-a.js
[17:53:21] Starting 'default'...
[17:53:21] Executing task A
[17:53:21] Finished 'default' after 2.73 ms
OK, looks good.
In order to import into gulpfile.js I need to modify the export in task-a.js to:
task-a.js:
// modified the exports for require()
module.exports taskA;
gulpfile.js:
const taskA = require('./task-a');
gulp.task('default', taskA);
gulp -f gulpfile.js
OK, this also works.
But is it possible to change the exports in task-a.js to allow for both import and to be run from command line using gulp?
One way to export the function in task-a.js as a task:
gulp.task('default', taskA);
module.exports = gulp.series('default');
This ensures that the gulpfile.js can import the function as a composed operation.

TypeError: dest.on is not a function in gulp

When I edit theme .yml files in my themes source folder, I get this error. This does not happen when I edit scss or template .yml files from the template folder, only in the main folder. Others that had this error use things like webpack what I don't have. The gulp task for copy_theme_files is the same as other tasks with the difference that it has no return because I didn't know how to return there with two gulp.src functions.
gulp.task('copy_theme_files', function() {
console.log('[copy_theme_files] Console Log: copy '+paths.themeSrc+' to '+paths.themeDest);
gulp.src(paths.themeSrc)
.pipe(gulp.dest(paths.themeDest));
gulp.src(paths.root + '/*.{png,ico,svg}')
.pipe(paths.themeDest);
});
Full gulpfile.js https://pastebin.com/NWt2uMwV
Error Output:
[00:37:58] Using gulpfile /var/www/themes.src/mytheme.src/gulpfile.js
[00:37:58] Starting 'watch'...
[00:38:20] Starting 'copy_theme_files'...
[copy_theme_files] Console Log: copy *.{yml,theme,php,png,jpg,gif,svg,ico},.gitkeep to build
[00:38:20] 'copy_theme_files' errored after 23 ms
[00:38:20] TypeError: dest.on is not a function
at DestroyableTransform.Readable.pipe (/var/www/themes.src/mytheme.src/node_modules/readable-stream/lib/_stream_readable.js:564:8)
at /var/www/themes.src/mytheme.src/gulpfile.js:122:10
at taskWrapper (/var/www/themes.src/mytheme.src/node_modules/undertaker/lib/set-task.js:13:15)
at bound (domain.js:395:14)
at runBound (domain.js:408:12)
at asyncRunner (/var/www/themes.src/mytheme.src/node_modules/async-done/index.js:55:18)
at process._tickCallback (internal/process/next_tick.js:61:11)
[00:38:20] Starting 'copy_includes'...
[copy_includes] Console Log: copy includes/**/*.*,includes/**/.gitkeep to build/includes
[00:38:20] Finished 'copy_includes' after 7.41 ms
[00:38:20] Starting 'copy_build'...
[copy_build] Console Log: copy build/**/*.*,build/**/.gitkeep to ../../web/themes/local/mytheme
[00:38:20] Finished 'copy_build' after 60 ms
Other tasks run fine
[00:41:06] Starting 'copy_templates'...
[copy_templates] Console Log: copy templates/**/*.twig,templates/**/.gitkeep to build/templates
[00:41:08] Finished 'copy_templates' after 1.86 s
[00:41:08] Starting 'copy_build'...
[copy_build] Console Log: copy build/**/*.*,build/**/.gitkeep to ../../web/themes/local/mytheme
[00:41:09] Finished 'copy_build' after 326 ms
You have this line in your task:
.pipe(paths.themeDest);
You probably mean:
.pipe(gulp.dest(paths.themeDest));
For your other question look at merging two gulp.src streams for how to return:
var merge = require('merge-stream');
gulp.task('copy_theme_files', function() {
console.log('[copy_theme_files] Console Log: copy '+paths.themeSrc+' to '+paths.themeDest);
const themesStream = gulp.src(paths.themeSrc)
.pipe(gulp.dest(paths.themeDest));
const imagesStream = gulp.src(paths.root + '/*.{png,ico,svg}')
.pipe(paths.themeDest);
return merge(themesStream , imagesStream );
});

gulp-ruby-sass not working

I did everything exactly as stated in docs:
https://github.com/sindresorhus/gulp-ruby-sass/blob/master/readme.md
This is my code in gulpfile.js:
// Sass configuration
const gulp = require('gulp');
const sass = require('gulp-ruby-sass');
const sourcemaps = require('gulp-sourcemaps');
gulp.task('sass', () =>
sass('kraater-web/src/app/css/admin/vars.scss', {sourcemap: true})
.on('error', sass.logError)
// for inline sourcemaps
.pipe(sourcemaps.write())
// for file sourcemaps
.pipe(sourcemaps.write('maps', {
includeContent: false,
sourceRoot: 'source'
}))
.pipe(gulp.dest('kraater-web/src/app/css/'))
);
gulp.task('default', ['sass'], function() {
})
When I run it, I just get this output, and no files are generated:
[15:47:25] Using gulpfile C:\wamp\www\site17\gulpfile.js
[15:47:25] Starting 'sass'...
[15:47:25] Could not find an option named "sourcemap".
[15:47:25] Usage: sass <input.scss> [output.css]
sass <input.scss>:<output.css> <input/>:<output/>
=== Input and Output ===================
--[no-]stdin Read the stylesheet from stdin.
--[no-]indented Use the indented syntax for input from stdin.
-I, --load-path=<PATH> A path to use when resolving imports.
May be passed multiple times.
-s, --style=<NAME> Output style.
[expanded (default), compressed]
--update Only compile out-of-date stylesheets.
=== Source Maps ========================
--[no-]source-map Whether to generate source maps.
(defaults to on)
--source-map-urls How to link from source maps to source files.
[relative (default), absolute]
--[no-]embed-sources Embed source file contents in source maps.
--[no-]embed-source-map Embed source map contents in CSS.
=== Other ==============================
-i, --interactive Run an interactive SassScript shell.
-c, --[no-]color Whether to emit terminal colors.
-q, --[no-]quiet Don't print warnings.
--[no-]trace Print full Dart stack traces for exceptions.
-h, --help Print this usage information.
--version Print the version of Dart Sass.
[15:47:25] Finished 'sass' after 416 ms
[15:47:25] Starting 'default'...
[15:47:25] Finished 'default' after 11 μs
Watching build tasks has finished.
Ideas how to get it to work?

Gulp file running tasks and notifying twice

I've just started using gulp for the first time, required all the plugins I want to use and written the first task for sass compilation. It seems to work but there are two problems, firstly when I type gulp on the command line it seems to take 3 or 4 seconds to start which seems slower than grunt (I started using gulp because I understood it was faster). Is this normal?
The main problem though is that I have a default task which calls the sass task. The command line output seems to suggest that both are being run which means the sass is being compiled twice. It is also outputting my single gulp-notify notification twice which doesn't seem right.
Here is the command line output...
λ gulp default
[00:53:40] Using gulpfile ~\Desktop\jon\gulpfile.js
[00:53:40] Starting 'sass'...
[00:53:40] Finished 'sass' after 10 ms
[00:53:40] Starting 'default'...
[00:53:40] Finished 'default' after 7.93 μs
[00:53:41] gulp-notify: [Gulp notification] Css created
[00:53:41] gulp-notify: [Gulp notification] Css created
And here is my gulp file in full...
var gulp = require('gulp'),
gutil = require('gulp-util'),
compass = require('gulp-compass'),
rename = require('gulp-rename'),
uglify = require('gulp-uglify'),
watch = require('gulp-watch'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
jshint = require('gulp-jshint'),
autoprefixer = require('gulp-autoprefixer'),
minifyCSS = require('gulp-minify-css'),
traceur = require('gulp-traceur'),
svgmin = require('gulp-svgmin'),
imagemin = require('gulp-imagemin'),
ngAnnotate = require('gulp-ng-annotate'),
expect = require('gulp-expect-file'),
sourcemaps = require('gulp-sourcemaps');
var paths = {
src: "src",
css: "stylesheets",
img: "images",
js: "js"
}
// Compile Our Sass
gulp.task('sass', function() {
gulp.src(paths.src + '/sass/*.scss')
.pipe(sourcemaps.init())
.pipe(compass({
sass: 'src/sass',
environment: 'development',
outputStyle: 'expanded',
debugInfo: false,
noLineComments: true
}))
.pipe(autoprefixer('> 5%', 'last 2 version', 'ie 9'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(paths.css))
.pipe(notify({ message: 'Css created' }));
});
// Dev Task
gulp.task('default', ['sass']);
Anybody know what's going on here? Have I misunderstood how Gulp tasks work?
Use the onLast option of gulp-notify if you only want a single notification per-stream:
//...
.pipe(notify({message: 'Css created', onLast: true}));

gulp-watch works only on initial run

What I do?
Run gulp (SCSS files are being processed, I get a CSS file)
I change any SCSS file again
Expected:
CSS file from 1. is updated with the changes from 2.
What happens?
CSS file from 1. isn't changed
Command line output:
$ gulp
[09:24:28] Using gulpfile c:\Users\User\_dev\github\project\gulpfile.js
[09:24:28] Starting 'sass'...
[09:24:28] Finished 'sass' after 98 ms
[09:24:28] Starting 'default'...
[09:24:28] Finished 'default' after 7.31 μs
[09:24:35] sass-watch saw _base.scss was changed
[09:25:39] sass-watch saw _base.scss was changed
gulpfile.js:
gulp.task('sass', function() {
watch({ glob: 'css/**/*.{scss,sass}', name: 'sass-watch'})
.pipe(plumber())
.pipe(sass())
.pipe(gulp.dest('css'))
});
gulp.task('default', ['sass']);
Notes:
Issue on GitHub (gulp)
Issue on GitHub (gulp-watch)
gulpfile.js on GitHub Gist)
OS: Win7
node: 0.10.29
npm: 1.4.14
The way the source files are piped in is not important. The result stays the same when using gulp.src()
I dont think your sass task is correctly written.
Try something like this:
var gulp = require('gulp');
var sass = require('gulp-sass')
gulp.task('sass', function () {
gulp.src('PATH-TO-SASS-FILES/*.scss')
.pipe(sass())
.pipe(gulp.dest('./css'));
});