Alternative to `MainTemplate.hooks.beforeStartup` in Webpack@5

Viewed 705

I'm trying to migrate a plugin from webpack 4 -> 5, however it's using MainTemplate.hooks.beforeStartup and webpack throws

UnhandledPromiseRejectionWarning: Error: MainTemplate.hooks.beforeStartup has been removed (use RuntimeGlobals.startup instead)

yet I haven't found any information on how to use RuntimeGlobals.startup, so what's the proper alternative this hook and how to use it?

1 Answers

Well, after some time, seems like someone finally has figured it out.

So according to him

if (isWebpack4) {
            compilation.mainTemplate.hooks.startup.tap(this.pluginName, (source) => {
                return (this.options.appendScript && this.isRunning())
                    ? require('./autoloadScript')(this.options) + source
                    : source;
            });
        } else {
            compilation.hooks.additionalChunkRuntimeRequirements.tap(this.pluginName, (chunk, set) => {
                set.add(RuntimeGlobals.require);
                set.add(RuntimeGlobals.startup);

                const LiveReloadRuntimeModule = require('./LiveReloadRuntimeModule');
                compilation.addRuntimeModule(chunk, new LiveReloadRuntimeModule(this.options));
            })
        }

The alternative seems to be compilation.hooks.additionalChunkRuntimeRequirements. I haven't tested myself though

Related