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

[Feature]: localVars hook to support RetryChunkPlugin #6837

Open
zackarychapple opened this issue Jun 17, 2024 · 5 comments
Open

[Feature]: localVars hook to support RetryChunkPlugin #6837

zackarychapple opened this issue Jun 17, 2024 · 5 comments
Labels
feat New feature or request

Comments

@zackarychapple
Copy link
Collaborator

What problem does this feature solve?

When trying to use webpack-retry-chunk-load-plugin the build fails because of a missing hook.

What does the proposed API of configuration look like?

Bring through the localVars hook to support RetryChunkPlugin

@zackarychapple zackarychapple added feat New feature or request pending triage The issue/PR is currently untouched. labels Jun 17, 2024
@ScriptedAlchemy
Copy link
Contributor

You can patch the same functionality without a plugin with something like this. Just put it at the top of your entrypoint and that should work.

(function() {
  // Save the original __webpack_require__.e function
  var originalEnsure = __webpack_require__.e;

  // Create a retry wrapper around the original ensure function
  function retryEnsure(chunkId, retries = 3) {
    function tryEnsure(attempt) {
      return originalEnsure(chunkId).catch(error => {
        if (attempt < retries) {
          console.warn(`Retrying to load chunk ${chunkId}, attempt ${attempt}`);
          return tryEnsure(attempt + 1);
        } else {
          console.error(`Failed to load chunk ${chunkId} after ${retries} attempts`);
          throw error;
        }
      });
    }
    return tryEnsure(1);
  }

  // Patch the original __webpack_require__.e function
  __webpack_require__.e = function(chunkId) {
    return retryEnsure(chunkId);
  };

  // If there are any additional properties or methods on __webpack_require__.e, copy them over
  for (var key in originalEnsure) {
    if (originalEnsure.hasOwnProperty(key)) {
      __webpack_require__.e[key] = originalEnsure[key];
    }
  }
})();

@zackarychapple
Copy link
Collaborator Author

This is actually a duplicate of this #4602

@chenjiahan
Copy link
Member

@SoonIter Can you refer to this implementation? Maybe it will be helpful for Rsbuild's assets retry plugin

@SoonIter
Copy link
Member

This part of code seems to be marked and will be deprecated in webpack6.
https://webpack.js.org/blog/2020-10-10-webpack-5-release/#mainchunkmoduletemplate-deprecation
https://github.com/webpack/webpack/blob/77a4398a907970a4bac66b45370806a8ec5e047c/lib/MainTemplate.js#L40

So the backup hooks maybe #5370

I reimplemented this plugin of Rspack in Rsbuild, at present, this plugin has not been exposed to users of rspack.
https://github.com/web-infra-dev/rsbuild/blob/85a7ced8287ed21c376dce51ee5fca94110fe187/packages/plugin-assets-retry/src/AsyncChunkRetryPlugin.ts#L88
web-infra-dev/rsbuild#2086

I think we need webpack-retry-loading-plugin to adjust the implementation, or we can provide a rspack-retry-loading-plugin.

@zackarychapple
Copy link
Collaborator Author

Luckily @hardfist did a little work to get us moving again and created this rspack-retry-chunk

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

No branches or pull requests

5 participants