question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Config dependency manager doesn't take into account all the hooks until "initialize".

See original GitHub issue

Bug report

What is the current behavior? I use configuration dependencies to make sure that the plugins from the second config 'server' will always be executed after the first config 'client' output is emitted:

module.exports = [
  {
    name: 'client',
    plugins: [
      {
        apply: compiler => compiler.hooks.afterEmit.tap('Plugin1', _ => {
          console.log('Config 1: afterEmit')
        })
      }
    ]
  },
  {
    name: 'server',
    dependencies: ['client'],
    plugins: [
      {
        apply: compiler => compiler.hooks.afterResolvers.tap('Plugin2', _ => {
          // I picked the "afterResolvers" hook, because i will apply here the HtmlWebpackPlugin,
          // that is required to be applied before the "initialize" step.
          console.log('Config 2: afterResolvers')
        })
      }
    ]
  }
]

Doesn’t work in the right order for the hooks like afterResolvers (or initialize), as seen in the console output:

Config 2: afterResolvers
Config 1: afterEmit

However, this works as expected starting from the beforeRun hook:

Config 1: afterEmit
Config 2: beforeRun

What is the expected behavior?

Config 1: afterEmit
Config 2: afterResolvers

Other relevant information: webpack version: 5.74.0 Node.js version: 16.17.0 Operating System: Windows 10

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Oct 6, 2022

@reves I want to close the issue, because we can’t fix it here, HtmlWebpackPlugin should be rewritten without initialize hook and get entrypoint from compilation (not from configuration, so you code will work without workarounds)

1reaction
alexander-akaitcommented, Sep 30, 2022

HtmlWebpackPlugin should not use compiler.hooks.initialize to get options, that is wrong and invalid, options can be changed in future too, so it should be rewritten to get options in the valid hooks, but here workaround:

const Plugin = require("html-webpack-plugin")

const compiler = [
  {
    name: 'client',
    output: {
      filename: "client-[name].js"
    },
    plugins: [
      {
        apply: compiler => compiler.hooks.afterEmit.tapAsync('Plugin1', (compilation, callback) => {
          console.log('Config 1: afterEmit')

          callback()
        })
      }
    ]
  },
  {
    name: 'server',
    dependencies: ['client'],
    output: {
      filename: "server-[name].js"
    },
    plugins: [
      {
        apply: compiler => compiler.hooks.beforeRun.tap('Plugin2', (compiler) => {
          new Plugin().apply(compiler);

          compiler.hooks.initialize.call(compiler);
        })
      }
    ]
  }
]

module.exports = compiler;

/cc @jantimon can we rewrite it, to get entries you can use this hook - https://github.com/webpack/webpack/blob/00c57ede11ee10dbb0a42e944b2cd7a038089944/lib/ProgressPlugin.js#L393, other things are just default options and small logic, you don’t need it to do inside hook and it is wrong

Read more comments on GitHub >

github_iconTop Results From Across the Web

Packer.nvim - GitHub
A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config ...
Read more >
How to fix missing dependency warning when using useEffect ...
I have this current setup, React hook useEffect runs continuously forever/infinite loop and the only comment is about useCallback() which I'm not familiar...
Read more >
DbContext Lifetime, Configuration, and Initialization - EF Core
The DbContext lifetime; DbContext in dependency injection for ASP. ... Make sure to await all async calls before continuing to use the ...
Read more >
Configuration Blocks and Attributes - Terragrunt
The working directory for hooks associated with init-from-module will run in the terragrunt config directory, while the working directory for hooks associated ...
Read more >
React useReducer Hook ultimate guide - LogRocket Blog
In the code below, we initialize state with the useReducer Hook: ... React doesn't use the (state = initialState, action) => newState Redux ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found