Skip to content

Detect --watch mode reliably #3460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
s-panferov opened this issue Dec 11, 2016 · 17 comments · Fixed by #8253
Closed

Detect --watch mode reliably #3460

s-panferov opened this issue Dec 11, 2016 · 17 comments · Fixed by #8253

Comments

@s-panferov
Copy link

s-panferov commented Dec 11, 2016

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();
        });
    }
}
@eeue56
Copy link

eeue56 commented Dec 13, 2016

I also need this for the elm-webpack-loader

@TheLarkInn
Copy link
Member

TheLarkInn commented Dec 14, 2016

@sokra Maybe we can pass this in with a small plugin that leverages: 'normal-module-loader' hook from Compilation.

@s-panferov I'm going to test something and see if it works. There is a hook found in NormalModule.js that does the following:

	compilation.applyPlugins("normal-module-loader", loaderContext, this);
	if(options.loader)
		for(var key in options.loader)
			loaderContext[key] = options.loader[key];

This adds information to the loaderContext. If @sokra agree's it's an okay direction maybe we can add a small plugin that detects whether compiler.watch or not. and set a loaderContext.watch = true/false

@TheLarkInn
Copy link
Member

Further research shows that it could be passed through to loaderContext in this manner:

		function apply(compiler) {
			compiler.plugin('compilation', function(compilation) {
				compilation.plugin('normal-module-loader', function(loaderContext, nmf) {
					loaderContext.watch = compiler.options.watch;
				});
			});
		}

But again, I'll yield to @sokra to ensure this is an okay practice to use this hook. Otherwise we could try and implement it into core in NormalModule

@s-panferov
Copy link
Author

@TheLarkInn the main point is how to make this without any plugins because they require more configuration from a user side.

@s-panferov
Copy link
Author

Also please note that you can't rely only on compiler.options.watch, because webpack-dev-server doesn't set it while watching.

@TheLarkInn
Copy link
Member

I'm saying internal implementation from core, IE new feature

@ZebraFlesh
Copy link

This would be a nice new feature. Typical webpack usage seems to be running webpack in watch mode when doing active development. Being able to easily detect this would allow customization of your webpack config. For example, if in watch mode then set config.devtool = "source-map"; or something similar.

@PanayotCankov
Copy link

PanayotCankov commented Dec 18, 2017

I am using something like:

export class WatchStateLoggerPlugin {
    apply(compiler) {
        compiler.plugin("watch-run", function(compiler, callback) {
            isWatching = true;
        }
    }
}

Hope it sounds reasonable.

@alexander-akait
Copy link
Member

Closing due to inactivity. Please test with latest version and feel free to new create if still have problems/regressions. Thanks!

@s-panferov
Copy link
Author

Please reopen. This is a feature request and it is NOT solved.

@alexander-akait
Copy link
Member

Pr welcome

@s-panferov s-panferov changed the title [Question] Detect --watch mode reliably Detect --watch mode reliably Jun 6, 2018
@TheAifam5
Copy link

@s-panferov you can detect the "watch" mode just by subscribing to "watchRun" and "watchClose" compiler hooks - so where is the problem?

@s-panferov
Copy link
Author

@TheAifam5 I just want to be able to understand this from a loader, without requiring user to install a compiler plugin just for this.

@renatoagds
Copy link
Contributor

@evilebottnawi and @TheLarkInn looks like this still open, I would like to help with this, can you guys point out where can I start?

@alexander-akait
Copy link
Member

alexander-akait commented Oct 9, 2018

@renatoagds
Looks need add isWatch (maybe better name) with false/true (false by default) to Compiler https://github.com/webpack/webpack/blob/master/lib/Compiler.js and set true here https://github.com/webpack/webpack/blob/master/lib/Watching.js#L40

@renatoagds
Copy link
Contributor

@evilebottnawi pretty thanks! I'll work on this one.

@renatoagds
Copy link
Contributor

@evilebottnawi added the flag at #8253. can you take a look? 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants