与'nodemon'和'watch'任务一起运行grunt-concurrent时的无限循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了与'nodemon'和'watch'任务一起运行grunt-concurrent时的无限循环相关的知识,希望对你有一定的参考价值。

我试图使用grunt-concurrent任务来运行grunt-nodemon来观看我的js脚本并同时使用watchto仍然concatuglify我的文件也改变了。

当我在命令行上运行grunt时,我得到以下无限循环:

Running "watch" task
Waiting...
Verifying property watch.concurrent.files exists in config...ERROR >> Unable to process task.
Warning: Required config property "watch.concurrent.files" missing.

阻止这种持续的消息拦截的唯一方法是退出命令行。

这是我的gruntfile:

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
    dist: {
        src: [
            'public/js/libs/*.js',
        ],
        dest: 'public/js/build/production.js',
        }
    },
    uglify: {
        build: {
            src: 'public/js/build/production.js',
            dest: 'public/js/build/production.min.js'
        }
    },
    css: {
      files: ['css/*.scss'],
      tasks: ['sass'],
      options: {
          spawn: false,
      }
    },
    sass: {
    dist: {
        options: {
            style: 'compressed'
        },
        files: {
            'public/css/build/main.css': 'public/css/main.scss'
        }
      }
    },
    nodemon: {
      dev: {
        script: './start.js'
      }
    },
    watch: {
    scripts: {
        files: ['js/*.js'],
        tasks: ['concat','uglify'],
        options: {
            spawn: false,
        },
    },
    concurrent: {
        target: {
            tasks: ['nodemon', 'watch'],
            options: {
                logConcurrentOutput: true
            }
        }
    },
  }
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-nodemon');
grunt.loadNpmTasks('grunt-concurrent');
grunt.registerTask('default', ['concat','uglify','sass','watch','nodemon','concurrent:target']);
};

这是我的package.json:

{
  "name": "**** *****",
  "version": "1.0.0",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-concurrent": "^2.3.1",
    "grunt-contrib-concat": "^1.0.1",
    "grunt-contrib-imagemin": "^1.0.1",
    "grunt-contrib-sass": "^1.0.0",
    "grunt-contrib-uglify": "^2.3.0",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-nodemon": "^0.4.2",
    "webpack-dev-server": "^2.9.7"
  },
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "nodemon ./start.js"
  },
  "author": "**** *******",
  "license": "ISC",
  "dependencies": {
    "cookie-parser": "^1.4.3",
    "dotenv": "^4.0.0",
    "express": "^4.16.2",
    "mongod": "^2.0.0",
    "mongoose": "^4.13.7",
    "nodemon": "^1.14.1",
    "normalize.css": "^6.0.0"
  }
}

编辑:无限循环解决

我仍然认为我没有完全解决我的问题,但我更近了一步......

我的watchtask中有语法错误/遗漏:

    watch: {
    scripts: {
        files: ['js/*.js'],
        tasks: ['concat','uglify'],
        options: {
            spawn: false,
        },
    },

应该包括在内

watch: {
scripts: {
    files: ['js/*.js'],
    tasks: ['concat','uglify'],
    options: {
        spawn: false,
    },
},
css: {
      files: ['css/*.scss'],
      tasks: ['sass'],
      options: {
          spawn: false,
      },
    }
},

这阻止了我的监视任务正常运行。我目前不再拥有无限循环了。而是我的命令行呈现这个:

Running "concat:dist" (concat) task

Running "uglify:build" (uglify) task
>> 1 file created 797.64 kB → 378.58 kB

Running "sass:dist" (sass) task

Running "nodemon:dev" (nodemon) task
[nodemon] 1.14.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./start.js`
Express running → PORT 7777

似乎nodemon正在运行,但它根本没有说明我的watch任务,当我更改我的SCSS文件时,没有任何反应。我显然希望grunt-concurrent同时运行我的nodemonwatch任务。

如果命令行已经成功执行而没有任何错误,那么命令行应该说些什么吗?

谢谢!

答案

nodemon代码中删除watchgrunt.registerTask解决了这个问题:

grunt.registerTask('default', ['concat','uglify','sass','watch','nodemon','concurrent:target']);

正在运行nodemon并观察默认任务,然后在运行concurrent:target时尝试运行concurrent:target无论如何都会同时运行nodemonwatch

现在终端呈现:

Running "concat:dist" (concat) task

Running "uglify:build" (uglify) task
>> 1 file created 797.64 kB → 378.58 kB

Running "sass:dist" (sass) task

Running "concurrent:target" (concurrent) task
    Running "watch" task
    Waiting...
    Running "nodemon:dev" (nodemon) task
    [nodemon] 1.14.1
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching: *.*
    [nodemon] starting `node ./start.js`
    Express running → PORT 7777

哪个看起来正确!

新的,更正的Gruntfile如下所示:

module.exports = function(grunt) {
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    concat: {
    dist: {
        src: [
            'public/js/libs/*.js',
        ],
        dest: 'public/js/build/production.js',
        }
    },
    uglify: {
        build: {
            src: 'public/js/build/production.js',
            dest: 'public/js/build/production.min.js'
        }
    },
    sass: {
    dist: {
        options: {
            style: 'compressed'
        },
        files: {
            'public/css/build/main.css': 'public/css/main.scss'
        }
      }
    },
    concurrent: {
        target: {
            tasks: ['nodemon', 'watch'],
            options: {
                logConcurrentOutput: true
            }
        }
    },
    watch: {
    scripts: {
        files: ['./public/js/*.js'],
        tasks: ['concat','uglify'],
        options: {
            spawn: false,
        },
    },
    css: {
          files: ['./public/css/*.scss'],
          tasks: ['sass'],
          options: {
              spawn: false,
          },
        }
    },
    nodemon: {
      dev: {
        script: './start.js'
      }
    },
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-nodemon');
grunt.loadNpmTasks('grunt-concurrent');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default', ['concat','uglify','sass','concurrent:target']);

};

以上是关于与'nodemon'和'watch'任务一起运行grunt-concurrent时的无限循环的主要内容,如果未能解决你的问题,请参考以下文章

python学习之动态规划解决博物馆大盗问题

求matlab 嵌入提取可见水印的程序

cannot convert from 'const char [3]' to 'char'

python中 ‘r',’w', 'rb' ,'wb'的区别

python中 ‘r',’w', 'rb' ,'wb'的区别

如何在'rake test'中关闭警告'ruby -w'?