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

Optimizing barrel file import from local workspace packages does not work with optimizePackageImports and turbopack #75148

Open
PMudra opened this issue Jan 21, 2025 · 0 comments
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.

Comments

@PMudra
Copy link

PMudra commented Jan 21, 2025

Link to the code that reproduces this issue

https://github.com/PMudra/reproduction-app-optimize-package-imports-npm-workspaces

To Reproduce

  1. Install packages in root of workspace: npm install.
  2. Start the application:
    1. cd packages/frontend
    2. npm run dev -- --turbopack
  3. Inspect the page using your browser dev tools. The "Sources" and "Coverage" tabs are helpful.

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:

├── packages
│   ├── frontend
│   │   ├── app
│   │   │   ├── favicon.ico
│   │   │   ├── layout.tsx
│   │   │   └── page.tsx
│   │   ├── next.config.ts
│   │   ├── next-env.d.ts
│   │   ├── package.json
│   │   └── tsconfig.json
│   └── my-lib
│       ├── featureA.js
│       ├── featureB.js
│       ├── index.js
│       └── package.json
├── package.json
├── package-lock.json
└── README.md

The Next.js app frontend depends on my-lib. When running npm install, a symlink is created in node_modules folder by npm like so: node_modules/my-lib -> ../packages/my-lib.

my-lib has 2 modules: featureA and featureB. The next.js app currently only uses featureA which is imported using the barrel file in index.js. my-lib looks like this:

// featureA.js
export function featureA() {
  console.log("featureA");
}
// featureB.js
export function featureB() {
  console.log("featureA");
}
// index.js (barrel file)
export * from "./featureA";
export * from "./featureB";

page.tsx looks like this:

"use client";
import { featureA } from "my-lib";

export default function Home() {
  featureA();
  return null;
}
// next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  /* config options here */
  reactStrictMode: true,

  experimental: {
    optimizePackageImports: ["my-lib"],
  }
};
export default nextConfig;

Current behavior

When inspecting which files are loaded, I can currently observe that featureA and featureB are being loaded:

Image

Expected behavior

I would expect that specifying optimizePackageImports: ["my-lib"] would prevent the featureB module to be loaded by the browser.

The result should look like as if im importing from import { featureA } from "my-lib/featureA";:

Image

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

  • My example is a very minimal reproduction of the repository I am working on. We have multiple apps and multiple (generated) libraries and we want to make use of optimizePackageImports because performance in dev mode is starting to be a pain.
  • Everything seems to be working as expected when using webpack.
  • The deprecated option modularizeImports seems to be working, too. But setting that up in my real repo seems to be complicated.
  • I suspect symlinks to be the cause of the issue. When I manually copy my-lib/ into node_modules/my-lib, the optimization seems to work fine.
  • I am having the same issue using pnpm which uses symlinks, too. I haven't tried yarn.

There are some workarounds. All of them are not satisfying. Are there better ones?

  • Use webpack.
  • Don't import from barrel files.
  • Move away from the monorepo.
  • Use deprecated modularizeImports.

Related issues:

Thanks for looking into this and let me know if you need more information.

@github-actions github-actions bot added 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. labels Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

1 participant