Dynamic import of a peer dependency causes inconsistencies between dev and prod #17529
Open
7 tasks done
Labels
feat: deps optimizer
Esbuild Dependencies Optimization
p3-minor-bug
An edge case that only affects very specific usage (priority)
Describe the bug
Issue
I've carefully read plenty of similar issues, but most of them had been already fixed and none of them had my exact structure. Any help is appreciated.
I have a couple of components in my architecture:
library-with-dynamic-import
;markdown-to-text
(was taken just as an example);The
library-with-dynamic-import
library addsmarkdown-to-text
as a peer dependency to itspackage.json
and imports it using dynamic import:Then, the library's source code is built using vite library mode (the config can be found in the library's source code here) and published to the public npm registry.
The application, in its turn, installs
library-with-dynamic-import
and its peer dependencymarkdown-to-text
.In prod build everything is fine and here what I'm seeing in the console:
However, whenever I run the development mode, the issue arises:
As you can see from the screenshot above, there is an extra nested
default
in the module's object, which of course creates inconsistencies between the development and prod modes and breaks the application.Note
If I move
markdown-to-text
frompeerDependencies
todependencies
, everything works fine in both the development and prod modes.In a separate branch I've created a reproduction of when
markdown-to-text
is removed frompeerDependencies
and added todependencies
and built insidelibrary-with-dynamic-import
. You can verify that in this case everything is fine: https://github.com/wijionejs/vite-bug-reproduction/tree/branch_without_peer_depsMy findings
Here some of my findings that might be helpful (or not) for debugging!
Finding 1
If dynamic import of
markdown-to-text
is only present inlibrary-with-dynamic-import
, whenlibrary-with-dynamic-import
is optimized, itsdynamic import
is not modified except for the path (see screenshots below).Application
main.js
file:Optimized
library-with-dynamic-import
:Finding 2
If I also add
dynamic import
ofmarkdown-to-text
to the application source code, both the application'sdynamic import
andlibrary-with-dynamic-import
is modified with some additional logic.Application
main.js
file:Optimized
library-with-dynamic-import
:Reproduction
https://github.com/wijionejs/vite-bug-reproduction
Steps to reproduce
pnpm install
.pnpm build
.pnpm preview
.4.1. Open the console and see the output.
pnpm dev
.5.1. Open the console and see the output
Actual result: Everything works as expected in the prod build. However, there is an extra nested
default
in the development mode.Expected result: Both the development and prod modes have the same output in the console.
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: