简体   繁体   English

Gulp-nodemon和观看任务

[英]Gulp-nodemon and watch task

I'm trying to create my build flow using gulp and nodemon. 我正在尝试使用gulp和nodemon创建构建流程。 The objective is to watch sass files and compile them to css, and also restart node application when server file changes. 目的是监视sass文件并将其编译为css,并在服务器文件更改时重新启动节点应用程序。

My gulpfile.js: 我的gulpfile.js:

gulp.task('sass', function(){
  return gulp.src(sassFilesTobeProcessed).
  pipe(sass()).
  pipe(concat('ready_stylesheet.css')).
  pipe(gulp.dest('express/public/stylesheets'))
})

gulp.task('watch', function(){
  return gulp.watch(allSassFiles, ['sass']);
})

gulp.task('serve', function(){
  return nodemon({
    script: 'express/app.js',
  }).on('start', ['watch'])
  .on('change', ['watch'])
  .on('restart', function(){
      console.log('restarted');
    })
})

The watch task is working fine, files are compiled after change. watch任务运行正常,更改后将编译文件。 But changes in my app.js server file doesn't trigger server restart. 但是我的app.js服务器文件中的更改不会触发服务器重启。 When I comment the .on statements it starts to work fine (server reloads), but then of course sass files are no longer observed. 当我评论.on语句时,它开始可以正常工作(服务器重新加载),但随后当然不再观察到sass文件。 I assume hence, there is some conflict between these two, which I cannot discover. 因此,我认为这两者之间存在一些冲突,我无法发现。 Appreciate any help! 感谢任何帮助! My OS - Windows 7, node 4.2.6, nodemon 1.9.1 我的操作系统-Windows 7,node 4.2.6,nodemon 1.9.1

Use a task dependency instead of .on(event) to start your watch task: 使用任务依赖项而不是.on(event)来启动watch任务:

gulp.task('serve', ['watch'], function(){
  return nodemon({
    script: 'express/app.js',
  })
  .on('restart', function(){
    console.log('restarted');
  })
})

emit the restart event with nodemon 用nodemon发出重启事件

const cfg = require('../config')
const gulp = require('gulp')
const nodemon = require('nodemon')
const gnodemon = require('gulp-nodemon')

gulp.task('nodemon', ['ts', 'json'], () => {
  gnodemon({
    script: cfg.paths.main,
    tasks: ['ts', 'json'],
    ext: 'js',
    watch: [cfg.paths.src],
    // para no alterar el entorno de prodicion con test
    env: {'NODE_ENV': process.env.NODE_ENV !== 'production'
      ? process.env.NODE_ENV || 'development' : 'development'}
  })
  .on('start', ['mocha'])
})

gulp.task('default', ['nodemon'], () => {
  gulp.watch(cfg.paths.src, (event) => nodemon.emit('restart'))
})

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

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