[Bug]: Can't resolve self-referencing
Version
/
Details
Given a package named @monorepo/self for example. If trying to bare import package itself. webpack / Rspack can't resolve the package. It works in webpack / Rspack with exports field when exports target could be resolved. But when not, for example the source is TS, it will failed to resolve.
// index.js
import { a } from './a.js'
import { b } from './b.js'
export { a, b }
// a.js
export const a = 1
// b.js
import { a } from '@monorepo/self'
export const b = 1 + a
error message:
> @monorepo/self@ build:webpack /Users/bytedance/Projects/webpack-test-out-library-esm-output/packages/self-import
> webpack --config rspack.config.mjs
asset main.mjs 2.07 KiB [compared for emit] [javascript module] (name: main)
runtime modules 670 bytes 3 modules
orphan modules 78 bytes [orphan] 2 modules
./src/index.mjs + 2 modules 141 bytes [built] [code generated]
ERROR in ./src/b.js 1:0-34
Module not found: Error: Can't resolve '@monorepo/self' in '/Users/bytedance/Projects/webpack-test-out-library-esm-output/packages/self-import/src'
@ ./src/index.mjs 2:0-22 4:0-15
ERROR in ./src/index.mjs 4:0-15
export 'default' (reexported as 'a') was not found in './a.js' (possible exports: a)
ERROR in ./src/index.mjs 4:0-15
export 'default' (reexported as 'b') was not found in './b.js' (possible exports: b)
1 error has detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.
webpack 5.93.0 compiled with 3 errors in 48 ms
ELIFECYCLE Command failed with exit code 1.
Reproduce link
/
Reproduce Steps
/
A temporary solution is to alias bare import to the entry file, but can't handle complex cases like exports field.
How about just applying aliases only when there's no "exports" field.
In Node.js, "exports" field is mandatory to do self-referencing with the ES Modules system.
If one is doing self-referencing in his project alone with the "exports" field, they may prefer to follow the Node.js way. Applying aliases could be confusing for them.
That's a good point, without exports, we're safe to do so. When exports is provided, user could manually adding alias to force override the spec behaviour.