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

fix: replace module interceptor with guarded require #871

Merged
merged 4 commits into from
Jan 16, 2025

Conversation

jbroma
Copy link
Member

@jbroma jbroma commented Jan 16, 2025

Summary

This PR improves error handling by wrapping webpack's require function with an error boundary, making it behave more similarly to Metro's guardedLoadModule and makes the error handling behaviour more consistent between the two.

  • - Replaced module execution interceptor with a guarded require implementation
  • - Reuses strict module handling from webpack/rspack which caches module errors so they don't need to be evaluated twice when it errors out
  • - Drops requirement for RuntimeGlobals.interceptModuleExecution (which might prevent inlining modules)

Final piece to #866

Additional context

This is roughly how body of __webpack_require__ looks like:

// The require function
function __webpack_require__(moduleId) {
    // Check if module is in cache
    var cachedModule = __webpack_module_cache__[moduleId];
    if (cachedModule !== undefined) {
        if (cachedModule.error !== undefined) throw cachedModule.error;
        return cachedModule.exports;
    }

    // Create a new module (and put it into the cache)
    var module = (__webpack_module_cache__[moduleId] = {
        id: moduleId,
        loaded: false,
        exports: {}
    });

    // Execute the module function
    try {
        var execOptions = { 
            id: moduleId, 
            module: module, 
            factory: __webpack_modules__[moduleId], 
            require: __webpack_require__ 
        };
        
        __webpack_require__.i.forEach(function(handler) { 
            handler(execOptions); 
        });
        
        module = execOptions.module;
        if (!execOptions.factory) {
            console.error("undefined factory", moduleId)
        }
        execOptions.factory.call(module.exports, module, module.exports, execOptions.require);
    } catch (e) {
        module.error = e;
        throw e;
    }

    // Flag the module as loaded
    module.loaded = true;

    // Return the exports of the module
    return module.exports;
}

which matches with the equivalent function loadModuleImplementation inside of metro-runtime: https://github.com/facebook/metro/blob/76fd2186c129deac38023df9d4501b519ef9f011/packages/metro-runtime/src/polyfills/require.js#L403-L517

loadModuleImplementation is called within guardedLoadModule here:
https://github.com/facebook/metro/blob/76fd2186c129deac38023df9d4501b519ef9f011/packages/metro-runtime/src/polyfills/require.js#L328-L347

guardedRequireRuntimeModule is our equivalent of guardedLoadModule from metro-runtime

Test plan

  • - testers work

Copy link

changeset-bot bot commented Jan 16, 2025

🦋 Changeset detected

Latest commit: c88402e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@callstack/repack Patch
@callstack/repack-plugin-reanimated Patch
@callstack/repack-dev-server Patch
@callstack/repack-init Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jan 16, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
repack-website ⬜️ Ignored (Inspect) Visit Preview Jan 16, 2025 0:11am

@jbroma jbroma merged commit 93f2c74 into main Jan 16, 2025
5 checks passed
@jbroma jbroma deleted the fix/module-error-handler branch January 16, 2025 16:17
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

Successfully merging this pull request may close these issues.

1 participant