rsbuild icon indicating copy to clipboard operation
rsbuild copied to clipboard

[Bug]: `resolve.dedupe` should support exports fields

Open SoonIter opened this issue 7 months ago • 5 comments

Version

@rsbuild/core 1.3.21

Details

"The alias expanded to an absolute path does not respect exports field."

  1. config like this
// rsbuild.config.ts
resolve: {
   dedupe: ['@tt/web']
}

Add the following two aliases to rspack:

// rspack.config.ts(Rsbuild actually generated)
resolve: {
   alias: {
     "@tt/web$": '/usr/demos/rsbuild/node_modules/.pnpm/@[email protected]/@tt/web',
     "@tt/web/*": '/usr/demos/rsbuild/node_modules/.pnpm/@[email protected]/node_modules/@tt/web' 
   }
}

If the user writes as below

import foo from "@tt/web/canary" 

it is actually equal to

import foo from "/usr/demos/rsbuild/node_modules/.pnpm/@[email protected]/node_modules/@tt/web/canary"

At this moment, if the exports of @tt/web are written like this:

// @tt/web/package.json
exports: {
   "./canary": "./dist/canary.js"
}

Error: resolve not found

Reproduce link

none

Reproduce Steps

none

SoonIter avatar May 26 '25 07:05 SoonIter

My idea is to modify it to this API, which can solve this problem and make it more user-friendly.

resolve: {
   alias: {
     "@tt/web/(.*)": {
           base: "/usr/demos/rsbuild/node_modules/.pnpm/@[email protected]/node_modules/@tt/web",
           request: "@tt/web/$1"
     }
   }
}

SoonIter avatar May 26 '25 08:05 SoonIter

Related: https://github.com/jkrems/proposal-pkg-exports/#:~:text=The%20exports%20target%20must%20be%20a%20string%20and%20start%20with%20%22./%22%20(URLs%20and%20absolute%20paths%20are%20not%20currently%20supported%20but%20may%20be%20supported%20in%20future).

chenjiahan avatar May 30 '25 02:05 chenjiahan

It seems that there is no perfect solution to this problem, so is there an option to provide a more open configuration. Let users customize subpath configuration.

For example, ‘@tt/web/xxx’, through require.resolve, you actually get the real address and support the exports field. So it may be necessary to modify the internal dedupe solution to get alias path. Of course, this seems to have some problems.

@chenjiahan @SoonIter cc

nanianlisao avatar Jun 24 '25 06:06 nanianlisao

Any progress on this?

If the resolver cannot resolve wildcard correctly for now, can we iterate over exports in Rsbuild and set alias using manually resolve (with correct conditionName)?

colinaaa avatar Aug 13 '25 08:08 colinaaa

No progress yet.. @stormslowly cc

chenjiahan avatar Aug 13 '25 08:08 chenjiahan