-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
docs(pages/gotchas): add 'Minimizing Bundle Size' section #6991
base: main
Are you sure you want to change the base?
Conversation
… to mitigate long build times and minimize bundle size for the end user
|
|
||
## Minimizing Bundle Size | ||
|
||
As you continue to develop you may start including larger packages and notice an impact on your build time. There are a few things you can do to minimize your build times that'll also end up being great for your end user (less javascript, faster load times!). Often times larger packages will centralize a location to easily import components or hooks into your app, this is great for convenience but can make it difficult for the compiler to tree shake out code that isn't used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you continue to develop you may start including larger packages and notice an impact on your build time. There are a few things you can do to minimize your build times that'll also end up being great for your end user (less javascript, faster load times!). Often times larger packages will centralize a location to easily import components or hooks into your app, this is great for convenience but can make it difficult for the compiler to tree shake out code that isn't used. | |
As you continue to develop you may start including larger packages and notice an impact on your build time. There are a few things you can do to minimize your build times that'll also end up being great for your end user (less JavaScript, faster load times!). Often times larger packages will centralize a location to easily import components or hooks into your app. This is great for convenience, but can make it difficult for the compiler to tree shake out code that isn't used. |
import { Button, TextField } from '@mui/material'; | ||
``` | ||
|
||
Here Button and TextField are imported from the top-level index of the MUI package, this won't have a massive effect on users but can create more work for the compiler giving you longer build times. Instead you can do the following: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here Button and TextField are imported from the top-level index of the MUI package, this won't have a massive effect on users but can create more work for the compiler giving you longer build times. Instead you can do the following: | |
Here `Button` and `TextField` are imported from the top-level index of the MUI package, this won't have a massive effect on users but can create more work for the compiler giving you longer build times. Instead you can do the following: |
import Button from "@mui/material/Button"; | ||
import TextField from "@mui/material/TextField"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import Button from "@mui/material/Button"; | |
import TextField from "@mui/material/TextField"; | |
import { Button } from "@mui/material/Button"; | |
import { TextField } from "@mui/material/TextField"; |
|
||
This lessens the work the compiler has to do and guarantees a smaller overall bundle size. | ||
|
||
Sometimes you may also import packages that you intend for server use only. Sometimes this will cause immediate errors, but other times the compiler may try adding polyfills that increase your overall bundle size. Take for example core node modules like node:crypto. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes you may also import packages that you intend for server use only. Sometimes this will cause immediate errors, but other times the compiler may try adding polyfills that increase your overall bundle size. Take for example core node modules like node:crypto. | |
Sometimes you may also import packages that you intend for server use only. Sometimes this will cause immediate errors, but other times the compiler may try adding polyfills that increase your overall bundle size. Take for example core Node modules like `node:crypto`. |
} | ||
``` | ||
|
||
Importing crypto this way could lead to the bundler including polyfills for crypto into your client build even if it wasn't directly used on the client. An easy way to get around this is to centralize server imports inside a .server file. For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importing crypto this way could lead to the bundler including polyfills for crypto into your client build even if it wasn't directly used on the client. An easy way to get around this is to centralize server imports inside a .server file. For example: | |
Importing crypto this way could lead to the bundler including polyfills for crypto into your client build even if it wasn't directly used on the client. An easy way to get around this is to centralize server imports inside a `.server` file. For example: |
export { fs, crypto }; | ||
``` | ||
|
||
```tsx good filename=app/routes/some/route.tsx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```tsx good filename=app/routes/some/route.tsx | |
```tsx good filename=app/routes/some-route.tsx |
I created a discussion around minimizing build time and bundle size, wanted to add this to gotchas for the Remix docs since it can be easy to include very large packages or polyfills by accident. I tagged along at the end of the gotcha page a section on tree shaking larger packages, and ensuring server only files aren't having polyfills added to your client bundle mistakenly.
Please let me know any revisions or what else I should do to help get this through as I believe it could be really useful to developers both as they start to develop with Remix and as their application becomes larger and build times increase.