如何用gulp 实现集成重启

来源:9-5 总结

BCS

2016-08-16

目前在看一门Nodejs教程,发现老师用的是grunt,我想用gulp试试,但是试了很多方法都不行,能不能麻烦老师给出一套案例,我好参考下,这是用grunt实现的源码。

module.exports = function(grunt) {
 
  grunt.initConfig({
    watch: {
      jade: {
        files: ['views/**'],
        options: {
          livereload: true
        }
      },
      js: {
        files: ['public/js/**', 'models/**/*.js', 'schemas/**/*.js'],
        //tasks: ['jshint'],
        options: {
          livereload: true
        }
      },
      uglify: {
        files: ['public/**/*.js'],
        tasks: ['jshint'],
        options: {
          livereload: true
        }
      },
      styles: {
        files: ['public/**/*.less'],
        tasks: ['less'],
        options: {
          nospawn: true
        }
      }
    },
 
    jshint: {
      options: {
        jshintrc: '.jshintrc',
        ignores: ['public/libs/**/*.js']
      },
      all: ['public/js/*.js', 'test/**/*.js', 'app/**/*.js']
    },
 
    less: {
      development: {
        options: {
          compress: true,
          yuicompress: true,
          optimization: 2
        },
        files: {
          'public/build/index.css': 'public/less/index.less'
        }
      }
    },
 
    uglify: {
      development: {
        files: {
          'public/build/admin.min.js': 'public/js/admin.js',
          'public/build/detail.min.js': [
            'public/js/detail.js'
          ]
        }
      }
    },
 
    nodemon: {
      dev: {
        options: {
          file: 'app.js',
          args: [],
          ignoredFiles: ['README.md', 'node_modules/**', '.DS_Store'],
          watchedExtensions: ['js'],
          watchedFolders: ['./'],
          debug: true,
          delayTime: 1,
          env: {
            PORT: 3000
          },
          cwd: __dirname
        }
      }
    },
 
    mochaTest: {
      options: {
        reporter: 'spec'
      },
      src: ['test/**/*.js']
    },
 
    concurrent: {
      tasks: ['nodemon', 'watch', 'less', 'uglify', 'jshint'],
      options: {
        logConcurrentOutput: true
      }
    }
  })
 
  grunt.loadNpmTasks('grunt-contrib-watch')
  grunt.loadNpmTasks('grunt-nodemon')
  grunt.loadNpmTasks('grunt-concurrent')
  grunt.loadNpmTasks('grunt-mocha-test')
  grunt.loadNpmTasks('grunt-contrib-less')
  grunt.loadNpmTasks('grunt-contrib-uglify')
  grunt.loadNpmTasks('grunt-contrib-jshint')
 
  grunt.option('force', true)
 
  grunt.registerTask('default', ['concurrent'])
 
  grunt.registerTask('test', ['mochaTest'])
}


写回答

2回答

姜维

2016-08-17

可能你主要想知道怎么实时监听文件改变,在文件改变后做一些操作,可以参考:https://www.npmjs.com/package/gulp-watch


0
0

BCS

提问者

2016-08-16

无从下手啊。。。

gulp.task('jekyll', function(cb) {
  exec('node app', function(err) {
    if (err) return cb(err); 
    cb(); 
  });
});

这个可以在命令行执行 'node app' 来启动服务
但是怎样 在文件改变后实时监听 ,自己重启服务。。求


0
0

响应式开发一招致胜

用一套代码开发出不受设备型号、尺寸限制的互联网金融网站

4021 学习 · 752 问题

查看课程