Skip to content

[Bug]: loaderContext.importModule does not return error #8536

@colinaaa

Description

@colinaaa

System Info

  System:
    OS: macOS 14.6.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 12.00 GB / 96.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 20.17.0 - ~/.local/state/fnm_multishells/51660_1732612003106/bin/node
    npm: 10.8.2 - ~/.local/state/fnm_multishells/51660_1732612003106/bin/npm
    pnpm: 9.14.2 - ~/.local/state/fnm_multishells/51660_1732612003106/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.86
    Safari: 17.6
  npmPackages:
    @rsbuild/core: ^1.0.19 => 1.1.5
    @rsbuild/plugin-sass: ^1.1.1 => 1.1.1
    @rsbuild/plugin-webpack-swc: ^1.0.8 => 1.0.8
    @rsbuild/webpack: ^1.1.3 => 1.1.3

Details

When using rspack.CssExtractRspackPlugin, the error message is weird:

File: /rsbuild-project/src/index.scss:1:1
  × Module build failed:
  ╰─▶   × TypeError: Cannot read properties of undefined (reading '__esModule')
        │     at handleExports (/rsbuild-project/node_modules/.pnpm/@rspack+core@1.1.4_@swc+helpers@0.5.15/node_modules/@rspack/core/dist/cssExtractLoader.js:169:40)
        │     at /rsbuild-project/node_modules/.pnpm/@rspack+core@1.1.4_@swc+helpers@0.5.15/node_modules/@rspack/core/dist/cssExtractLoader.js:263:7
        │     at /rsbuild-project/node_modules/.pnpm/@rspack+core@1.1.4_@swc+helpers@0.5.15/node_modules/@rspack/core/dist/index.js:6274:13

 @ ./src/index.js

After debugging into the source code, I found that Rspack did not return the error in the callback of importModule:

this.importModule(
`${this.resourcePath}.webpack[javascript/auto]!=!!!${request}`,
{
layer: options.layer,
publicPath: publicPathForExtract,
baseUri: `${BASE_URI}/`
},
(error, exports) => {
if (error) {
callback(error);
return;
}
handleExports(exports);
}
);

Thus, an undefined is resolved and caused the TypeError: Cannot read properties of undefined (reading '__esModule') error.

Reproduce link

https://github.com/colinaaa-reproductions/rspack-repro-sass-error

Reproduce Steps

Run Rspack:

  1. pnpm install
  2. pnpm run build

Errors occurs:

error   Compile error:
Failed to compile, check the errors for troubleshooting.
File: /rsbuild-project/src/index.scss:1:1
  × Module build failed:
  ╰─▶   × TypeError: Cannot read properties of undefined (reading '__esModule')
        │     at handleExports (/rsbuild-project/node_modules/.pnpm/@rspack+core@1.1.4_@swc+helpers@0.5.15/node_modules/@rspack/core/dist/cssExtractLoader.js:169:40)
        │     at /rsbuild-project/node_modules/.pnpm/@rspack+core@1.1.4_@swc+helpers@0.5.15/node_modules/@rspack/core/dist/cssExtractLoader.js:263:7
        │     at /rsbuild-project/node_modules/.pnpm/@rspack+core@1.1.4_@swc+helpers@0.5.15/node_modules/@rspack/core/dist/index.js:6274:13

 @ ./src/index.js

File: /Users/colin/rsbuild-project/src/index.scss:1:1
  × Module build failed:
  ╰─▶   × Expected identifier.
        │   ╷
        │ 8 │ ..content {
        │   │  ^
        │   ╵
        │   /rsbuild-project/src/index.scss 8:2  root stylesheet

error   Failed to build.
error   Rspack build failed!

Comparing with Webpack:

  1. USE_WEBPACK=1 npm run build
error   Compile error:
Failed to compile, check the errors for troubleshooting.
File: /Users/colin/rsbuild-project/src/index.scss:1:1
Module build failed (from ./node_modules/.pnpm/@rsbuild+plugin-sass@1.1.1_@rsbuild+core@1.1.5/node_modules/@rsbuild/plugin-sass/compiled/sass-loader/index.js):
Expected identifier.
  ╷
8 │ ..content {
  │  ^
  ╵
  /Users/colin/rsbuild-project/src/index.scss 8:2  root stylesheet
 @ ./src/index.scss
 @ ./src/index.js

File: /Users/colin/rsbuild-project/src/index.scss:1:1
Module build failed (from ./node_modules/.pnpm/mini-css-extract-plugin@2.9.2_webpack@5.96.1/node_modules/mini-css-extract-plugin/dist/loader.js):
HookWebpackError: Module build failed (from ./node_modules/.pnpm/@rsbuild+plugin-sass@1.1.1_@rsbuild+core@1.1.5/node_modules/@rsbuild/plugin-sass/compiled/sass-loader/index.js):
Expected identifier.
  ╷
8 │ ..content {
  │  ^
  ╵
  /Users/colin/rsbuild-project/src/index.scss 8:2  root stylesheet
    at tryRunOrWebpackError (/Users/colin/rsbuild-project/node_modules/.pnpm/webpack@5.96.1/node_modules/webpack/lib/HookWebpackError.js:86:9)
    at __webpack_require_module__ (/Users/colin/rsbuild-project/node_modules/.pnpm/webpack@5.96.1/node_modules/webpack/lib/Compilation.js:5299:12)
    at __webpack_require__ (/Users/colin/rsbuild-project/node_modules/.pnpm/webpack@5.96.1/node_modules/webpack/lib/Compilation.js:5256:18)
    at /Users/colin/rsbuild-project/node_modules/.pnpm/webpack@5.96.1/node_modules/webpack/lib/Compilation.js:5328:20
    at symbolIterator (/Users/colin/rsbuild-project/node_modules/.pnpm/neo-async@2.6.2/node_modules/neo-async/async.js:3485:9)
    at done (/Users/colin/rsbuild-project/node_modules/.pnpm/neo-async@2.6.2/node_modules/neo-async/async.js:3527:9)
    at Hook.eval [as callAsync] (eval at create (/Users/colin/rsbuild-project/node_modules/.pnpm/tapable@2.2.1/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/Users/colin/rsbuild-project/node_modules/.pnpm/tapable@2.2.1/node_modules/tapable/lib/Hook.js:18:14)
    at /Users/colin/rsbuild-project/node_modules/.pnpm/webpack@5.96.1/node_modules/webpack/lib/Compilation.js:5234:43
    at symbolIterator (/Users/colin/rsbuild-project/node_modules/.pnpm/neo-async@2.6.2/node_modules/neo-async/async.js:3482:9)

error   Failed to build.
error   Webpack build failed!

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions