简体   繁体   English

为什么 Gulp 服务在“服务”任务后退出而没有任何错误消息?

[英]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我最近将基于 gulp 3.x 的 gulp 文件迁移到 4.x,方法是为任务添加 gulp.series 并为任务函数添加异步函数

But when I started gulp serve, it finishing the 'clean' and 'serve' task but exiting without starting the server and no messages shown但是当我启动 gulp 服务时,它完成了“清洁”和“服务”任务,但在没有启动服务器的情况下退出并且没有显示任何消息

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 Finished 'serve'后,它只是退出到控制台

There is no errors and no warning and even no logs of the ExpressJS server. ExpressJS服务器没有错误,没有警告,甚至没有日志。 It's weird有点奇怪

This is gulp file这是 gulp 文件

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 \由于我是 nodejs 的新手,我不知道这是什么问题\

Please help me请帮我

As I understand in the documentation, the gulp.task() signature is gulp.task([taskName], function) .据我在文档中了解, gulp.task()签名是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):我相信您可以通过这样的重构来修复它(您还必须修复nodemon任务):

// 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.此外,根据文档,不建议注册任务并将它们作为字符串传递给series() 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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