繁体   English   中英

grunt创建的grunt监视文件

[英]Grunt watch files created by grunt

我使用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)重命名监视任务,再次加载监视

您可以使用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']);

暂无
暂无

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

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