简体   繁体   English

grunt创建的grunt监视文件

[英]Grunt watch files created by grunt

I use grunt to automatically create build directory and its content in case any of the source file changes. 我使用grunt来自动创建构建目录及其内容,以防任何源文件发生更改。 Now I am trying to watch build directory and copy its content to my project folders whenever some file in build folder changes, however grunts watch will not detect any changes to the files inside build directory, although they are constantly being overwritten. 现在,我试图监视构建目录,并在构建文件夹中的某些文件发生更改时将其内容复制到我的项目文件夹中,但是grunts watch不会检测到对构建目录中文件的任何更改,尽管它们会不断被覆盖。 What could be the issue? 可能是什么问题?

My simplified grunt config file to give you an idea: 我简化的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"]
        }
    }
});

Although init.js is constantly being overwritten in build directory, the copy:toproject will never be fired. 尽管init.js在构建目录中不断被覆盖,但是copy:toproject将永远不会被触发。 I know I could copy the files right when they are concated from src but I need this to work as I explained above instead. 我知道我可以在从src中提取完文件后立即复制这些文件,但是我需要这样做,如上所述。

Two possible approaches: 两种可能的方法:

1) rename the watch task, load watch a second time 1)重命名监视任务,再次加载监视

You could use grunt.renameTask to rename the first watch task, and then load a second copy of watch. 您可以使用grunt.renameTask重命名第一个监视任务,然后加载第二个监视副本。 It would look something like this: 它看起来像这样:

//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) use grunt-concurrent 2)使用grunt-concurrent

A second option is to use the grunt-concurrent plugin. 第二种选择是使用grunt-concurrent插件。 You wouldn't add watch to any tasks, just the concurrent target. 您不会将watch添加到任何任务,而仅将concurrent目标添加。 In this case, your Gruntfile would resemble this: 在这种情况下,您的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']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM