Files
react/examples/tree-shaking-nextjs
Andrew 53192111a8 Fix typos (#595)
* fix(typo):  customise -> customize (en-us)

* fix(typo): Recat -> React

* fix(typo): porject -> project

Co-authored-by: spectralapps <56676505+spectralapps@users.noreply.github.com>
2021-08-05 18:36:42 +03:00
..
2020-08-24 18:40:07 +08:00
2020-08-24 18:40:07 +08:00
2020-08-24 18:40:07 +08:00
2020-08-24 18:40:07 +08:00
2021-08-05 18:36:42 +03:00

Tree shaking with Next.js

About

This is example of tree-shaking for next.js. Note that @next/bundle-analyzer is not required.

Getting Started

First, run the development server:

npm run dev
# or
yarn dev

Open http://localhost:3000 with your browser to see the result.

Run the following command to see the size of the bundle:

npm run analyze
# or
yarn analyze

Preview

If we don't do anything, the volume of geist-ui/react is about 58kb, note that the volume here includes lib styled-jsx. It means that even if you use lib styled-jsx again, the volume will not increase.

At present, it seems that this volume of component library is still acceptable:

output-before


When we use tree shaking:

The volume of geist-ui/react is about 13kb.(It consists of two parts) It should be noted that the specific package size depends on how many components you use.

output-after

Other

If you don't use tree shaking in your project, bundle geist-ui/react as a chunk every time, you may notice that the hash name of chunk is still changing, this may cause you to not make full use of the cache.

  • This issue from next.js, and they're improving that, you can track progress here.
  • If you want to customize config of webpack, to ensure that the chunk from geist-ui/react is always the same, you can refer to this document.