-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
future flag for automatic optimize deps (#9927)
- Loading branch information
Showing
7 changed files
with
84 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: Dependency optimization | ||
--- | ||
|
||
<docs-info>This feature only affects development. It does not impact production builds.</docs-info> | ||
|
||
# Dependency optimization | ||
|
||
Remix introduced automatic dependency optimization in development behind the `future.unstable_optimizeDeps` [Future Flag][future-flags]. | ||
This allows you to opt-into this behavior which will become the default in the next major version of Remix - a.k.a. React Router v7 ([1][rr-v7], [2][rr-v7-2]). | ||
|
||
In development, Vite aims to [prebundle dependencies](https://vitejs.dev/guide/dep-pre-bundling.html) so that it can efficiently serve up those dependencies on-demand. | ||
To do this, Vite needs to know where to start crawling your app's module graph to look for dependencies. | ||
|
||
Previously, Remix did not inform Vite to start dependency detection at route modules nor at the client entry. | ||
That meant that in development, Vite would encounter new dependencies as you navigated around in your app resulting in `504 Outdated Dependency` errors. | ||
Consequently, the development experience could feel janky at times since those errors could cause HMR to break or link navigations to be aborted. | ||
Navigation could also feel sluggish as processing dependencies interactively could sometimes be slow. | ||
|
||
For more information, see [Vite's Dep Optimization Options](https://vitejs.dev/config/dep-optimization-options#dep-optimization-options). | ||
|
||
## Troubleshooting | ||
|
||
### `Failed to resolve entry for package` | ||
|
||
```txt | ||
✘ [ERROR] Failed to resolve entry for package "<package>". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-pre-bundle] | ||
``` | ||
|
||
This is usually caused by a misconfigured dependency. | ||
You use [publint](https://publint.dev/) to check if the offending package is misconfigured. | ||
To fix the issue, you'll need to use `npm why` or `pnpm why` to determine which of your direct dependencies to add to `optimizeDeps.exclude`. | ||
|
||
For example, let's say your app is running into this error: | ||
|
||
```txt | ||
✘ [ERROR] Failed to resolve entry for package "jimp". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-pre-bundle] | ||
``` | ||
|
||
Sure enough, `publint` reports that the [`jimp` package is misconfigured](https://publint.dev/[email protected]). | ||
Then, you determine that `jimp` is an indirect dependency being pulled in by your `svg2img` direct dependency: | ||
|
||
```sh | ||
❯ npm why jimp | ||
[email protected] | ||
node_modules/jimp | ||
jimp@"^0.16.1" from [email protected] | ||
node_modules/svg2img | ||
svg2img@"^1.0.0-beta.2" from the root project | ||
``` | ||
|
||
Finally, you add `svg2img` to `optimizeDeps.exclude`, which should fix the issue: | ||
|
||
```ts filename=vite.config.ts | ||
export default defineConfig({ | ||
optimizeDeps: { | ||
exclude: ["svg2img"], | ||
}, | ||
}); | ||
``` | ||
|
||
[future-flags]: ../guides/api-development-strategy | ||
[rr-v7]: https://remix.run/blog/merging-remix-and-react-router | ||
[rr-v7-2]: https://remix.run/blog/incremental-path-to-react-19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters