由grunt创建的Grunt监视文件

时间:2014-11-26 11:17:27

标签: javascript gruntjs grunt-contrib-watch

我使用grunt自动创建构建目录及其内容,以防任何源文件发生变化。现在,我正在尝试观察构建目录,并在构建文件夹中的某个文件发生更改时将其内容复制到我的项目文件夹中,但是grunts watch不会检测构建目录中文件的任何更改,尽管它们会不断被覆盖。可能是什么问题?

我简化的grunt配置文件给你一个想法:

grunt.initConfig({
    concat:{
        js:{
            files:{
                "build/init.js : ["src/init.js"]
            }
        }
    },
    copy:{
        toproject:{
            expand: true,
            filter: "isFile",
            cwd : "build/",
            src : "*.js",
            dest : "d:/path/to/my/project/js/"
        }
    },
    watch:{
        js:{
            files : ["src/*.js"],
            tasks : ["concat:js"]
        },
        copybuildtoproject:{
            files : ["build/*.js"],
            tasks : ["copy:toproject"]
        }
    }
});

尽管在构建目录中不断覆盖init.js,但永远不会触发copy:toproject。我知道我可以在从src汇总时正确复制这些文件但是我需要这个工作,就像我上面解释的那样。

1 个答案:

答案 0 :(得分:0)

两种可能的方法:

1)重命名监视任务,再次加载监视

您可以使用grunt.renameTask重命名第一个监视任务,然后加载第二个监视副本。它看起来像这样:

//from your grunt.initConfig...
watch:{
    js:{
        files : ["src/*.js"],
        tasks : ["concat:js"]
    }
},
watchbuild:{
    copybuildtoproject:{
        files : ["build/*.js"],
        tasks : ["copy:toproject"]
    }
}

// then later...
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.renameTask('watch', 'watchbuild');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('default',['watch','watchbuild']);

2)使用grunt-concurrent

第二种选择是使用grunt-concurrent插件。您不会将watch添加到任何任务,只需添加concurrent目标。在这种情况下,您的Gruntfile将类似于:

grunt.initConfig({
    watch:{
        js:{
            files : ["src/*.js"],
            tasks : ["concat:js"]
        },
        copybuildtoproject:{
            files : ["build/*.js"],
            tasks : ["copy:toproject"]
        }
    },
    concurrent: {
        watch1: ['watch:js'],
        watch2: ['watch:copybuildtoproject']
    }
});

grunt.registerTask('default', ['concurrent:watch1','concurrent:watch2']);
相关问题