Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Brocktho
Copy link

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.

  • Docs

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.

bd8993 and others added 2 commits July 29, 2023 23:11
@changeset-bot
Copy link

changeset-bot bot commented Jul 30, 2023

⚠️ No Changeset found

Latest commit: 0cb08cc

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR


## 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:

Comment on lines +202 to +203
import Button from "@mui/material/Button";
import TextField from "@mui/material/TextField";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```tsx good filename=app/routes/some/route.tsx
```tsx good filename=app/routes/some-route.tsx

@MichaelDeBoey MichaelDeBoey changed the title Add on to Gotchas explaining some easy ways to minimize bundle size, helping both DX and UX. docs(pages/gotchas): add 'Minimizing Bundle Size' section Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants