简体   繁体   English

Gulp 4手表未检测到变化

[英]Gulp 4 watch doesn't detect changes

Is it possible with gulp v.4.0.0 to watch for files outside of the folder where gulpfile.js is located? 是否有可能与一饮而尽v.4.0.0看文件夹以外的文件,其中gulpfile.js所在?

In older gulp it was possible to set file path to ../../someFile.css and watch for its changes, but in v4 it doesn't detect changes for the same path. 在较早版本的../../someFile.css ,可以将文件路径设置为../../someFile.css并监视其更改,但是在v4中,它无法检测到同一路径的更改。

 // snip.... let rootFolder = '..\\..\\root\\'; // Watch function let watchThis = (filePath, gulpTasks) => { gulp.watch(filePath, gulp.series(gulpTasks)) .on('change', function(path) { console.log(`${chalk.blue('Changed')}: ${chalk.yellow(path)}`); }) .on('unlink', function(path) { console.log(`${chalk.red('Deleted')}: ${chalk.yellow(path)}`); }); } // Watch task configuration let watchFiles = () => { watchThis([`${rootFolder}/style1.scss`, `${rootFolder}/style2.scss`], 'scss') watchThis('./js/main.js', 'js') } // Final watch task gulp.task('watch', gulp.series( 'development', gulp.parallel( watchFiles, 'startLiveServers' ) )); // ...snip 

Changes to files ['../../style1.scss', '../../style2.scss'] will not be detected, but they will be for './js/main.js' . 将不会检测到对文件['../../style1.scss', '../../style2.scss']更改,但它们将适用于'./js/main.js' Am I missing something? 我想念什么吗?

Problem with new Gulp 4 watch task is in paths. 新的Gulp 4监视任务存在问题。 Unlike watch task from Gulp 3, new version is unforgiving and requires correct path structure with correct paths separators. 与Gulp 3中的watch任务不同,新版本不可原谅,并且需要具有正确路径分隔符的正确路径结构。

So instead of using paths that may result in ..\\\\..\\\\root\\\\/style1.scss , we must convert paths to proper structure like ../../root/style1.scss . 因此,我们不能使用可能导致..\\\\..\\\\root\\\\/style1.scss路径,而必须将路径转换为../../root/style1.scss类的适当结构。

This simple function helps and the rest is handled by gulp and nodejs 这个简单的函数有帮助,其余的由gulp和nodejs处理

let fixPath = (oldPath) => {
    const pathString = oldPath;
    const fix = /\\{1,}|\/{1,}/;

    return pathString.replace(new RegExp(fix, 'gm'), '/').replace(new RegExp(fix, 'gm'), '/')
}

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

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