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

[Bug]: Alias Not Working #4650

Closed
PointSingularity opened this issue Feb 24, 2025 · 9 comments · Fixed by #4663
Closed

[Bug]: Alias Not Working #4650

PointSingularity opened this issue Feb 24, 2025 · 9 comments · Fixed by #4663

Comments

@PointSingularity
Copy link

Version

System:
    OS: macOS 15.3.1
    CPU: (12) arm64 Apple M2 Pro
    Memory: 102.72 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 133.0.6943.127
    Edge: 133.0.3065.82
    Safari: 18.3
  npmPackages:
    @rsbuild/core: ^1.2.8 => 1.2.11

Details

It seems that aliasing is not working properly for Rsbuild, when it works for Rspack.

I tried to make the two configs pretty much the same, but still no luck.

Reproduce link

https://github.com/PointSingularity/rsbuild-repro

Reproduce Steps

Run npm i first.

Rspack works:

npm run build:rspack

Rsbuild does not:

npm run build:rsbuild

Error:

❯ npm run build:rsbuild

> repro-rsbuild@1.0.0 build:rsbuild
> rsbuild build

  Rsbuild v1.2.11

error   Compile error:
Failed to compile, check the errors for troubleshooting.
File: repro-rsbuild/src/index.ts:1:1
  × ESModulesLinkingError: export 'constant' (imported as 'constant') was not found in './module' (module has no exports)
   ╭─[2:12]
 1 │ import { constant } from './module';
 2 │ console.log(constant);
   ·             ────────
   ╰────

File: repro-rsbuild/src/module/index.ts:1:1
  × Cannot find module './example' for matched aliased key './example'
  help: Found module './example.ts'. However, it's not possible to request this module without the extension
        if its extension was not listed in the `resolve.extensions`. Here're some possible solutions:

        1. add the extension `".ts"` to `resolve.extensions` in your rspack configuration
        2. use './example.ts' instead of './example'

 @ ./src/index.ts

error   Failed to build.
error   Rspack build failed!
    at file://repro-rsbuild/node_modules/@rsbuild/core/dist/index.js:5638:87
    at finalCallback (repro-rsbuild/node_modules/@rsbuild/core/node_modules/@rspack/core/dist/index.js:14780:9)
    at repro-rsbuild/node_modules/@rsbuild/core/node_modules/@rspack/core/dist/index.js:14812:16
    at done (repro-rsbuild/node_modules/@rspack/lite-tapable/dist/index.js:473:13)
    at promise.then.index (repro-rsbuild/node_modules/@rspack/lite-tapable/dist/index.js:493:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

@hasnum-stack
Copy link
Contributor

try
'./example': path.resolve(__dirname, 'src/module/example.alias'),

@hasnum-stack
Copy link
Contributor

run DEBUG=rsbuild npm run build:rsbuild, /dist/.rsbuild/rspack.config.web.mjs

@PointSingularity
Copy link
Author

try './example': path.resolve(__dirname, 'src/module/example.alias'),

So this worked, thank you!

Here are the differences:

Image

https://www.diffchecker.com/L8kbYtRj/

Is there a reason why this is not working in rsbulid compared to rspack?

@hasnum-stack
Copy link
Contributor

hasnum-stack commented Feb 26, 2025

in rsbuild, aslias relative paths need to be turned into absolute paths.
/rsbuild/packages/core/src/plugins/resolve.ts, function ensureAbsolutePath

@PointSingularity
Copy link
Author

in rsbuild, aslias relative paths need to be turned into absolute paths. /rsbuild/packages/core/src/plugins/resolve.ts, function ensureAbsolutePath

Ah I see, thanks!

According to the docs it should behave the same as Rspack, so this is a bug or the docs will have to change

Image

@chenjiahan
Copy link
Member

We will update the documentation, thank you

@hasnum-stack
Copy link
Contributor

in rsbuild, aslias relative paths need to be turned into absolute paths. /rsbuild/packages/core/src/plugins/resolve.ts, function ensureAbsolutePath

Ah I see, thanks!

According to the docs it should behave the same as Rspack, so this is a bug or the docs will have to change

Image

A new discovery: In a TypeScript project, configuring paths can make aliasing effective.

@PointSingularity
Copy link
Author

@chenjiahan Thanks for quickly updating the docs!

Is it possible to pass the aliases from Rsbuild to Rspack in the tool config, so my alias setup would work directly?

@chenjiahan
Copy link
Member

yes you can use tools.rspack.resolve.alias

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 a pull request may close this issue.

3 participants