rspack icon indicating copy to clipboard operation
rspack copied to clipboard

[Feature]: localVars hook to support RetryChunkPlugin

Open zackarychapple opened this issue 1 year ago • 5 comments

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 avatar Jun 17 '24 14:06 zackarychapple

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];
    }
  }
})();

ScriptedAlchemy avatar Jun 17 '24 23:06 ScriptedAlchemy

This is actually a duplicate of this #4602

zackarychapple avatar Jun 21 '24 18:06 zackarychapple

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

chenjiahan avatar Jun 23 '24 06:06 chenjiahan

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 https://github.com/web-infra-dev/rspack/pull/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 https://github.com/web-infra-dev/rsbuild/pull/2086

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

SoonIter avatar Jun 23 '24 06:06 SoonIter

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

zackarychapple avatar Jun 23 '24 18:06 zackarychapple

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar Sep 20 '24 03:09 stale[bot]

since the whole mainTemplate will be deprecated in webpack 6 https://github.com/webpack/webpack/blob/899f06934391baede59da3dcd35b5ef51c675dbe/lib/MainTemplate.js#L51 so localVars won't be supported, we will support addRuntimeModule to meet the need of localvars hook

hardfist avatar Sep 20 '24 06:09 hardfist

I like the addRuntimeModule api better too!

ScriptedAlchemy avatar Nov 12 '24 02:11 ScriptedAlchemy

Implementation using runtimeRequirementInTree https://github.com/KhodorAmmar/rspack-plugin-retry-chunk-load

KhodorAmmar avatar Dec 30 '24 08:12 KhodorAmmar

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar Feb 28 '25 12:02 stale[bot]

bump

zackarychapple avatar Feb 28 '25 14:02 zackarychapple

Because compilation.hooks.runtimeRequirementInTree and compilation.addRuntimeModule had been implemented and KhodorAmmar/rspack-plugin-retry-chunk-load can work. So I think this issue should be close.

LingyuCoder avatar Apr 25 '25 03:04 LingyuCoder