Javascript 使用grunt contrib watch实现两个目录的深度单向同步。代码可以工作,但grunt contrib watch重新初始化时间太慢
我有两个目录Javascript 使用grunt contrib watch实现两个目录的深度单向同步。代码可以工作,但grunt contrib watch重新初始化时间太慢,javascript,gruntjs,grunt-contrib-watch,Javascript,Gruntjs,Grunt Contrib Watch,我有两个目录src和compiled。我希望确保从src到compiled的单向数据同步。作为中间步骤,我想编译*.less文件以及使用ES6语法编写的*.js文件的子集 我已经成功地编写了完成我需要的任务: // NOTE: Spawn must be disabled to keep watch running under same context in order to dynamically modify config file. watch: { // Compile LESS f
src
和compiled
。我希望确保从src
到compiled
的单向数据同步。作为中间步骤,我想编译*.less
文件以及使用ES6语法编写的*.js
文件的子集
我已经成功地编写了完成我需要的任务:
// NOTE: Spawn must be disabled to keep watch running under same context in order to dynamically modify config file.
watch: {
// Compile LESS files to 'compiled' directory.
less: {
options: {
interrupt: true,
spawn: false,
cwd: 'src/less'
},
files: ['**/*.less'],
tasks: ['less']
},
// Copy all non-ES6/LESS files to 'compiled' directory. Include main files because they're not ES6. Exclude LESS because they're compiled.
copyUncompiled: {
options: {
event: ['added', 'changed'],
spawn: false,
cwd: 'src'
},
files: ['**/*', '!**/background/**', '!**/common/**', '!contentScript/youTubePlayer/**/*', '!**/foreground/**', '!**/test/**', '!**/less/**', '**/main.js'],
tasks: ['copy:compileSingle']
},
// Compile and copy ES6 files to 'compiled' directory. Exclude main files because they're not ES6.
copyCompiled: {
options: {
event: ['added', 'changed'],
spawn: false,
cwd: 'src/js'
},
files: ['background/**/*', 'common/**/*', 'contentScript/youTubePlayer/**/*', 'foreground/**/*', 'test/**/*', '!**/main.js'],
tasks: ['babel:compileSingle']
},
// Whenever a file is deleted from 'src' ensure it is also deleted from 'compiled'
remove: {
options: {
event: ['deleted'],
spawn: false,
cwd: 'src'
},
files: ['**/*'],
tasks: ['clean:compiledFile']
}
}
grunt.event.on('watch', function(action, filepath, target) {
// Determine which task config to modify based on the event action.
var taskTarget = '';
if (action === 'deleted') {
taskTarget = 'clean.compiledFile';
} else if (action === 'changed' || action === 'added') {
if (target === 'copyCompiled') {
taskTarget = 'babel.compileSingle';
} else if (target === 'copyUncompiled') {
taskTarget = 'copy.compileSingle';
}
}
if (taskTarget === '') {
console.error('Unable to determine taskTarget for: ', action, filepath, target);
} else {
// Drop src off of filepath to properly rely on 'cwd' task configuration.
grunt.config(taskTarget + '.src', filepath.replace('src\\', ''));
}
});
这些任务监视相应的文件。事件处理程序动态修改clean
copy
和babel
任务,以便它们处理添加/更改/删除的文件
然而,我正在查看数千个文件,而监视任务需要相当长的时间来初始化。在我的高端开发PC上,初始化需要6秒以上的时间。每次执行任务后,监视任务都会重新初始化,这一事实加剧了此问题
这意味着,如果我有两个文件,fileA
和fileB
,并且我修改了fileA
并保存了,那么在6秒以上的时间段内,watch无法检测到对fileB
的修改。这将导致我的两个目录之间的去同步
我发现这个GitHub问题与我的问题有关,但它仍然是开放的,没有得到回答:
GitHub上的讨论强调,只有在设置了spawn:false
时,才会出现问题,但是,根据:
如果需要动态修改配置,则必须禁用spawn选项以使手表在相同上下文下运行
因此,我认为我需要继续使用spawn:false
我必须假设这是一个相当标准的Grunt任务过程。我是不是漏掉了什么明显的东西?监视任务是否不适合此用途?其他选择?好吧,我有一个可行的解决方案,但它并不漂亮 我最终还是使用了它来帮助解决问题。不幸的是,它不能很好地与grunt contrib copy配合使用,因为复制文件不会更新其上次修改的时间,因此grunt update将在100%的时间内执行 因此,我分叉了grunt contrib copy并添加了一个选项以允许更新上次修改的时间: 有了这些,我现在可以写:
// NOTE: Spawn must be disabled to keep watch running under same context in order to dynamically modify config file.
watch: {
// Compile LESS files to 'compiled' directory.
less: {
options: {
interrupt: true,
cwd: 'src/less'
},
files: ['**/*.less'],
tasks: ['less']
},
// Copy all non-ES6/LESS files to 'compiled' directory. Include main files because they're not ES6. Exclude LESS because they're compiled.
copyUncompiled: {
options: {
event: ['added', 'changed'],
cwd: 'src'
},
files: ['**/*', '!**/background/**', '!**/common/**', '!contentScript/youTubePlayer/**/*', '!**/foreground/**', '!**/test/**', '!**/less/**', '**/main.js'],
tasks: ['newer:copy:compiled']
},
// Compile and copy ES6 files to 'compiled' directory. Exclude main files because they're not ES6.
copyCompiled: {
options: {
event: ['added', 'changed'],
cwd: 'src/js'
},
files: ['background/**/*', 'common/**/*', 'contentScript/youTubePlayer/**/*', 'foreground/**/*', 'test/**/*', '!**/main.js'],
tasks: ['newer:babel:compiled']
},
// Whenever a file is deleted from 'src' ensure it is also deleted from 'compiled'
remove: {
options: {
event: ['deleted'],
spawn: false,
cwd: 'src'
},
files: ['**/*'],
tasks: ['clean:compiledFile']
}
}
grunt.event.on('watch', function(action, filepath) {
if (action === 'deleted') {
// Drop src off of filepath to properly rely on 'cwd' task configuration.
grunt.config('clean.compiledFile.src', filepath.replace('src\\', ''));
}
});
现在,只有当“src”比“dest”新时,才会复制ES6文件以及非LESS/非ES6文件
不幸的是,当从“src”中删除时,grunt更新版实际上不支持同步删除操作。因此,我继续使用以前的代码进行“删除”操作。此仍有相同的缺陷在删除后,监视任务将暂时停止。您是否查看了?听起来这正是lowkay在公开发行中提到的。在过去的一两个小时里,他一直在玩grunt更新版。它适用于编译代码,但如果文件从“src”复制到“compiled”而不需要更改,则不会更新文件的“上次修改”时间。这将导致每次运行任务时都包含该文件。如果我能找到一种方法来解决这个问题,那么grunt更新版就足够了。你用它吗?这将有助于加快手表的速度,我会检查它!谢谢你的建议。不知道它的存在。你有一个所有函数的详细日志来知道每个函数需要多少钱吗?说清楚一点,每次发生什么事情,你都会复制整个目录?不仅仅是受影响的文件?