-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fix(remix-dev/vite): tree-shake unused route exports #8468
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3de73a
fix(remix-dev/vite): tree shake unused route exports
markdalgleish acee252
Merge branch 'dev' into markdalgleish/tree-shake-unused-route-exports
markdalgleish a777133
handle hmr for virtual client route files
markdalgleish ac4bde2
remove stray console log
markdalgleish 6a60267
Merge branch 'dev' into markdalgleish/tree-shake-unused-route-exports
markdalgleish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@remix-run/dev": patch | ||
--- | ||
|
||
Vite: Tree-shake unused route exports in the client build |
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,37 @@ | ||
import * as path from "node:path"; | ||
import { test, expect } from "@playwright/test"; | ||
|
||
import { createProject, grep, viteBuild } from "./helpers/vite.js"; | ||
|
||
test("Vite / dead-code elimination for unused route exports", async () => { | ||
let cwd = await createProject({ | ||
"app/routes/custom-route-exports.tsx": String.raw` | ||
const unusedMessage = "ROUTE_EXPORT_THAT_ISNT_USED"; | ||
const usedMessage = "ROUTE_EXPORT_THAT_IS_USED"; | ||
|
||
export const unusedRouteExport = unusedMessage; | ||
export const usedRouteExport = usedMessage; | ||
|
||
export default function CustomExportsRoute() { | ||
return <h1>Custom route exports</h1> | ||
} | ||
`, | ||
"app/routes/use-route-export.tsx": String.raw` | ||
import { usedRouteExport } from "./custom-route-exports"; | ||
|
||
export default function CustomExportsRoute() { | ||
return <h1>{usedRouteExport}</h1> | ||
} | ||
`, | ||
}); | ||
let { status } = viteBuild({ cwd }); | ||
expect(status).toBe(0); | ||
|
||
expect( | ||
grep(path.join(cwd, "build/client"), /ROUTE_EXPORT_THAT_ISNT_USED/).length | ||
).toBe(0); | ||
|
||
expect( | ||
grep(path.join(cwd, "build/client"), /ROUTE_EXPORT_THAT_IS_USED/).length | ||
).toBeGreaterThanOrEqual(1); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Reading through the Vite conventions for virtual modules again and wondering if we should do something to ensure the client routes don't get processed/handled by other plugins. We could just prepend with
\0
or model them fully as vmods.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.
I think it should be okay if it's handled by other plugins. In terms of resolving the module, since it's a real file on disk, it'll be treated the same as the original route file. In terms of transforming the file, it's going to behave as if you'd manually written a re-export file, which also would work fine.
The benefit of this approach from what I understand is that Vite will treat this pseudo-virtual module the same as the route file in terms of file changes, invalidation, HMR. This means we don't need to do anything special to invalidate virtual modules when the underlying route changes.