Optimizing barrel file import from local workspace packages does not work with optimizePackageImports and turbopack #75148
Labels
Developer Experience
Issues related to Next.js logs, Error overlay, etc.
Performance
Anything with regards to Next.js performance.
Turbopack
Related to Turbopack with Next.js.
Link to the code that reproduces this issue
https://github.com/PMudra/reproduction-app-optimize-package-imports-npm-workspaces
To Reproduce
npm install
.cd packages/frontend
npm run dev -- --turbopack
Current vs. Expected behavior
Example
In my example project, I am using npm workspaces to organize a Next.js app with a library "my-lib". The folders are like this:
The Next.js app
frontend
depends onmy-lib
. When runningnpm install
, a symlink is created innode_modules
folder by npm like so:node_modules/my-lib -> ../packages/my-lib
.my-lib
has 2 modules:featureA
andfeatureB
. The next.js app currently only usesfeatureA
which is imported using the barrel file inindex.js
.my-lib
looks like this:page.tsx
looks like this:Current behavior
When inspecting which files are loaded, I can currently observe that
featureA
andfeatureB
are being loaded:Expected behavior
I would expect that specifying
optimizePackageImports: ["my-lib"]
would prevent thefeatureB
module to be loaded by the browser.The result should look like as if im importing from
import { featureA } from "my-lib/featureA";
:Provide environment information
Operating System: Platform: linux Arch: x64 Version: #14-Ubuntu SMP PREEMPT_DYNAMIC Sat Nov 30 23:51:51 UTC 2024 Available memory (MB): 63439 Available CPU cores: 16 Binaries: Node: 22.13.0 npm: 10.9.2 Yarn: N/A pnpm: 10.0.0 Relevant Packages: next: 15.2.0-canary.18 // Latest available version is detected (15.2.0-canary.18). eslint-config-next: N/A react: 19.0.0 react-dom: 19.0.0 typescript: 5.7.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Developer Experience, Performance, Turbopack
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
optimizePackageImports
because performance in dev mode is starting to be a pain.modularizeImports
seems to be working, too. But setting that up in my real repo seems to be complicated.my-lib/
intonode_modules/my-lib
, the optimization seems to work fine.There are some workarounds. All of them are not satisfying. Are there better ones?
modularizeImports
.Related issues:
Thanks for looking into this and let me know if you need more information.
The text was updated successfully, but these errors were encountered: