使用监视任务时,Grunt.js会生成一个新的节点进程

时间:2013-06-25 11:49:19

标签: windows node.js gruntjs

当使用grunt的监视任务时,Grunt.js显然会产生一个新的node.js进程:

http://www.youtube.com/watch?v=fgRlcFt9dkg

一旦保存文档,就会执行默认任务,并且(正如您在视频中看到的那样)突然出现了一个新的node.exe进程。

这是我的gruntfile:

module.exports = function(grunt) {

    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-compress');
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-qunit');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-text-replace');
    grunt.loadNpmTasks('grunt-yui-compressor');

  // Project configuration.
    grunt.initConfig({
        // Metadata.
        meta: {
            version: '1.0',
        },
        banner: '/*! Copyright (c) <%= grunt.template.today("yyyy") %> */\n',
        // Task configuration.
        concat: {
            options: {
                banner: '<%= banner %>',
                stripBanners: true
            },
            develop: {
                src: ['js/app.main.js', 'js/app/*.js', 'js/app.debug.js', 'js/app.workarounds.js', 'js/app.init.js'],
                dest: 'build/js/app.js'
            },
            production: {
                src: ['js/app.main.js', 'js/app/*.js', 'js/app.workarounds.js', 'js/app.init.js'],
                dest: 'build/js/app.js'
            },
            habla_css: {
                src: ['build/habla.css'],
                dest: 'build/habla.css'
            },
            wiwo_css: {
                src: ['build/wiwo.css'],
                dest: 'build/wiwo.css'
            },
            iefix: {
                src: 'js/iefix/*',
                dest: 'build/js/iefix.js'
            },
            libs: {
                src: ['js/src/*'],
                dest: 'build/js/libs.js'
            }
        },
        clean: {
            pre: [
                'build'
            ],
            post: 'dest'
        },
        copy: {
            thirdparty: {
                files: [
                    {
                        expand: true, 
                        src: ['js/jquery.min.js'],
                        dest: 'build/',
                    }]
            }
        },
        min: {
            options: {
                report: false,
                banner: '<%= banner %>'
            },
            app: {
                src: ['build/js/app.js'],
                dest: 'build/js/app.min.js'
            },
            iefix: {
                src: 'js/iefix.js',
                dest: 'build/js/iefix.min.js'
            },
            libs: {
                src: ['js/src/*'],
                dest: 'build/js/libs.min.js'
            },
        },
        cssmin: {
            options: {
                report: false,
                banner: '<%= banner %>'
            },
            icons: {
                src: ['build/icons.css'],
                dest: 'build/icons.min.css'
            },
            habla: {
                src: ['build/habla.css'],
                dest: 'build/habla.min.css'
            },
            wiwo: {
                src: ['build/wiwo.css'],
                dest: 'build/wiwo.min.css'
            }
        },
        less: {
            icons: {
                src: ['less/system/icons.less'],
                dest: 'build/icons.css'
            },
            habla: {
                src: ['less/habla.less'],
                dest: 'build/habla.css'
            },
            wiwo: {
                src: ['less/wiwo.less'],
                dest: 'build/wiwo.css'
            }
        },
        replace: {
            production: {
                src: 'build/js/app.js',
                dest: 'build/js/app.js',
                replacements: [{
                    from: /\s*App.debug.(log|group|groupEnd)\(.*\);/g,
                    to: ''
                },
                {
                    from: 'debug: true',
                    to: 'debug: false'
                }]
            },
            date: {
                src: 'build/js/app.js',
                dest: 'build/js/app.js',
                replacements: [{
                    from: '/*!%CURDATE%*/',
                    to: 'console.log("' + d.replace(/(T|Z)/gi,' ') + '");'
                }]
            }
        },
        qunit: {
            files: ['tests/**/*.html']
        },
        watch: {
            main: {
                files: ['grunt.js', 'js/app.*.js', 'js/**/*', 'less/**/*', 'less/*'],
                tasks: ['default'],
                options: {
                    nospawn: true
                }
            }
        }

    });


    grunt.registerTask('con', ['concat:habla_css', 'concat:wiwo_css', 'concat:iefix', 'concat:libs']);
    grunt.registerTask('icons', ['less:icons cssmin:icons']);
    grunt.registerTask('test', ['qunit']);

    // Default task.
    grunt.registerTask('default', ['less:habla', 'less:wiwo', 'concat:develop', 'con', 'replace:date', 'copy:thirdparty', 'clean:post', 'watch']);
    grunt.registerTask('release', ['clean:pre', 'less', 'concat:production', 'con', 'replace:production', 'copy:thirdparty', 'min', 'cssmin', 'clean:post']);

};

这是一个咕噜咕噜的bug还是我的gruntfile中的一个bug?我在Windows 7 x64上使用的grunt版本是0.4.1(+ grunt-cli 0.1.9)。

2 个答案:

答案 0 :(得分:5)

您不应将'watch'作为默认任务序列中的最终任务。您的监视配置会调用默认值,然后每次都会生成另一个监视任务。这一行是问题所在:

grunt.registerTask('default', ['less:habla', 'less:wiwo', 'concat:develop', 'con', 'replace:date', 'copy:thirdparty', 'clean:post', 'watch']);

应该是这样:

grunt.registerTask('default', ['less:habla', 'less:wiwo', 'concat:develop', 'con', 'replace:date', 'copy:thirdparty', 'clean:post']);

您只需运行grunt:watch即可观看文件。

如果你想在不做监视的情况下运行默认任务序列,那么你只需运行grunt

答案 1 :(得分:1)

就是这样。谢谢!

我对它进行了一些优化,因此我仍然可以运行grunt(不指定watch参数):

watch: {
    main: {
        files: ['grunt.js', 'js/app.*.js', 'js/**/*', 'less/**/*', 'less/*'],
        tasks: ['develop']
    }
}

grunt.registerTask('develop', ['less:habla', 'less:wiwo', 'concat:develop', 'con', 'replace:date', 'copy:thirdparty', 'clean:post']);
grunt.registerTask('release', ['clean:pre', 'less', 'concat:production', 'con', 'replace:production', 'copy:thirdparty', 'min', 'cssmin', 'clean:post']);
grunt.registerTask('default', ['develop', 'watch']);

工作正常!谢谢!