Skip to content

Difficulty with webpack multi-compiler and plugin hooks #273

@swashata

Description

@swashata

Feature motivation

Right now, the way the plugin's hook system and webpack multi-compiler works, it makes it quite difficult to actually get the plugin hooks. For example, let's say we have

const ForkTsCheckerPlugin = require('fork-ts-checker-webpack-plugin');
const compiler = webpack([
	// array of webpack configuration with fork-ts-checker plugin
]);
const pluginHooks = ForkTsCheckerPlugin.getCompilerHooks(compiler);

// This won't work, because ForkTsCheckerPlugin didn't attach itself to
// the multi-compiler, rather the individual compilers
pluginHooks.receive.tap((d, l) => {
	console.log('done');
});

In such multi-compiler setup, we can not hook into fork-ts-checker hooks from the multi-compiler instance.

Feature description

However, we can loop over compilers property to get the actual compiler where the plugin is attached.

compiler.compilers.forEach(singleCompiler => {
	const pluginHooks = ForkTsCheckerPlugin.getCompilerHooks(singleCompiler);

	// This will work
	pluginHooks.receive.tap((d, l) => {
		console.log('done');
	});
});

Feature implementation

So I was thinking maybe the ForkTsCheckerPlugin.getCompilerHooks could be a bit intuitive to support multi-compiler out of the box. Internal implementation could be like this

  1. If compiler is a single instance, the don't change anything.
  2. If compiler is a multi-compiler instance, then loop over all compilers instance and add the callback on the same hook for every instance.

Maybe this would be a bit out of scope, but I think and helper would be really nice here. Something like

export function getMultiCompilerHooks(compiler) {
	if (! 'compilers' in compiler) {
		throw new Error('Works only with multi-compiler instance');
	}
	const hooks = [];
	compiler.compilers.forEach(singleCompiler => {
		hooks.push(getCompilerHooks(singleCompiler);
	});
	return hooks;
}

In any case, a little documentation on usage of the hooks would be really nice. I was trying to implement async typechecking and custom error reporting for a multi-compiler friendly webpack setup on my tooling and I really needed to dig through the source code to findout how and why things work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions