9

How Next.js 12 improved Material-UI app compile by 8X

 2 years ago
source link: https://dev.to/kyleapex/how-nextjs-12-improved-material-ui-app-compile-by-8x-47nf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Some are calling Next.js 12 the "biggest step forward yet." Especially Rust fans.

Next.js 12's new Rust compiler features ~3x faster refresh locally and ~5x faster builds, but that's not the feature that helped me most.

The unsung hero of the release for me was...

Compiled module count!

Next.js now outputs Fast Refresh timing in the console for both client and server compilation, including the number of modules and files compiled.

10,000 Modules * faint *

10,000 is great if we're talking about Outliers 📚, but downright scary 😱 when we're talking about modules.

My application uses Material-UI, but is pretty small, so what happened?!

Take a look:

import Menu from '@mui/icons-material/Menu';
import ChevronRight from '@mui/icons-material/ChevronRight';
import Save from '@mui/icons-material/Save';
Enter fullscreen modeExit fullscreen mode

versus:

import { Menu, ChevronRight, Save} from '@mui/icons-material';
Enter fullscreen modeExit fullscreen mode

The shorter one sure looks prettier, and VSCode even suggests importing from @mui/icons-material first, but don't fall for it!

How does it impact the compile time?

// importing from @mui/icons-material
event - compiled successfully in 5.1s (10013 modules)

// importing from @mui/icons-material/ChevronRight
event - compiled successfully in 615ms (1024 modules)
Enter fullscreen modeExit fullscreen mode

As a first time Next.js user, I did not have a project to compare against to recognize that my compile times were slow until the new module count logging feature.

If you use Material-UI, be sure to replace any instances of direct imports from @mui/material or @mui/styles as you will be compiling ALL the modules from the entire packages. Watch out for barrel files too!

With 1,000 modules remaining, I still have some improvements to make, but I figured I'd share for any others who may experience the same shock! How many modules does your Next.js application have?

P.S. Thanks for reading my first DEV article!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK