Coder Social home page Coder Social logo

Comments (10)

belgattitude avatar belgattitude commented on May 14, 2024 2

And deleting it breaks the build.

If you're on nextjs 10.2+, you can replace it by

const nextConfig = {
  experimental: {
    externalDir: true,
  },
};
export default nextConfig;

See https://github.com/belgattitude/nextjs-monorepo-example#step-33-next-config. It basically does the same but with support for css files too. In my experience it does not affect the bundle

I replaced that config for next-transpile-modules, and the bundle stayed the same.

The experimental.externalDir (or the previous webpack rule you had) is enough. No need for ntm in general. In my experience it does not affect the bundle

There's probably something else to find out

from nextjs-monorepo-example.

lucas-janon avatar lucas-janon commented on May 14, 2024 1

externalDir works perfectly! Thanks.

Yes, we're using a bundle analyzer, and the only way to prevent importing a lot of unneeded modules on _app.ts seems to be not importing from the barrel file:

With barrel file:
image

Without it:
image

from nextjs-monorepo-example.

lucas-janon avatar lucas-janon commented on May 14, 2024 1

Yes! I ended up changing the file from Javascript to Typescript and using import, it previously was a js file because we used to import it from next.config.js, that change reduced the bundle by ~190kb.

We don't build the packages for the frontend, we just build one of them for the backend, so I'm not sure what's happening with sideEffects, honestly, the combination of multiple technologies + versions + configs to make the monorepo setup work, make the debugging process a bit complicated, mostly since learning about the underlying tech to make the monorepo work competes against learning/improving customer-facing tech, that's why I really appreciate what you did with this open-source monorepo, it makes it much smoother.

from nextjs-monorepo-example.

belgattitude avatar belgattitude commented on May 14, 2024 1

That explains yes. Thank for sharing the explanation. Nice

from nextjs-monorepo-example.

belgattitude avatar belgattitude commented on May 14, 2024

We are having issues with tree shaking and barrel (index.ts) files in our setup, we have 3 big shared libraries and we're getting (a lot) more code than we need from them on every Next.js page.

If you suspect something linked to the barrel, I would check if there's no circular deps

ie:

index.ts

export { compOne } from "./compOne";
export { compTwo } from "./compTwo";

compTwo.ts

 import { compOne } from './index.ts';  // or equivalent to import compOne from './';  
 import { compOne } from './compOne'; // that would fix circular deps that webpack won't really digest 

export compTwo = () => compOne(12) * 2;

So I'm curious to know what config is enabling tree shaking to work properly, even without needing "sideEffects": false in the package.json files.

sideEffects is the thing yes... at least when the lib/package is built (prebuild, preconstructed) and published before getting consumed (by nextjs or ...).

In the nextjs-monorepo-example we do not (but we could)... so sideEffect to false won't change anything. Packages are consumed without prior build.

Let me know if helps a bit. Cause I'm not sure I have enough info 😄

from nextjs-monorepo-example.

lucas-janon avatar lucas-janon commented on May 14, 2024

Thanks for the answer, we'll check for circular deps!
For now, we were able to reduce the bundle size by using nested imports instead of the barrel file.
We're also consuming the packages directly from their source (using Vercel's monorepo support and Typescript), and checking our config I noticed that we still have this in next.config.js:

    const resolvedBaseUrl = path.resolve(config.context, "../../");

    config.module.rules = [
      ...config.module.rules,
      {
        test: /\.(tsx|ts|js|mjs|jsx)$/,
        include: [resolvedBaseUrl],
        use: options.defaultLoaders.babel,
        exclude: (excludePath) => /node_modules/.test(excludePath),
      },
    ];

And deleting it breaks the build. I think I took that from a previous version of this repo because the current one doesn't have it, I'm curious if it affects the bundle or not.

This is the error we get after deleting it:

You may need an appropriate loader to handle this file type, currently, no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { ErrorBoundaryModal } from "./ErrorBoundaryModal";
|
> export class ErrorBoundary extends React.Component<UserDataProps, ErrorProps> {
|   state = {
|     error: null,

EDIT:
I replaced that config for next-transpile-modules, and the bundle stayed the same.

from nextjs-monorepo-example.

belgattitude avatar belgattitude commented on May 14, 2024

Have you tried a bundle-analyzer ?

"bundle:analyze": "cross-env ANALYZE=true yarn build",

from nextjs-monorepo-example.

belgattitude avatar belgattitude commented on May 14, 2024

I'm curious now. Webpack 5 right? Not swc ?

What version of nextjs are you using?

Ps: worth to disable this if set

from nextjs-monorepo-example.

lucas-janon avatar lucas-janon commented on May 14, 2024

Ok, mystery solved!

The issue was caused by a require statement (pointing to one of our shared libs) in a config file that was imported in _app.tsx, but the crazy thing is that our setup is affected by setting "sideEffects": false in the libs package.json files.

We have 3 shared libs.

If I set "sideEffects": false in all of them:
image

If I don't:
image

from nextjs-monorepo-example.

belgattitude avatar belgattitude commented on May 14, 2024

Nice.

Have you tried to change the require into an import ? Out of curiosity...

I didn't know package.json would be read when using aliases. So you build your packages prior to consume them?

from nextjs-monorepo-example.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.