簡體   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