简体   繁体   English

Watchify并不总是检测javascript文件中的更改

[英]Watchify doesn't always detect changes in javascript files

I created a gulp task for bundling modules with browserify and I am using watchify to watch for changes. 我创建了一个gulp任务,用于使用browserify捆绑模块,我使用watchify来监视更改。 Here is my gulp task for watchify: 以下是watchify的gulp任务:

gulp.task('watch:browserify', function () {
    var opts = assign({}, watchify.args, {
        entries: ['./js/app.js'],
        debug: true,
        basedir: './app/',
        paths: ['./lib']
    });

    var b = watchify(browserify(opts));

    b.on('update', function () {
        bundle();
    });

    function bundle() {
        gutil.log(gutil.colors.blue("Starting Browserify..."));
        var time = Date.now();
        return b.bundle()
            .on('error', gutil.log.bind(gutil, gutil.colors.red('Browserify Error')))
            .pipe(source('bundle.js'))
            .pipe(buffer())
            .pipe(sourcemaps.init({loadMaps: true}))
            .pipe(sourcemaps.write('.'))
            .pipe(gulp.dest('app'))
            .on('end', function () {
                var duration = Date.now() - time;
                gutil.log(gutil.colors.blue('Finished Browserify') + " (%dms)", duration);
            })
    }

    bundle();
});

If I edit main js file (./js/app.js), the change is always detected. 如果我编辑主js文件(./js/app.js),则始终检测到更改。 But when I edit some other files that the main file requires, the change is detected roughly every other time (but not always). 但是,当我编辑主文件所需的其他文件时,大致每隔一段时间(但并非总是)检测到更改。 Am I doing something wrong here? 我在这里做错了吗?

Here is the full Github repo so maybe you get the complete idea how I planned this to work 这是完整的Github回购,所以也许你完全了解我如何计划这个工作

There are two issues with your code sample. 您的代码示例有两个问题。

First, watch:browserify must either take a callback or return a stream, or race conditions can occur, as discussed here . 首先, watch:browserify必须要么需要一个回调或返回流,或者发生竞争条件,讨论在这里 So, for example, the last line in your task could be return bundle(); 因此,例如,您的任务中的最后一行可能是return bundle(); .

Second, when using watchify, the cache and packageCache options must be passed to browserify() , as shown below and specified here . 其次,使用watchify时,必须将cachepackageCache选项传递给browserify() ,如下所示并在此处指定。

    var b = browserify({ cache: {}, packageCache: {} });

Finally, ensure that app.js in fact depends, somewhere in its dependency chain, on the other files that you are editing. 最后,确保app.js实际上依赖于其依赖链中的某个位置,而不是您正在编辑的其他文件。

I had the same issue, and I had everything @akarve mentioned set up correctly. 我有同样的问题,我已经提到@akarve所提到的一切正确。 I went back to watchify: "^2.6.0" and the issue was resolved. 我回去watchify: "^2.6.0" ,问题解决了。 However build/detection time was a tad slower - about half a second. 然而,构建/检测时间稍微慢一点 - 大约半秒钟。 Still, much better than occasionally (often!) not having my changes detected by watchify. 然而,偶尔(经常!)没有比watchify检测到我的更改更好。

Related discussion here (also where I found the comment about v2.6.0) - https://github.com/substack/watchify/issues/216#issuecomment-119285896 这里的相关讨论(也是我发现有关v2.6.0的评论) - https://github.com/substack/watchify/issues/216#issuecomment-119285896

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

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