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

@nrwl/next build does not treeshake @nrwl/next library #18271

Closed
1 of 4 tasks
astrodomas opened this issue Jul 24, 2023 · 6 comments
Closed
1 of 4 tasks

@nrwl/next build does not treeshake @nrwl/next library #18271

astrodomas opened this issue Jul 24, 2023 · 6 comments
Assignees
Labels
scope: nextjs Issues related to NextJS support for Nx stale type: bug

Comments

@astrodomas
Copy link

astrodomas commented Jul 24, 2023

Current Behavior

Currently I have 2 client components exported from my @mylib/next but if I import 1 client component via index to my app then my bundlesize increases if it is a direct import - then it decreases.

Can this be due to SWC?

// Increased bundle size
// apps/next-app/pages/carousel

import { MyCarousel } from '@mylib/next'

export const page = () => <MyCarousel />

export default page;
'use client'
// Reduced bundle size
// apps/next-app/pages/carousel
import { MyCarousel } from '@mylib/next/components/Carousel'

export const page = () => <MyCarousel />

export default page;

Root index:

'use client'
// libs/my-lib/src/index.ts
export * from './client'

Components index:

// libs/my-lib/src/components/index.ts
export * from './Carousel'

Component index:

// libs/my-lib/src/components/Carousel/index.ts
export * from './Carousel'
export {default as MyCarousel} from './Carousel'

Before direct import:

image

Expected Behavior (after direct import)

The bundlesize should be reduced:
image

GitHub Repo

No response

Steps to Reproduce

  1. Create a @nx/next application
  2. Create a @nx/next library
  3. Create 2 heavy components inside the library
  4. Export them to a single index.ts
  5. Import a component into a next.js app page
  6. After importing - run the build with bundle analyzer to see the bundlesize of each page
  7. Change the import path to be a direct component path
  8. After the change - run the build again and the bundlesize should be reduced

Nx Report

$ npx nx report
>  NX  Falling back to ts-node for local typescript execution. This may be a little slower.
  - To fix this, ensure @swc-node/register and @swc/core have been installed

 >  NX   Report complete - copy this into the issue template

   Node   : 18.16.0
   OS     : win32 x64
   npm    : 9.5.1
   Hasher : Native
   
   nx                 : 16.2.1
   @nx/js             : 16.2.1
   @nx/jest           : 16.2.1
   @nx/linter         : 16.2.1
   @nx/workspace      : 16.2.1
   @nx/cypress        : 16.2.1
   @nx/devkit         : 16.2.1
   @nx/eslint-plugin  : 16.2.1
   @nx/next           : 16.2.1
   @nx/react          : 16.2.1
   @nx/rollup         : 16.2.1
   @nx/storybook      : 16.2.1
   @nrwl/tao          : 16.2.1
   @nx/web            : 16.2.1
   @nx/webpack        : 16.2.1
   typescript         : 5.0.4
   ---------------------------------------
   Community plugins:
   @nxkit/style-dictionary : 3.0.1

Failure Logs

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@AgentEnder AgentEnder added the scope: nextjs Issues related to NextJS support for Nx label Jul 25, 2023
@astrodomas
Copy link
Author

astrodomas commented Oct 10, 2023

Any news on this? @AgentEnder

@astrodomas
Copy link
Author

astrodomas commented Dec 19, 2023

Still persistent as of nx v17.2.0. By changing imports got a huge bundlesize improvement
image
image

@victorcarvalhosp
Copy link

victorcarvalhosp commented Dec 19, 2023

Still persistent as of nx v17.2.0. By changing imports got a huge bundlesize improvement image image

@astrodomas Not related to the issue itself, but I was not able to send you a private message. How were you able to set this % of Budget that you're showing in the first screenshot?

@astrodomas
Copy link
Author

Still persistent as of nx v17.2.0. By changing imports got a huge bundlesize improvement image image

@astrodomas Not related to the issue itself, but I was not able to send you a private message. How were you able to set this % of Budget that you're showing in the first screenshot?

This is a PR comment generated by the bot

By using https://github.com/hashicorp/nextjs-bundle-analysis , specifically the setup for app router described here:
hashicorp/nextjs-bundle-analysis#83 . But basically it takes nextjs app router build, inside the master branch it uploads an artifact to github and then compares the PR's bundlesize to the masters bundlesize and makes a PR comment on the bundlesize.

Copy link

This issue has been automatically marked as stale because it hasn't had any activity for 6 months.
Many things may have changed within this time. The issue may have already been fixed or it may not be relevant anymore.
If at this point, this is still an issue, please respond with updated information.
It will be closed in 21 days if no further activity occurs.
Thanks for being a part of the Nx community! 🙏

@github-actions github-actions bot added the stale label Nov 12, 2024
@astrodomas
Copy link
Author

This is related to barrel exports.
Meaning, it's not worth solving this on the nx side, however it's something to be aware of. Maybe some docs mentioning this would be useful. @AgentEnder
I'm closing this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: nextjs Issues related to NextJS support for Nx stale type: bug
Projects
None yet
Development

No branches or pull requests

4 participants