简体   繁体   中英

Why Gulp Serve exiting without any error message after 'serve' task?

I recently migrated a gulp file based on gulp 3.x to 4.x by adding gulp.series for tasks and async functions for the task functions

But when I started gulp serve, it finishing the 'clean' and 'serve' task but exiting without starting the server and no messages shown

Log:

> videostream@2.0.0 start /root/VideoStream
> gulp serve

[01:47:05] Requiring external module babel-register
[01:47:05] Using gulpfile ~/VideoStream/gulpfile.babel.js
[01:47:05] Starting 'serve'...
[01:47:05] Starting 'clean'...
[01:47:05] Finished 'clean' after 5.31 ms
[01:47:05] Finished 'serve' after 6.8 ms
root@iZa2d90eklkjfyzkilci2vZ:~/VideoStream#

Look at the logs. After Finished 'serve' it simply exits to the console

There is no errors and no warning and even no logs of the ExpressJS server. It's weird

This is gulp file

import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import path from 'path';
import del from 'del';
import runSequence from 'run-sequence';

const plugins = gulpLoadPlugins();

const paths = {
  js: ['./**/*.js', '!dist/**', '!node_modules/**', '!coverage/**'],
  nonJs: ['./package.json', './.gitignore', './.env', './services/fet_firebase.json'],
  tests: './server/tests/*.js'
};

// Clean up dist and coverage directory
gulp.task('clean', (done) => {
  del.sync(['dist/**', 'dist/.*', 'coverage/**', '!dist', '!coverage']);
   done();
}
);

// Copy non-js files to dist
gulp.task('copy', (done) => {
  gulp.src(paths.nonJs)
    .pipe(plugins.newer('dist'))
    .pipe(gulp.dest('dist'));
  done();
}
);

// Compile ES6 to ES5 and copy to dist
gulp.task('babel', (done) => {
  gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' })
    .pipe(plugins.newer('dist'))
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.babel())
    .pipe(plugins.sourcemaps.write('.', {
      includeContent: false,
      sourceRoot(file) {
        return path.relative(file.path, __dirname);
      }
    }))
    .pipe(gulp.dest('dist'));
    done();
}
);


// Start server with restart on file changes
gulp.task('nodemon', gulp.series(['copy', 'babel']), () => {
  plugins.nodemon({
    script: path.join('dist', 'index.js'),
    ext: 'js',
    ignore: ['node_modules/**/*.js', 'dist/**/*.js'],
    tasks: ['copy', 'babel']
  });
done();
}
);

// gulp serve for development
gulp.task('serve', gulp.series(['clean']), (done) => {runSequence('nodemon'); done();});

// default task: clean dist, compile js files and copy non-js files.
gulp.task('default', gulp.series(['clean']), (done) => {
  runSequence(
    ['copy', 'babel']
  );
done();
});

Since I am new to nodejs, I cant figure it what's the issue \

Please help me

As I understand in the documentation, the gulp.task() signature is gulp.task([taskName], function) . Thus, it does not expect the third argument. I believe you can fix it by refactoring like this (you'll also have to fix the nodemon task):

// gulp serve for development
gulp.task('serve', (done) => {
  gulp.series(['clean', 'nodemon']);
  done();
});

Additionally, it is not recommended, per the documentation , to register the tasks and pass them to series() as strings. Instead, do this:

const gulp = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const del = require('delete');
const path = require('path');
const runSequence = require('run-sequence');

const plugins = gulpLoadPlugins();

const paths = {
  js: ['./**/*.js', '!dist/**', '!node_modules/**', '!coverage/**'],
  nonJs: ['./package.json', './.gitignore', './.env', './services/fet_firebase.json'],
  tests: './server/tests/*.js'
};

// Clean up dist and coverage directory
function clean(done) {
  del.sync(['dist/**', 'dist/.*', 'coverage/**', '!dist', '!coverage']);
  done();
}

// Copy non-js files to dist
function copy(done) {
  gulp.src(paths.nonJs)
    .pipe(plugins.newer('dist'))
    .pipe(gulp.dest('dist'));
  done();
}

// Compile ES6 to ES5 and copy to dist
function babel(done) {
  gulp.src([...paths.js, '!gulpfile.babel.js'], { base: '.' })
    .pipe(plugins.newer('dist'))
    .pipe(plugins.sourcemaps.init())
    .pipe(plugins.babel())
    .pipe(plugins.sourcemaps.write('.', {
      includeContent: false,
      sourceRoot(file) {
        return path.relative(file.path, __dirname);
      }
    }))
    .pipe(gulp.dest('dist'));
  done();
}

// Start server with restart on file changes
function nodemon(done) {
  gulp.series(copy, babel)
  plugins.nodemon({
    script: path.join('dist', 'index.js'),
    ext: 'js',
    ignore: ['node_modules/**/*.js', 'dist/**/*.js'],
    tasks: ['copy', 'babel']
  });
  done();
}

// gulp serve for development
function serve(done) {
  gulp.series(clean, nodemon);
  done();
}

exports.clean = clean;
exports.copy = copy;
exports.babel = babel;
exports.serve = serve;
// default task: clean dist, compile js files and copy non-js files.
exports.default = gulp.series(clean, copy, babel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM