Gulp 4手表未检测到变化

时间:2018-12-20 17:28:38

标签: javascript node.js gulp gulp-4

gulp v.4.0.0是否可以监视gulpfile.js所在文件夹之外的文件?

在较早的版本中,可以将文件路径设置为../../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

将不会检测到对文件['../../style1.scss', '../../style2.scss']的更改,但是将对'./js/main.js'进行更改。我想念什么吗?

1 个答案:

答案 0 :(得分:0)

新的Gulp 4监视任务存在问题。与Gulp 3中的watch任务不同,新版本不可原谅,并且需要具有正确路径分隔符的正确路径结构。

因此,我们不必使用可能导致..\\..\\root\\/style1.scss的路径,而必须将路径转换为../../root/style1.scss之类的适当结构。

这个简单的功能很有帮助,其余的则由gulp和nodejs处理

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

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