Detect `–watch` mode reliably

Hi @sokra. I’m making https://github.com/s-panferov/awesome-typescript-loader and I need some way to detect if webpack is working in --watch mode or not (without plugins). I need this because I need to know if I should kill a spawned child_process or not.

Right now I’m doing something like:

function isWatching(compiler) {
    return compiler.options.watch || compiler.outputFileSystem.constructor.name === 'MemoryFileSystem';
}

But in doesn’t seems to work in the latest webpack@beta. Can you please advise some reliable solution?

Right now the only way I see is to use a compiler plugin:

export const WatchModeSymbol = Symbol('WatchMode');

export class DetectWatchPlugin {
    apply(compiler) {
        compiler.plugin("run", function(params, callback) {
            compiler[WatchModeSymbol] = false;
            callback();
        });

        compiler.plugin("watch-run", function(params, callback) {
            compiler[WatchModeSymbol] = true;
            callback();
        });
    }
}

Author: Fantashit

4 thoughts on “Detect `–watch` mode reliably

Comments are closed.