Skip to content

[Bug]: Found require.resolveWeak on ESM module for web target build #7830

@Pablinho

Description

@Pablinho

System Info

System:
OS: macOS 14.6.1
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Memory: 32.71 GB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
Yarn: 1.22.5 - /usr/local/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
pnpm: 9.9.0 - ~/.nvm/versions/node/v20.14.0/bin/pnpm
Watchman: 2023.08.14.00 - /usr/local/bin/watchman
Browsers:
Chrome: 128.0.6613.120
Safari: 17.6
npmPackages:
@rspack/cli: ^1.0.0 => 1.0.0
@rspack/core: ^1.0.0 => 1.0.0

Details

Context

Some libraries like @lodable/components or react-loadable leverage this require.resolveWeak capability to implement their code-splitting abstractions. When generating ESM code that use them, we end up with require.resolveWeak inside ESM. This creates the runtime failure.

Issue

In a production Rspack build, a runtime failure was encountered in the browser due to a require.resolveWeak call in a ESM file. In the example, we've set target: 'web' and specified the target browsers in swc-loader:

rspack-dist/479.js

    resolve (props) {
        if (true) {
            return require.resolveWeak(`./locale/${props.locale}/lite.js`);
        }
        return eval('require.resolve')(`./locale/${props.locale}/lite.js`);
    }

We are not encountering this issue in the Webpack build:

wepack-dist/560.js

resolve (props) {
        if (true) {
            return /*require.resolve*/(__webpack_require__(268).resolve(`./${props.locale}/lite.js`));
        }
        return eval('require.resolve')(`./locale/${props.locale}/lite.js`);
    }

I've tried using Babel and encountered the same issue. Any thoughts or workarounds would be appreciated.
Seems similar to this issue in webpack: webpack/webpack#18633. Thanks in advance.

Reproduce link

https://github.com/Pablinho/rspack-resolve-weak-issue

Reproduce Steps

1.- pnpm run build

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpending triageThe issue/PR is currently untouched.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions