# How to visualize the size of your app bundle.

> Check the size of your Next app using bundle-analyzer.

Daniel Fernández · Nov 07, 2020 · https://dfrico.com/blog/2020-11-07-bundle-analyzer

import Link from 'next/link';
import Image from 'next/image';

export const meta = {
  title: 'How to visualize the size of your app bundle.',
  description: 'Check the size of your Next app using bundle-analyzer.',
  date: 'Nov 07, 2020',
  readTime: 3,
  postImg: '/img/blog/2020-11-07-bundle-analyzer/bundle.jpg',
};

export const metadata = {
  title: `${meta.title} - Daniel Fernandez`,
  description: meta.description,
  openGraph: {
    title: meta.title,
    description: meta.description,
    ...(meta.postImg ? { images: meta.postImg } : {}),
    type: 'website',
  },
  twitter: {
    card: 'summary_large_image',
    site: '@blayhem',
    creator: '@blayhem',
    title: meta.title,
    description: meta.description,
    ...(meta.postImg ? { images: meta.postImg } : {}),
  },
};

If you use Next for developing web applications, you probably are using a [custom `next.config.js` file](https://nextjs.org/docs/api-reference/next.config.js/introduction). This file allows custom advanced configurations on dev server and build phases.

For example, a simplified version of my next config (<Link href="/blog/2020-11-05-mdx-intro">using MDX</Link>), would look like:

```javascript
const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
});

module.exports = withMDX({
  pageExtensions: ['js', 'jsx', 'md', 'mdx'],
});
```

Now, you can use [Next Bundle Analyzer](https://github.com/vercel/next.js/tree/canary/packages/next-bundle-analyzer), or use [cross-env](https://flaviocopes.com/nextjs-analyze-app-bundle/) with Next Bundle Analyzer to check the size of your dependencies, and it will produce a beautiful [treemap](https://en.wikipedia.org/wiki/Treemapping) of them.

<figure>
  <Image
    src="/img/blog/2020-11-07-bundle-analyzer/bundle.jpg"
    alt="application bundle treemap"
    width={700}
    height={400}
    quality={99}
  />
  <figcaption>
    The production bundle dependencies treemap of this site, at the moment of
    writing this article.
  </figcaption>
</figure>

However, a lot of those tutorials tell you to just write something like:

```javascript
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({});
```

What happens if you have more configurations? `withTypescript`, `withSass`... How do you make all of them work together in harmony?

The answer is, you have 2 options:

## 1. Just chain the configurations

If the configurations are simple enough, you can do something like this to chain them:

```javascript
module.exports = withCSS(
  withTypescript(
    withSass({
      cssModules: true,
      webpack: (config) => {
        return config;
      },
    })
  )
);
```

However, depending on the plugins you might be using, this option can be problematic. Entering the second option (that I know so far, and the one I'm using).

## 2. Using `next-compose-plugins`.

If you're already worried about dependencies, adding an extra one might be a doubtful approach. However, [this plugin](https://github.com/cyrilwanner/next-compose-plugins) might be worth it to configure different other plugins where each plugin has its own specific configuration.

So, the way I configured my Next config to use MDX and Next Bundle Analyzer at the same time is:

```javascript
// MDX config
const withMDX = require('@next/mdx')({
  extension: /\.mdx?$/,
});

const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
});

module.exports = withPlugins([
  [
    withMDX,
    {
      pageExtensions: ['js', 'jsx', 'md', 'mdx'],
    },
  ],
  [optimizedImages],
  [withBundleAnalyzer, {}],
]);
```

A simplified version of this would be:

```javascript
// [plugin: function, configuration?: object, phases?: array]
withPlugins([...plugins], nextConfiguration);
```

You can see here that we're using our two plugins (`withMDX` and `withBundleAnalyzer`), and the nextConfiguration is optional so we're passing none.

Thanks to this, we can use all the configurations previously mentioned, and we have an optimized blog, with images inside each post folder, and bundle analyzer to stay informed of our modules sizes. I hope this article was useful!

See you on the next one!

