Skip to content
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

Using Proxy of 'compilation' break plugins relies on other Plugin #198

Open
icy0307 opened this issue Jul 20, 2023 · 2 comments
Open

Using Proxy of 'compilation' break plugins relies on other Plugin #198

icy0307 opened this issue Jul 20, 2023 · 2 comments

Comments

@icy0307
Copy link

icy0307 commented Jul 20, 2023

webpack Plugins can provide hooks for other Plugins;
take html-webpack-plugin for example,
we can write a custom plugin depending on it

// If your plugin is direct dependent to the html webpack plugin:
const HtmlWebpackPlugin = require('html-webpack-plugin');
// If your plugin is using html-webpack-plugin as an optional dependency
// you can use https://github.com/tallesl/node-safe-require instead:
const HtmlWebpackPlugin = require('safe-require')('html-webpack-plugin');

class MyPlugin {
  apply (compiler) {
    compiler.hooks.compilation.tap('MyPlugin', (compilation) => {
      console.log('The compiler is starting a new compilation...')

      // Static Plugin interface |compilation |HOOK NAME | register listener 
      HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
        'MyPlugin', // <-- Set a meaningful name here for stacktraces
        (data, cb) => {
          // Manipulate the content
          data.html += 'The Magic Footer'
          // Tell webpack to move on
          cb(null, data)
        }
      )
    })
  }
}

module.exports = MyPlugin

However, this kind of plugin ceases to work if using [speed-measure-webpack-plugin](https://github.com/stephencookdev/speed-measure-webpack-plugin) together.
Because every time speed-measure-webpack-plugin wrap a plugin, it wraps its inside compilation provide by tap method to a new proxy project.
And every time, this object is a new proxy project, instead of returning memorized one.

So plugins that provide other plugins would not be working anymore, because the need compilation object as different keys for different compile processes.
Hooks that tapped into would never be found.

/**
 * Returns all public hooks of the html webpack plugin for the given compilation
 *
 * @param {WebpackCompilation} compilation
 * @returns {HtmlWebpackPluginHooks}
 */
function getHtmlWebpackPluginHooks (compilation) {
  let hooks = htmlWebpackPluginHooksMap.get(compilation);
  // Setup the hooks only once
  if (hooks === undefined) {
    hooks = createHtmlWebpackPluginHooks();
    htmlWebpackPluginHooksMap.set(compilation, hooks);
  }
  return hooks;
}
@Y-Bingo
Copy link

Y-Bingo commented Sep 19, 2023

I've got the same problem.

@sxxhlx
Copy link

sxxhlx commented Oct 11, 2023

I've got the same problem too.

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

No branches or pull requests

3 participants