Eugene Aseyev
Eugene Aseyev

Reputation: 91

Force webpack-dev-server to reload

I am writing a plugin for webpack which starts good-fence in the background using EXECA. After the job is over I add errors to compilation.errors array. As execa runs in separate process errors added after the webpack compilation process finished. As a result, webpack-dev-server reloads the page with no errors. I had to manually reload the page to see the errors. Is there a way to force webpack-dev-server to reload?

const execa = require('execa');

class GoodFencesWebpackPlugin {
    apply(compiler) {
        let subprocess = null;

        compiler.hooks.make.tap('GoodFencesWebpackPlugin', async (compilation) => {
             const logger = compiler.getInfrastructureLogger('GoodFencesWebpackPlugin');

            if (subprocess) {
                subprocess.kill();
                subprocess = null;
            }

            subprocess = execa('good-fences');
            try {
                await subprocess;
            }
            catch (err) {
                logger.error("\x1b[31m", err.stderr);
                compilation.errors.push(err.stderr);
            }

            subprocess = null;
        })
    }
}

module.exports = GoodFencesWebpackPlugin;

Upvotes: 1

Views: 912

Answers (1)

Eugene Aseyev
Eugene Aseyev

Reputation: 91

After digging in webpack-development-server source codes I found that it is possible to LiveReoload programmatically by calling done hook on compiler instance. It's working because I have compilation.errors updated. So I do the following

const execa = require('execa');

class GoodFencesWebpackPlugin {
    apply(compiler) {
        console.log("plugin");
        let subprocess = null;
        let thisStats;

        compiler.hooks.done.tap('GoodFencesWebpackPlugin', async (stats) => {
            thisStats = stats;
        });

        compiler.hooks.afterEmit.tap('GoodFencesWebpackPlugin', async (compilation) => {
             const logger = compiler.getInfrastructureLogger('GoodFencesWebpackPlugin');
            if (subprocess) {
                subprocess.kill();
                subprocess = null;
            }

            subprocess = execa('good-fences');
            try {
                await subprocess;
            }
            catch (err) {
                logger.error("\x1b[31m", err.stderr);
                compilation.errors.push(err.stderr);
                if (thisStats) {
                    compiler.hooks.done.callAsync(thisStats, () => {});
                }
            }
            finally {
                subprocess = null;
            }
        })
    }
}

module.exports = GoodFencesWebpackPlugin;

Upvotes: 1

Related Questions