Saturday 6 May 2023

Gulp JS task outputting NodeError: Callback called multiple times

Gulp JS task outputting NodeError: Callback called multiple times

I have five Gulp JS tasks, one of these tasks called js_bundle is giving me an error. The error is as follows:

NodeError: Callback function called multiple times

Here is my gulpfile.js:

const { src, dest, parallel , series , watch } = require('gulp');

const sass = require('gulp-sass')(require('sass'));
const fileinclude = require('gulp-file-include');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const minify = require('gulp-minifier');
const strip = require('gulp-strip-comments');
const rtlcss = require('gulp-rtlcss');
const rename = require('gulp-rename');

// sass.compiler = require('node-sass'); // no-need for gulp-sass v5+

var node_path = '../../../';


function html(cb) {
  src('html/src/**')
  .pipe(dest('html/dist/'));

  cb();
}

function scss(cb) {
  src(['scss/*.scss'])
  // .pipe(sourcemaps.init())               // If you want generate source map.
  .pipe(sass().on('error', sass.logError))  // uses {outputStyle: 'compressed'} in saas() for minify css
  // .pipe(sourcemaps.write('./'))          // If you want generate source map.
  .pipe(dest('assets/dist/css'));

  src(['scss/*.scss', '!scss/style-email.scss'])
  // .pipe(sourcemaps.init())               // If you want generate source map.
  .pipe(sass().on('error', sass.logError))  // uses {outputStyle: 'compressed'} in saas() for minify css
  // .pipe(sourcemaps.write('./'))          // If you want generate source map.
  .pipe(rtlcss())
  .pipe(rename({ suffix: '.rtl' }))
  .pipe(dest('assets/dist/css'));

  // EDITORS
  src(['scss/editors/*.scss'])
  // .pipe(sourcemaps.init())                                           // If you want generate source map.
  .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))   // remove {outputStyle: 'compressed'} from saas() if do not want to minify css
  // .pipe(sourcemaps.write('./'))                                      // If you want generate source map.
  .pipe(dest('assets/dist/css/editors'));

  src(['scss/editors/*.scss'])
  // .pipe(sourcemaps.init())                                           // If you want generate source map.
  .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))   // remove {outputStyle: 'compressed'} from saas() if do not want to minify css
  // .pipe(sourcemaps.write('./'))                                      // If you want generate source map.
  .pipe(rtlcss())
  .pipe(rename({ suffix: '.rtl' }))
  .pipe(dest('assets/dist/css/editors'));

  src(['scss/libs/*.scss'])
  // .pipe(sourcemaps.init())                                           // If you want generate source map.
  .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))   // remove {outputStyle: 'compressed'} from saas() if do not want to minify css
  // .pipe(sourcemaps.write('./'))                                      // If you want generate source map.
  .pipe(dest('assets/dist/css/libs'));

  // SKINS
  src(['scss/skins/*.scss'])
  // .pipe(sourcemaps.init())               // If you want generate source map.
  .pipe(sass().on('error', sass.logError))  // uses {outputStyle: 'compressed'} in saas() for minify css
  // .pipe(sourcemaps.write('./'))          // If you want generate source map.
  .pipe(dest('assets/dist/css/skins'));

  cb();
}

function js_scripts(cb) {
  src(['assets/src/js/*.js','!assets/src/js/bundle.js'])
  // .pipe(uglify())                        // If you minify the code.
  .pipe(dest('assets/dist/js'));

  src(['assets/src/js/charts/*.js'])
  .pipe(uglify())                        // If you minify the code.
  .pipe(dest('assets/dist/js/charts'));

  src(['assets/src/js/apps/*.js'])
  // .pipe(uglify())                        // If you minify the code.
  .pipe(dest('assets/dist/js/apps'));

  cb();
}

/* The task giving me the error */
function js_bundle(cb) {
  src('assets/src/js/bundle.js')
  .pipe(fileinclude({
    prefix: '@@',
    basepath: '@file',
    context: { build: 'dist', nodeRoot: node_path }
  }))
  .pipe(strip())
  .pipe(minify({ minify: true, minifyJS: { sourceMap: false } }))     // Disable, if you dont want to minify bundle file.
  .pipe(dest('assets/dist/js'));

  src(['assets/src/js/libs/**', '!assets/src/js/libs/editors/skins/**'])
  .pipe(fileinclude({
    prefix: '@@',
    basepath: '@file',
    context: { build: 'dist', nodeRoot: node_path }
  }))
  .pipe(strip())
  .pipe(minify({ minify: true, minifyJS: { sourceMap: false } }))     // Disable, if you dont want to minify bundle file.
  .pipe(dest('assets/dist/js/libs'));

  src('assets/src/js/libs/editors/skins/**').pipe(dest('assets/dist/js/libs/editors/skins'));

  cb();
}

function assets(cb){
  src(['assets/src/**', '!assets/src/js/**', '!assets/src/css/**'])
  .pipe(dest('assets/dist/'));

  cb();
}

exports.build = series(html, scss, js_scripts, /* The task giving me the error */ js_bundle, assets);

exports.develop = function() {
    watch(['scss/*.scss','scss/**'], scss)
    watch(['html/src/*.html','html/src/**/*.html'], html)
    watch(['assets/src/**'], assets)
    watch(['assets/src/js/*.js','assets/src/js/charts/*.js', 'assets/src/js/apps/*.js', '!assets/src/js/bundle.js'], js_scripts)
    watch(['assets/src/js/libs/**','assets/src/js/bundle.js'], js_bundle)
};

Here is the code snippet causing the error:

.pipe(fileinclude({
    prefix: '@@',
    basepath: '@file',
    context: { build: 'dist', nodeRoot: node_path }
}))

I tried the following:

  • Updated the gulp_file_include plugin to the latest version which I suspect is causing the error.
  • Console logged the functions before and after to check if the callback function is repeating but to no avail.
  • Removed the strip and minify functions but still got the error which confirms my doubt that the gulp_file_include plugin is causing the error.
  • Checked this SO question How to fix callback function called multiple times but I did not have the gulp-imagemin plugin which the author of the question had said was causing the problem

BRIEF DUMMY PROJECT STRUCTURE

Brief Overview


Any help is appreciated thanks, Ciao



from Gulp JS task outputting NodeError: Callback called multiple times

No comments:

Post a Comment