我一直有与Grunt.js和几个插件,尤其是一个问题:grunt-contrib-watch
,grunt-nodemon
和grunt-contrib-coffee
。我已经尝试解决了两天,但是我现在对Grunt的了解还不足以解决这个问题。
我遇到的问题很简单,就是我要.coffee
编译服务器端文件,然后让nodemon重新启动服务器,然后再进行livereload工作。现在,Livereload可以按预期工作,但服务器端coffee
文件除外。contrib-watch
检测到更改,运行coffee
并触发livereload
事件,然后nodemon
重新启动。
有没有一种方法可以nodemon
在页面重新加载之前重新启动,以使我在屏幕上看到的内容与服务器端代码中的最新信息一致?
已经为我提供了仅nodemon
在单独的终端选项卡上运行的选项,但是我在Windows上,因此更愿意让一个终端运行以达到此目的,这就是我正在使用的全部原因grunt-concurrent
。
这是我的Gruntfile,现在还处于初期阶段(因为我试图弄清所有这些)。如果您希望我将其编译为JavaScript,然后发表评论并提出要求,我将很高兴。
module.exports = (grunt) ->
# configuration
grunt.initConfig
pkg: grunt.file.readJSON 'package.json'
# watch task
watch:
css:
files: ['src/assets/styles/**/*.styl']
tasks: ['stylus']
options:
livereload: true
coffee:
files: ['src/**/*.coffee']
tasks: ['coffee']
js:
files: ['**/*.js']
options:
livereload: true
jade:
files: ['views/**/*.jade']
options:
livereload: true
# compile coffeescript to javascript
coffee:
compile:
options:
sourceMap: true
files: [
expand: true
cwd: 'src/'
src: ['**/*.coffee']
dest: ''
ext: '.js'
]
# compile stylus to css
stylus:
compile:
files: [
expand: true
cwd: 'src/assets/styles/'
src: ['**/*.styl']
dest: 'assets/styles/'
ext: '.css'
]
# run server
nodemon:
dev:
options:
file: 'server.js'
watchedExtensions: ['js', 'json']
ignoredFiles: [
'assets/**',
'node_modules/**',
'**/.js.map'
]
# run tasks concurrently for fast builds
concurrent:
first:
tasks: ['coffee', 'stylus']
options:
logConcurrentOutput: true
second:
tasks: ['nodemon', 'watch']
options:
logConcurrentOutput: true
# load dependencies
require('load-grunt-tasks') grunt
# register tasks
grunt.registerTask 'default', [
'concurrent:first',
'concurrent:second'
]
我自己没有使用过,但是前一段时间遇到了:grunt-rerun
。与监视任务结合使用,您可以暂停长时间运行的任务,例如express(但也可以与nodemon一起使用),运行一些任务,然后再次启动该任务。示例配置如下所示:
grunt.initConfig({
watch: {
dev: {
files: ['server/*.js'],
//Note the :go flag used for sending the reload message to the rerun server
tasks: ['clean','rerun:dev:express:go']
},
},
express: {
dev: {
options: {
port: 3000,
bases: ['/public'],
keepalive: true,
server: path.resolve('./server/app.js')
}
}
},
// Configuration to be run (and then tested).
rerun: {
dev: {
options: {
tasks: ['express']
},
},
}
})
https://npmjs.org/package/grunt-rerun
我不确定实时重新加载。我的猜测是因为它关闭了进程,通过生成一个新进程将重新加载该页面,但是我没有亲自使用过,因此我不确定。
第二种选择是,使用支持多个选项卡的命令提示符,例如Console。我是Mac用户,因此我使用具有多个窗格的iTerm 2。大部分的时间,我每个项目四开,对watch
,testem
支持PHP的服务器和其他一切壳。您可能会发现这更快,更省事。
只是关于Coffeescript的简短说明,许多JavaScript开发人员都没有使用它,因此为了让更多的读者了解您的源代码,在您发布问题之前将coffee编译为js可能是一个好习惯。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句